提问者:小点点

如何修复将数据插入数据库mysql python


在数据库中插入数据时遇到问题。

这是密码。 你能修好吗

if (id=="" or Name=="" or Age=="" or Father_Name==""):
    Messagebox.showinfo('info', 'all fields required  You idiot')
else:
    conn= mysql.connect(host="localhost", user="root", password="", database="software")
    cursor=conn.cursor()
    cursor.execute("INSERT INTO information ('id','Name','Age', 'Father Name') VALUES  ('"+ id +"', '"+ Name +"', '"+ Age +"', '"+ Father_Name +"')")
    cursor.execute("commit");

    Messagebox.showinfo("info","Done succesfully");
    conn.close();

共1个答案

匿名用户

问题是您引用了查询中的字段名。 使用背勾而不是引号,或者什么都不用。 在本例中,您需要用到反勾号,因为父名中有空格。

cursor.execute("INSERT INTO information (`id`, `Name`, `Age`, `Father Name`) VALUES  ('"+ id +"', '"+ Name +"', '"+ Age +"', '"+ Father_Name +"')")

重要提示:为了避免SQL注入,不要将用户输入连接到查询SQL中。 像这里一样分别传递数据。

通常,在MySQL中,您应该避免字段名中的空格。