gpt4 book ai didi

Python Mysql - 查询语法错误

转载 作者:行者123 更新时间:2023-11-30 22:21:05 25 4
gpt4 key购买 nike

我在 python 中使用 mysql 连接器,我不知道 shy ,但是这个查询不起作用,我不知道为什么!我确定连接已正确完成。

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 服务器版本对应的手册,了解在第 1 行的“%s”附近使用的正确语法

完整代码:

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()

最佳答案

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

cursor.execute(query, (cod_comanda, ))

关于Python Mysql - 查询语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36608365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com