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

P

ppotrebic

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top