[Tutor] Python sqlite3 issue
Juan Christian
juan0christian at gmail.com
Wed Oct 22 19:38:04 CEST 2014
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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141022/4e7544eb/attachment-0001.html>
More information about the Tutor
mailing list