考虑以下代码:
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")
我现在被困住了,有人可以帮忙吗?
答案 0 :(得分:1)
好的。我将给出答案。您的代码取决于一个没有人可以访问的数据库,因此我们无济于事,但是您得到的错误是:
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
这表示 values = [stu,re1,at] 设置为 ['',``,''] ,这显然是错误的。您需要深入了解 stu , re1 和 at < / em> 设置为空字符串。