2016-04-13 7 views
1

私はPythonでmysqlコネクタを使用していますが、私は恥ずかしがり屋を知っていませんが、このクエリはうまくいきません。私は接続が正しく行われていることを知っています。Python Mysql - クエリsintaxのエラー

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構文でエラーが発生しています。ラインに近い「%s」を使用する権利構文についてはMySQLサーバのバージョンに対応するマニュアルを確認してください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() 

答えて

0

cursor.execute(query, (cod_comanda,)) 
+0

ありがとうございました!なぜ説明できますか? – julianomontini

+0

@julianomontini関連スレッドを参照してください:http://stackoverflow.com/questions/24798411/mysql-cursors-execute-with-only-one-parameter-why-is-a-string-sliced-intalal 。希望が役立ちます。 – alecxe