Mysql 单选按钮选择性别。如何成功检索所选性别?

Mysql 单选按钮选择性别。如何成功检索所选性别?,mysql,tkinter,sql-insert,Mysql,Tkinter,Sql Insert,下面的代码是一个函数,用于检索输入并将记录存储到mysql中的表中 def添加记录: cstmid=tcstmid.get1.0检索输入 cstmpass=tcstmpass.get1.0 cstname=tcstname.get1.0 cstmdob=tcstmdob.get1.0 cstmsex=v.get cstmaddress=tcstAddress.get1.0 数据库连接 conn=sqltor.connecthost=localhost,user=root,passwd=iwaki

下面的代码是一个函数,用于检索输入并将记录存储到mysql中的表中

def添加记录: cstmid=tcstmid.get1.0检索输入 cstmpass=tcstmpass.get1.0 cstname=tcstname.get1.0 cstmdob=tcstmdob.get1.0 cstmsex=v.get cstmaddress=tcstAddress.get1.0 数据库连接 conn=sqltor.connecthost=localhost,user=root,passwd=iwakiri,database=APPARELSTORE tkcursor=conn.cursorprepared=True query=插入customercstmid、cstmpass、cstmname、cstmdob、cstmsex、cstmaddress值“%s”、“s”、“s”、“s”、“s” tkcursor.executequery 康涅狄格州 tkcursor.close 以下代码用于制作带有选择性别选项的单选按钮

将tkinter作为tk导入 root=tk.tk root.geometry325x400 树根贮藏 性别标签 lcstmstex=tk.Labelroot,font='helvetica',10,text='选择性别:' lcstmsex.gridrow=9,column=1 单选按钮 v=tk.IntVar v、 set0 性别单选按钮 r1=tk.Radiobuttonroot,text='Female',value=1,variable=v r1.网格行=10,列=1 r2=tk.Radiobuttonroot,text='Male',value=2,variable=v r2.gridrow=11,column=1 r3=tk.Radiobuttonroot,text='Other',value=3,variable=v r3.网格行=12,列=1 错误: mysql.connector.errors.ProgrammingError:1054 42S22:“字段列表”中的未知列“cstmsex” 问题: 单选按钮的代码有什么问题?如何修复它,以便将记录输入tkinter gui并存储在MySQL的表中?

这:

Insert into customer(cstmid,cstmpass,cstmname,cstmdob,cstmsex,cstmaddress) values ...
不是有效的INSERT语句

在表名之后的括号内,必须枚举将接收您提供的值的所有列。 不过,您要做的是传递保存列值的变量

因此,将声明更改为:

query = "Insert into customer(id, pass, name, dob, sex, address) values (?, ?, ?, ?, ?, ?)"
tkcursor.execute(query, (cstmid, cstmpass, cstmname, cstmdob, cstmsex, cstmaddress,))
我用什么?方法中传递的列值的占位符作为元组执行


将列的名称更改为实际名称。

您的错误似乎不是来自tkinter应用程序,而是因为mysql找不到任何名为cstmsex的列,也许您应该验证数据库中的表列名,可能是拼写错误?刚刚注意到,为什么不向查询中的占位符传递任何值?查询可能做错了,您将变量传递到字符串中?问题的标题和问题的主体问了两个完全不同的问题。标题询问如何从单选按钮获取值,但正文询问的是mysql错误。我尝试了您的代码并收到错误:mysql.connector.errors.ProgrammingError:1054 42S22:field list中的未知列“id”如何修复此错误?为什么我们可以使用不存在的列名,如customerid、pass、name、dob、sex、address?@I感谢您在我的回答中的帮助我提到:将列的名称更改为实际名称输入日期,我们使用什么来代替?在cstmid、cstmpass、cstmname、cstmdob、cstmsex、cstmaddress中,我使用了您的代码并得到一个错误:第1行“cstmdob”列的日期值不正确:“2”如果该列的数据类型为date,则必须传递格式为YYYY-MM-DDI的字符串值。我在tkinter中输入了日期2020-11-19,它返回了上述错误。在插入customercstmid、cstmpass、cstmname、cstmdob、cstmsex、cstmaddress值?、?、?、?、?时,是否有任何更改?