我在数据库中插入数据时遇到问题。
这是代码。你能解决吗
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();
问题是您在查询中引用了字段名称。使用反引号而不是引号,或不使用引号。在这种情况下,您需要反引号,因为您有空格Father Name
。
cursor.execute("INSERT INTO information (`id`, `Name`, `Age`, `Father Name`) VALUES ('"+ id +"', '"+ Name +"', '"+ Age +"', '"+ Father_Name +"')")
重要提示:为避免SQL注入,请勿将用户输入连接到查询SQL中。像这里分别传递数据。
通常,在MySQL中,字段名称中应避免空格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句