我正在研究一个scrabblecheat程序如何测试一个表是否已经存在?
下面的一些例子我有下面的代码使用SQLite来存储我的单词的简单数据库。
但它告诉我我不能重新创建数据库表。
如何在检查中写入是否已经有名为spwords
的表,然后跳过尝试创建它?
错误:
(<class 'sqlite3.OperationalError'>, OperationalError('table spwords already exists',), None)
验证码:
def load_db(data_list):
# create database/connection string/table
conn = sqlite.connect("sowpods.db")
#cursor = conn.cursor()
# create a table
tb_create = """CREATE TABLE spwords
(sp_word text, word_len int, word_alpha text, word_score int)
"""
conn.execute(tb_create) # <- error happens here
conn.commit()
# Fill the table
conn.executemany("insert into spwords(sp_word, word_len, word_alpha, word_score) values (?,?,?,?)", data_list)
conn.commit()
# Print the table contents
for row in conn.execute("select sp_word, word_len, word_alpha, word_score from spwords"):
print (row)
if conn:
conn.close()
你忘了通过在纠正了例如呼叫 – flaschbier
到isTable连接。谢谢,但您应该随时自行解决问题。 – jdr5ca