Python 3.x SQLite3 Python3有什么问题
我想不出是什么问题。我收到以下错误消息: sqlite3.2错误:没有这样的表:Atm。这发生在第34行,它是insertData函数,一旦到达cursor.execute(sql)。一切似乎都很好,有人能帮忙吗 当我在第14行按“Y”重新创建当前表时,这会崩溃Python 3.x SQLite3 Python3有什么问题,python-3.x,sqlite,Python 3.x,Sqlite,我想不出是什么问题。我收到以下错误消息: sqlite3.2错误:没有这样的表:Atm。这发生在第34行,它是insertData函数,一旦到达cursor.execute(sql)。一切似乎都很好,有人能帮忙吗 当我在第14行按“Y”重新创建当前表时,这会崩溃 import sqlite3 import time def create_table(dbName, table_name, sql): with sqlite3.connect('ATM.db') as db
import sqlite3
import time
def create_table(dbName, table_name, sql):
with sqlite3.connect('ATM.db') as db:
cursor = db.cursor()
cursor.execute("select name from sqlite_master where name=?",(table_name,))
result = cursor.fetchall()
keep_table = True
if len(result) == 1:
response = input("The table {0} already exists, do you wish to recreate it (y/n)".format(table_name))
if response.lower() == "y":
keep_table = False
print("The {0} table will be recreated".format(table_name))
cursor.execute("drop table if exists {0}".format(table_name))
db.commit()
insertData()
else:
print("The existing table was kept")
insertData()
else:
keep_table = False
if not keep_table:
cursor.execute(sql)
db.commit()
insertData()
def insertData():
with sqlite3.connect("ATM.db") as db:
cursor = db.cursor()
sql = """INSERT INTO Atm(Title, First, Surname, Balance) VALUES ('Mr.', 'Jeremy', 'Clarkson', 172.16),
('Miss', 'Suzanne', 'Perry', 15.62)"""
cursor.execute(sql)
db.commit()
db_name = "ATM.db"
sql = """ create table Atm
(CustomerID integer,
Title text,
First text,
Surname text,
Balance real,
primary key(CustomerID))"""
create_table(db_name, "Atm", sql)
sqlite3.OperationalError:没有这样的表:Atm当您对重新创建表说“是”时,您会删除它,但在调用
insertData
之前,决不会尝试重新创建该表。。。(如果不保留表格,您只需在内的最末端执行一次)。现在看来,您可以在删除表后直接添加cursor.execute(sql)
,但最好还是重新考虑一下您的整个逻辑……当然,是的。谢谢如果我在“yes”部分中创建了一个额外的函数来创建表,然后转到insertData,那么它应该可以工作。。非常感谢。