So guys, now I have this code that is fully working:

import sqlite3

db = sqlite3.connect('db.sqlite')


def ini_db():
    db.execute('''
            CREATE TABLE IF NOT EXISTS TOPICS(
                    ID INTEGER NOT NULL,
                    URL VARCHAR NOT NULL,
                    AUTHOR VARCHAR NOT NULL,
                    MESSAGE VARCHAR NOT NULL
                );
            ''')


def insert_db(_id, url, author, message):
    db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES (?, ?,
?, ?)", (_id, url, author, message))
    db.commit()


def get_db(_id):
    cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE
ID = ?", (_id,))
    return cursor.fetchone()


if __name__ == '__main__':
    ini_db()

    insert_db(12, 'abc.com', 'a', 'b')
    insert_db(20, 'abc2.com', 'a2', 'c')
    insert_db(1, 'abc3.com', 'a3', 'd')

    for row in db.execute('SELECT * FROM TOPICS ORDER BY ID'):
        print(row)

    db.close()


------

Anything else that I need to improve/change? Regarding the 'DROP TABLE'
before creating, It wouldn't be good for me, because I do want the 'old'
data from the table there, I don't want to drop them!
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to