Python Mysql - 查询sintax时出错

时间:2016-04-13 19:58:22

标签: python mysql tkinter mysql-python

我在python中使用mysql连接器,我不知道害羞,但这个查询不起作用,我没有ideia为什么!我确信连接已正确完成。

def lista_itens(cod_comanda):
    print (cod_comanda)
    cnx = mysql.connector.connect(user='root', password='123qwe', host='192.168.56.1', database='teste')
    cursor = cnx
    cursor = cnx.cursor()

    query = ("SELECT * FROM itens_comanda WHERE cod_comanda = %s")
    cursor.execute(query, (cod_comanda))
    cnx.close()
    return cursor

错误是: ProgrammingError:1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在'%s'附近使用。在第1行

完整代码:

from Tkinter import *

import mysql.connector
import tkMessageBox

class Tela_principal(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
        self.master.withdraw()
        self.open_login()

    def create_widgets(self):
        self.b_controle_caixa = Button(text = "Controle de caixa",command = lambda : self.abrir_controle_cx())
        self.b_controle_caixa.grid()
        self.b_controle_comanda = Button(text = "Controle de Comanda",command= lambda  : self.abrir_comanda())
        self.b_controle_comanda.grid()

    def abrir_controle_cx(self):
        self.root3 = Toplevel()
        self.app3 = Tela_caixa(self.root3)

    def abrir_comanda(self):
        pass

    def open_login(self):
        self.root2 = Toplevel()
        self.app2 = Tela_login(self.root2)

class Tela_login(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.txt1 = Label(self,text = "Login")
        self.txt2 = Label(self,text = "Senha")
        self.login = Entry(self)
        self.senha = Entry(self)
        self.entrar = Button(self,text="Entrar",command=lambda: self.valida_login())

        self.txt1.grid()
        self.txt2.grid()
        self.login.grid()
        self.senha.grid()
        self.entrar.grid()

    def valida_login(self):
        self.usr_login = self.login.get()
        self.usr_senha = self.senha.get()

        self.cnx = mysql.connector.connect(user='root', password='123qwe', host='192.168.56.1', database='teste')
        self.cursor = self.cnx
        self.cursor = self.cnx.cursor()

        self.query = ("SELECT nome, senha FROM funcionario WHERE nome = %s AND senha = %s")
        self.cursor.execute(self.query, (self.usr_login, self.usr_senha))
        self.row = self.cursor.fetchone()
        self.cnx.close()

        if self.row is None:

            tkMessageBox.showinfo("Erro", "Usuario ou Senha Incorretos")

        else:
            root.deiconify()
            self.master.destroy()

class Tela_caixa(Frame):

    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        scrollbar = Scrollbar(self.master)
        self.txt1 = Label(self,text="Numero Comanda")
        self.comanda = Entry(self)
        self.procura = Button(self,text="Procurar",command = lambda : self.trazer_itens())
        self.txt2 = Label(self,text="Cod.Item / Quan")
        self.item = Entry(self)
        self.quan = Entry(self)
        self.adiciona = Button(self,text="Adicionar")
        self.remover = Button(self,text = "Remover")
        self.lista = Listbox(self,yscrollcommand=scrollbar.set)

        self.txt1.grid()
        self.comanda.grid()
        self.procura.grid()
        self.txt2.grid()
        self.item.grid()
        self.quan.grid()
        self.adiciona.grid()
        self.remover.grid()
        self.lista.grid()

    def trazer_itens(self):
        self.lista.delete(0,(self.lista.size()-1))
        self.itens_trazidos = lista_itens(self.comanda.get())

def lista_itens(cod_comanda):
    print (cod_comanda)
    cnx = mysql.connector.connect(user='root', password='123qwe', host='192.168.56.1', database='teste')
    cursor = cnx
    cursor = cnx.cursor()

    query = ("SELECT * FROM itens_comanda WHERE cod_comanda = %s")
    cursor.execute(query, (cod_comanda))
    cnx.close()
    return cursor


root = Tk()
app = Tela_principal(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您需要在元组中传递查询参数:

cursor.execute(query, (cod_comanda, ))
相关问题