tkinter使用MySQL存数据_我无法从tkinter表单向mysql插入数据
我能很好地連接到我的數據庫,總線,不知怎么的,我把它的書寫部分搞砸了。
這是我的代碼:
from tkinter import *
from tkinter import messagebox
import mysql.connector
from mysql.connector import MySQLConnection, Error
db = mysql.connector.connect(
host="localhost",
user="root",
passwd="oliver1989",
database="pruebas_mysql"
)
def insert_newuser():
cursor = db.cursor()
add_usuario = """INSERT INTO reg_us(nombre, paterno, materno, usuario, contra)"
"VALUES(%s, %s, %s, %s, %s)"""% (new_nombre.get(), new_paterno.get(), new_materno.get(), new_usuario.get(), new_contrase?±a.get())
#data_registro = (new_nombre.get(), new_paterno.get(), new_materno.get(), new_usuario.get(), new_contrase?±a.get())
cursor.execute(add_usuario)
db.commit()
def registro_usuario():
screen1=Toplevel(screen)
screen1.geometry("280x400")
screen1.config(bg="#5EC659")
screen.title("Registro")
global new_nombre
new_nombre=StringVar()
global new_paterno
new_paterno=StringVar()
global new_materno
new_materno=StringVar()
global new_usuario
new_usuario=StringVar()
global new_contrase?±a
new_contrase?±a=StringVar()
global con_contrase?±a
con_contrase?±a=StringVar()
Label(screen1, text="Por favor llena este formulario", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Label(screen1, text="", bg="#5EC659").pack()
Label(screen1, text="Nombre / s", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=new_nombre).pack()
Label(screen1, text="Apellido paterno", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=new_paterno).pack()
Label(screen1, text="Apellido Materno", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=new_materno).pack()
Label(screen1, text="Usuario", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=new_usuario).pack()
Label(screen1, text="Contrase?±a", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=new_contrase?±a).pack()
Label(screen1, text="Confirma la contrase?±a", font=("consolas", 10), bg="#5EC659", fg="white").pack()
Entry(screen1, textvariable=con_contrase?±a).pack()
Label(screen1, text="", bg="#5EC659").pack()
Button(screen1, text=" Registrar", height="1", width="10", font=("consolas", 11), command=insert_newuser).pack()
def check_udb():
print("hola")
def main_screen():
global screen
screen = Tk()
screen.geometry("280x300")
screen.config(bg="#5EC659")
screen.title("Planeaciones 1.0")
global usuario
global contrase?±a
usuario = StringVar()
contrase?±a =StringVar()
Label(text="Bienvenido", font=("consolas", 15), bg="#5EC659", fg="white").pack()
Label(text="", bg="#5EC659").pack()
Label(text="Usuario", bg="#5EC659", fg="white", font=("consolas", 13)).pack()
Entry(screen, textvariable = usuario).pack()
Label(text="Contrase?±a", bg="#5EC659", fg="white", font=("consolas", 13)).pack()
Entry(screen, textvariable = contrase?±a).pack()
Label(text="", bg="#5EC659").pack()
Button(text="Ingresar", height="1", width="8", font=("consolas", 10), fg="#1E1E1E", command=check_udb).pack()
Label(text="??No cuentas con usuario?", bg="#5EC659", fg="white").pack()
Label(text="??Facil!, registra tu nuevo usuario.", bg="#5EC659", fg="white").pack()
Button(text="Registrarse", height="1", width="11", font=("consolas", 10), fg="#1E1E1E", command=registro_usuario).pack()
screen.mainloop()
main_screen()
這是控制臺中出現的錯誤:
“C:\用戶\ICATVER 03\PycharmProjects\pruebas
mysqldb\venv\Scripts\python.exe“”C:/Users/ICATVER
03/PycharmProjects/pruebas mysqldb/index.py“Tkinter中的異常
回調回溯(最近一次調用最后一次):文件“C:\用戶\ictor
03\AppData\Local\Programs\Python\Python37-32\lib\tkinter_uuinit_uu.py“,
1705號線,輸入
呼叫
插入新用戶
mysqldb\venv\lib\site packages\mysql\connector\cursor.py“,第551行,
執行中
self._handle_result(self._connection.cmd_query(stmt))文件“C:\Users\ICATVER 03\PycharmProjects\pruebas
mysqldb\venv\lib\site packages\mysql\connector\connection.py“,行
490,在命令查詢中
result=self._handle_result(self._send_cmd(ServerCmd.QUERY,QUERY))文件“C:\用戶\ICATVER 03\PycharmProjects\pruebas
mysqldb\venv\lib\site packages\mysql\connector\connection.py“,行
395,在處理結果中
SQL語法錯誤;請查看與
MySQL服務器的正確語法版本使用near“
第1行的“值(asdf、asdf、asdf、asdf、asdf)”
總結
以上是生活随笔為你收集整理的tkinter使用MySQL存数据_我无法从tkinter表单向mysql插入数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python包导入细节_python循环
- 下一篇: vs2008创建mysql数据库_求教V