pycharm 需要帮助找出是什么导致sqlite3.OperationalError:在我的代码中输入不完整[关闭]

kknvjkwl  于 2023-04-20  发布在  PyCharm
关注(0)|答案(1)|浏览(154)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由一个错字或一个无法再复制的问题引起的。虽然类似的问题可能是on-topic在这里,但这个问题的解决方式不太可能帮助未来的读者。
4天前关闭。
Improve this question
我不知道问题出在哪里。我的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中。

ljsrvy3e

ljsrvy3e1#

sql = "create table movies (id integer, title text, director text, year integer, genre text, price real"

这个SQL语句有一个开始(,但没有结束)

相关问题