sqlite3 - a create of table x fails after dropping table x (only on Windows)

ppotrebic at box.com ppotrebic at box.com
Sat Mar 16 03:26:39 EDT 2013


Interested to understand this problem. It only happens in Windows, not MacOS. Running python 2.7.3. sqlite3 3.7.12. The following script:

    import sqlite3
    table_name = 'table_name'
    conn = sqlite3.connect('data2.db')
    c = conn.cursor()
    
    sql = 'create table if not exists ' + table_name + ' (id integer)'
    c.execute(sql)
    sql = 'drop table ' + table_name
    c.execute(sql)
    
    sql = 'create table if not exists ' + table_name + ' (id integer)'
    c.execute(sql)
    
    sql = 'insert into ' + table_name + ' (id) values (%d)' % 97
    c.execute(sql)
    
    conn.commit()

Fails every other time it is executed. Here's the error:
   Traceback (most recent call last):
     File "C:/Users/potrebic/PycharmProjects/play/play.py", line 15, in <module>
       c.execute(sql)
   sqlite3.OperationalError: no such table: table_name

line 15 is the execute of the 'insert' stmt.

If the "if not exists" is removed from line 11, then the scripts works every time.

(Note - on MacOS it succeeds every time).



More information about the Python-list mailing list