将变量设置为stringvar.get()

时间:2015-03-22 23:56:26

标签: python mysql python-3.4

我遇到问题我有一个代码可以生成一个选择查询:

loginquery = "SELECT * from User "
loginquery = loginquery + "WHERE UserName="+ loginVar.get() +" AND Password="+ passVar.get()+""

问题是结果是 SELECT *来自用户WHERE UserName = AND Password =

我该如何解决?

EDIT :::::

这是我的代码

loginquery = ("SELECT * from User "
              "WHERE UserName=%s AND Password=%s")
bobby = (loginVar.get(),passVar.get())

我做了

(query.execute(loginquery % bobby)

我的打印结果是:

  

SELECT * from User WHERE UserName = usernameentered AND Password = passwordentered

然而,mysql连接器出现了这个错误:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND Password=' at line 1

为什么?

1 个答案:

答案 0 :(得分:0)

您应该在字符串文字周围添加引号:

loginquery = ("SELECT * from User "
              "WHERE UserName='%s' AND Password='%s'")