[Tutor] Python sqlite3 issue
Alan Gauld
alan.gauld at btinternet.com
Mon Oct 20 23:59:35 CEST 2014
On 20/10/14 18:04, Juan Christian wrote:
> CREATE TABLE TOPICS(
> ID INT PRIMARY KEY NOT NULL,
This means SQLite will automatically create the ID value,
you do not need to provide one.
> 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
> ({}, {}, {}, {})".format(_id, url, author, message))
> db.commit()
So in the insert you should only provide the 3 values
URL, AUTHOR, MESSAGE
The use of format is also a mistake but I see that Danny has
already pointed that out.
Finally when creating tables you can use:
DROP TABLE IF EXISTS TOPICS;
Prior to creating it. That will ensure you don't get an existing
table error.
Some other databases provide other, more conservative, mechanisms
for safely creating tables but SQLite doesn't support them so dropping
the table and then recreating it is your best option.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list