pymysql.err.ProgrammingError程序错误运行tkinter+pymysql scrip时

2024-06-10 06:05:37 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑以下代码:

from tkinter import *
import pymysql

def newc():
    def inr1():

        var1 = var.get()
        connection = pymysql.connect(host='localhost', user='root', password='', db='class')
        cursor = connection.cursor()
        q = ("create table " + str(var1) + "(Name text,regno int,attendence text)")
        cursor.execute(q)
        connection.commit()
        connection.close()

    r2.withdraw()
    r3.deiconify()
    cfr=Frame(r3,width=800,height=600).grid(row=0,column=0)
    l3=Label(cfr,text="Enter the name of the class").grid(row=0,column=0)
    var=StringVar()
    ec=Entry(cfr,textvariable=var).grid(row=0,column=1)
    eb=Button(cfr,text="Create",command=inr1).grid(row=1,column=1)
    eb1=Button(cfr,text="quit",command=quit).grid(row=1,column=2)

def insstud():
    def inr():
        classname =sec.get()
        stu=name.get()
        re1=regno.get()
        at=att.get()
        connection = pymysql.connect(host='localhost', user='root', password='', db='class')
        cursor = connection.cursor()
        q = ("insert into "+str(classname)+" values(%s,%s,%s)")
        values=[stu,re1,at]
        cursor.execute(q,values)
        connection.commit()
        connection.close()

    r2.withdraw()
    s1.deiconify()
    sf = Frame(s1, width=800, height=600).grid(row=0, column=0)
    Label(sf,text="Class").grid(row=0,column=0)
    Entry(sf,textvariable=sec).grid(row=0,column=1)
    Label(sf,text="Name").grid(row=1,column=0)
    Entry(sf, textvariable=name).grid(row=1, column=1)
    Label(sf, text="Regno").grid(row=2, column=0)
    Entry(sf, textvariable=regno).grid(row=2, column=1)
    Label(sf, text="Attendance ").grid(row=3, column=0)
    Entry(sf, textvariable=att).grid(row=3, column=1)
    Button(sf,text="Submit",command=inr).grid(row=4,column=3)
def postlogin():
    root.destroy()
    r2.deiconify()
    nf2=Frame(r2,width=800,height=600).grid(row=0,column=0)
    # /*----For Displaying class---------s-*/
    b1=Button(r2,text="Show the details of a class",command=show).grid(row=0,column=0,rowspan=3,columnspan=3)
    # /*----For creating New class----------*/
    b2=Button(r2,text="Create a new class",command=newc).grid(row=0,column=2,rowspan=3,columnspan=3)
    b3=Button(r2,text="Insert a student into a class",command=insstud).grid(row=1,column=0,rowspan=3,columnspan=3)
    b4=Button(r2,text="Exit",command=quit).grid(row=1,column=2,rowspan=3,columnspan=3)


def sel():
    s1.destroy()
    data1 = []
    sec1=sec.get()
    print(sec1)
    connection = pymysql.connect(host='localhost', user='root', password='', db='class')
    cursor = connection.cursor()
    q = ("select * from " + str(sec1))
    cursor.execute(q)
    data = cursor.fetchall()

    for row in data:

        temp = [row[0], row[1], row[2]]
        data1.append(temp)
    cursor.close()
    connection.close()
    r1 = Tk()
    nf = Frame(r1, width=10, height=10).grid(row=0, column=0)
    l1 = Label(nf, text="Name").grid(row=0, column=0, sticky=W)
    l2 = Label(nf, text="Regno").grid(row=0, column=1, sticky=NW)
    l3 = Label(nf, text="Attendance perc").grid(row=0, column=2, sticky=NW)
    nf1 = Frame(r1, width=200, height=200).grid(row=2, column=2)
    t = Text(nf1)
    for x in range(4):
        t.insert(END, data1[x])
        t.insert(END, "\n")

    t.grid(row=2, column=0)


def show():

    r2.withdraw()

    s1.deiconify()
    sf=Frame(s1,width=100,height=100).grid(row=0,column=0)
    l4=Label(sf,text="Section").grid(row=0,column=0)
    se=Entry(sf,textvariable=sec).grid(row=0,column=1,columnspan=4)
    sb=Button(sf,text="Ok",command=sel).grid(row=1,column=1)



def login():
    usr=user.get()
    pas=password.get()
    connection = pymysql.connect(host='localhost', user='root', password='', db='login')
    cursor = connection.cursor()
    q=("select username from user where username=%s")


    q1 = ("select pass from user where pass=%s")
    if cursor.execute(q,usr) and cursor.execute(q1, pas):
        postlogin()
    else:
        print("Try again")

    connection.commit()
    connection.close()
root=Tk()
user=StringVar()
password=StringVar()
sec=StringVar()
name=StringVar()
regno=StringVar()
att=StringVar()
tp=Frame(root,width=800,height=600)
tp.pack()
l1=Label(tp,text="Username")
l1.grid(row=0,column=0)
e1=Entry(tp,textvariable=user).grid(row=0,column=1,columnspan=4)

l2=Label(tp,text="Password")
l2.grid(row=1,column=0)
e2=Entry(tp,textvariable=password).grid(row=1,column=1,columnspan=4)

submit=Button(tp,text="Login",command=login).grid(row=2,column=2)

#r2 is the windows for post login screen
r2 = Tk()
r2.withdraw()
# r3 is the windows for creating a new class
r3=Tk()
r3.withdraw()
s1=Tk()
s1.withdraw()
root.mainloop()

当我运行它时,我会得到这个回溯:

C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/dimon/PycharmProjects/untitled/1.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:/Users/dimon/PycharmProjects/untitled/1.py", line 34, in inr
    cursor.execute(q,values)
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\connections.py", line 893, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\connections.py", line 1103, in _read_query_result
    result.read()
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\connections.py", line 1396, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\connections.py", line 1059, in _read_packet
    packet.check_error()
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\connections.py", line 384, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\dimon\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'values('','','')' at line 1")

我被困在这一点上了,有人能帮忙吗?你知道吗


Tags: textinpylinecolumnsfconnectionusers
1条回答
网友
1楼 · 发布于 2024-06-10 06:05:37

好的。我要试试答案。您的代码依赖于这里没有人可以访问的数据库,因此我们无法提供很多帮助,但您遇到的错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'values('','','')' at line 1

这表明值=[stu,re1,at]被设置为['','','],这显然是不正确的。您需要深入了解为什么或如何将sture1at设置为空字符串。你知道吗

相关问题 更多 >