[Tutor] Python sqlite3 issue

Alan Gauld alan.gauld at btinternet.com
Tue Oct 21 11:43:30 CEST 2014


On 21/10/14 09:18, Peter Otten wrote:

> Another option is to only create the table if it does not already exist:
>
> create table if not exists topics ...

The danger with that one is that if you are changing the structure of 
the table you can wind up keeping the old structure by accident and your 
queries no longer match the table. This is especially common in SQLite 
since the ALTER TABLE command is so weak in capability that you often 
have to recreate tables to make changes.

In more powerful SQL databases I use the "if not exists" quite
often for creates because I can do most restructuring via ALTER,
but in SQLite I usually drop and then create.

The exception to that is of course if you are working on a pre-populated 
database with many tables. Then the exists check
can prevent you wiping out a key tabl;e and its data by accidental
name duplication.

-- 
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