python - 在 python 中将变量值传递给 SQL 时出现多个参数错误

标签 python mysql tkinter tkinter-entry

当我试图将 python tkinter 中分配的变量值传递到我设置的 SQL 数据库时,我成功地难住了自己,这是我使用的代码:

        new_db, c = SetUpDataBase()
        FirstName = entry1.get()
        LastName =entry2.get()
        DoB =entry3.get()
        Gender =entry4.get()
        MobileNo =entry5.get()
        HomeNo =entry6.get()
        Email =entry7.get()

然后是执行语句:

       c.executescript('''INSERT INTO Patients (Last_Name, First_Name, Date_of_Birth, Gender,
                        Home_Phone_Number, Mobile_Phone_Number, Email) VALUES(?,?,?,?,?,?,?) ''',(LastName,FirstName,DoB,Gender,HomeNo,MobileNo,Email))

这导致了错误消息:

Home_Phone_Number, Mobile_Phone_Number, Email) VALUES(?,?,?,?,?,?,?) ''',(LastName,FirstName,DoB,Gender,HomeNo,MobileNo,Email))

类型错误:函数仅接受 1 个参数(给定 2 个参数)

以及如何解决这个问题的想法?

谢谢

最佳答案

如果您查看executescript 的文档,您会发现它只有一个参数:要运行的脚本。您向其传递两个参数:要运行的脚本和元组。 executescript 不像其他一些数据库方法那样采用参数替换。

解决方案是使用 execute 而不是 executescript

关于python - 在 python 中将变量值传递给 SQL 时出现多个参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55223440/

相关文章:

python - 为 RGB 图片校正 tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) 中的暗淡参数

python - random.randint 和 randrange 有什么区别?

php - 如何从 WordPress 数据库中选择帖子?

python - 需要帮助的一个简单的python错误

python - 如何在运行循环/函数时阻止 GUI 卡住?

python - Pandas 错误 : “pandas. libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477)

python - 使用 Python 的 matplotlib 在 x 轴上绘制时间

java - 如何修复错误

MySQL 从多个表中选择和最近的记录

python - 尝试修复tkinter GUI卡住(使用线程)