在数据库中插入数据时遇到问题。
这是密码。 你能修好吗
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();
问题是您引用了查询中的字段名。 使用背勾而不是引号,或者什么都不用。 在本例中,您需要用到反勾号,因为父名
中有空格。
cursor.execute("INSERT INTO information (`id`, `Name`, `Age`, `Father Name`) VALUES ('"+ id +"', '"+ Name +"', '"+ Age +"', '"+ Father_Name +"')")
重要提示:为了避免SQL注入,不要将用户输入连接到查询SQL中。 像这里一样分别传递数据。
通常,在MySQL中,您应该避免字段名中的空格。