我无法弄清楚问题是什么。我的 db_cursor 函数/变量似乎是所有问题的一部分,因为它出现在大多数错误中。
如果代码运行正常,它应该在我输入“S”后再次询问我的选择。然而,事实并非如此。相反,它会抛出一个错误。
我的代码:
import sqlite3
db_name = "movies.db"
def connect_to_db():
db_conn = sqlite3.connect(db_name)
db_cursor = db_conn.cursor()
print("Connected to DB.")
return db_conn, db_cursor
def create_table(db_cursor):
sql = "create table movies (id integer, title text, director text, year integer, genre text, price real"
db_cursor.execute(sql)
print("Table created.")
(Skipping some lines of code that are not relevant to the question since they don't do anything yet.)
def display_menu(db_conn, db_cursor):
while True:
print("Menu:")
print("Enter S to get started and create a new database")
print("Enter C to create a new row")
print("Enter R to retrieve data")
print("Enter U to update a row")
print("Enter D to delete a row")
print("Enter Q to quit the program")
choice = input("Enter your choice: ").upper()
if choice == "S":
drop_table(db_cursor)
create_table(db_cursor)
elif choice == "C":
insert_row(db_cursor)
elif choice == "R":
select_row(db_cursor)
elif choice == "U":
update_row(db_cursor)
elif choice == "D":
delete_row(db_cursor)
elif choice == "Q":
print("Goodbye")
break
else:
print("Invalid choice of", choice, ".")
db_conn.commit()
select_all(db_cursor)
def main():
db_conn, db_cursor = connect_to_db()
display_menu(db_conn, db_cursor)
db_conn.close()
main()
错误信息:
Traceback (most recent call last):
File "C:\AppData\Roaming\JetBrains\PyCharmCE2022.3\scratches\dtbprogram.py", line 89, in <module>
main()
File "C:\AppData\Roaming\JetBrains\PyCharmCE2022.3\scratches\dtbprogram.py", line 86, in main
display_menu(db_conn, db_cursor)
File "C:\AppData\Roaming\JetBrains\PyCharmCE2022.3\scratches\dtbprogram.py", line 66, in display_menu
create_table(db_cursor)
File "C:\AppData\Roaming\JetBrains\PyCharmCE2022.3\scratches\dtbprogram.py", line 13, in create_table
db_cursor.execute(sql)
sqlite3.OperationalError: incomplete input
** 注意用户信息出于隐私原因被删除,尽管它存在于 ide 中。
sql = "create table movies (id integer, title text, director text, year integer, genre text, price real"
这个 SQL 语句有一个开始
(
但没有结束)
.