Mysql 使用连接器时出现连接器语法错误 python
Mysql connector syntax error python while using connector
所以我在 python 中制作了一个密码管理器,使用 mysql 来存储数据。当我尝试使用 mysql 连接器创建 table 时,出现以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
我的完整代码是
import mysql.connector as mysql
import tkinter as tk
from simplecrypt import *
def createusr():
global oo
global pss
top1 = tk.Toplevel(root)
top1.title('Create new user ')
top1.geometry('480x360')
oo = tk.StringVar()
pss = tk.StringVar()
userid = tk.Label(top1, text = 'Enter User ID: ')
userid.place(relx = 0.09, rely = 0.1, relheight = 0.058, relwidth = 0.24)
entrusr = tk.Entry(top1, bg = 'gray', textvariable = id)
entrusr.place(relx = 0.3, rely = 0.1, relheight = 0.058, relwidth = 0.24)
pwd = tk.Label(top1, text = "enter password: ")
pwd.place(relx = 0.08, rely = 0.18, relheight = 0.058, relwidth = 0.24)
pwde = tk.Entry(top1, bg = 'gray', textvariable = pss)
pwde.place(relx = 0.3, rely = 0.18,relheight = 0.058, relwidth = 0.24)
hnt = tk.Label(top1, text = "hint: ")
hnt.place(relx = 0.15, rely = 0.33, relheight = 0.058, relwidth = 0.24)
hnte = tk.Entry(top1, bg = 'gray')
hnte.place(relx = 0.3, rely = 0.33, relheight = 0.058, relwidth = 0.24)
createacc = tk.Button(top1, text = 'Create Account', font = 50, command = newacc)
createacc.place(relx = 0.1, rely = 0.42, relheight = 0.058, relwidth = 0.24)
def settings():
top3 = tk.Toplevel(root)
top3.title('Settings')
top3.geometry('480x360')
url = tk.IntVar()
#darkt = tk.IntVar()
#lightt = tk.IntVar()
ourl = tk.Checkbutton(top3, text = "open URL of service instantly", variable = url)
ourl.pack()
#theme = tk.Label(top3, text = 'Theme:')
#theme.pack()
#dark = tk.Checkbutton(top3, text = 'Dark', variable = darkt, command = reset(f = True))
#dark.pack()
#light = tk.Checkbutton(top3, text = 'Light', variable = lightt, command = reset)
#light.pack()
def forgotT():
top2 = tk.Toplevel(root)
top2.title('recover password')
top2.geometry('480x360')
hint = tk.Label(top2)
def newacc():
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
table = "CREATE TABLE {0}(SERVICE VARCHAR(100), USERNAME VARCHAR(100),PASSWORD VARCHAR(256))".format(str(oo.get()))
cursor.execute(table)
msql.close()
root = tk.Tk()
root.title('Cipher')
canvas = tk.Canvas(root, height=480, width=360)
canvas.pack()
msql = mysql.connect(user='root', password='7014403396', database='cipher')
cursor = msql.cursor()
coolimg = tk.PhotoImage(file='images-4.png')
settt = tk.PhotoImage(file='settings+icon-1320183238404656194_16.png')
img = tk.Label(root, image = coolimg)
img.place(relx = 0.3, rely = 0.07, relheight = 0.5, relwidth = 0.4)
Userid = tk.Label(root, text = 'User ID: ', width = 20)
Userid.place(relx = 0.2, rely = 0.6, relheight = 0.04, relwidth = 0.2)
passwd = tk.Label(root, text = 'Password: ', width = 20)
passwd.place(relx = 0.2, rely = 0.65, relheight = 0.04, relwidth = 0.2)
create = tk.Button(root, text = 'Create', font = 50, command = createusr)
create.place(relx = 0, rely = 0.9, relwidth = 0.3)
forgot = tk.Button(root, text = 'Forgot?', font = 50)
forgot.place(relx = 0.72 ,rely = 0.9, relwidth = 0.3)
usrent = tk.Entry(root, bg = 'gray')
usrent.place(relx = 0.37, rely = 0.6, relheight = 0.045, relwidth = 0.24)
psent = tk.Entry(root, bg = 'gray')
psent.place(relx = 0.37, rely = 0.65, relheight = 0.045, relwidth = 0.24)
sett = tk.Button(root, image = settt, command = settings)
sett.place(relx = 0.9, rely = 0.07, relheight = 0.05, relwidth = 0.05)
login = tk.Button(root, text = 'Log in', font = 50)
login.place(relx = 0.2, rely = 0.7, relheight = 0.05, relwidth = 0.48)
root.mainloop()
问题区域是:
def newacc():
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
table = "CREATE TABLE {0}(SERVICE VARCHAR(100), USERNAME VARCHAR(100),PASSWORD VARCHAR(256))".format(str(oo.get()))
cursor.execute(table)
msql.close()
'''
你能帮忙吗
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
EXISTS关键字后需要有space
这是我对问题出在哪里的猜测
def newacc():
check = "DROP TABLE IF EXISTS {}".format(oo.get())
table = '''CREATE TABLE `{}`(
`SERVICE` VARCHAR(100),
`USERNAME` VARCHAR(100),
`PASSWORD` VARCHAR(256))'''.format(oo.get())
cursor.execute(table)
msql.close()
请尝试一下,如果有任何错误,请告诉我。
干杯
所以我在 python 中制作了一个密码管理器,使用 mysql 来存储数据。当我尝试使用 mysql 连接器创建 table 时,出现以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
我的完整代码是
import mysql.connector as mysql
import tkinter as tk
from simplecrypt import *
def createusr():
global oo
global pss
top1 = tk.Toplevel(root)
top1.title('Create new user ')
top1.geometry('480x360')
oo = tk.StringVar()
pss = tk.StringVar()
userid = tk.Label(top1, text = 'Enter User ID: ')
userid.place(relx = 0.09, rely = 0.1, relheight = 0.058, relwidth = 0.24)
entrusr = tk.Entry(top1, bg = 'gray', textvariable = id)
entrusr.place(relx = 0.3, rely = 0.1, relheight = 0.058, relwidth = 0.24)
pwd = tk.Label(top1, text = "enter password: ")
pwd.place(relx = 0.08, rely = 0.18, relheight = 0.058, relwidth = 0.24)
pwde = tk.Entry(top1, bg = 'gray', textvariable = pss)
pwde.place(relx = 0.3, rely = 0.18,relheight = 0.058, relwidth = 0.24)
hnt = tk.Label(top1, text = "hint: ")
hnt.place(relx = 0.15, rely = 0.33, relheight = 0.058, relwidth = 0.24)
hnte = tk.Entry(top1, bg = 'gray')
hnte.place(relx = 0.3, rely = 0.33, relheight = 0.058, relwidth = 0.24)
createacc = tk.Button(top1, text = 'Create Account', font = 50, command = newacc)
createacc.place(relx = 0.1, rely = 0.42, relheight = 0.058, relwidth = 0.24)
def settings():
top3 = tk.Toplevel(root)
top3.title('Settings')
top3.geometry('480x360')
url = tk.IntVar()
#darkt = tk.IntVar()
#lightt = tk.IntVar()
ourl = tk.Checkbutton(top3, text = "open URL of service instantly", variable = url)
ourl.pack()
#theme = tk.Label(top3, text = 'Theme:')
#theme.pack()
#dark = tk.Checkbutton(top3, text = 'Dark', variable = darkt, command = reset(f = True))
#dark.pack()
#light = tk.Checkbutton(top3, text = 'Light', variable = lightt, command = reset)
#light.pack()
def forgotT():
top2 = tk.Toplevel(root)
top2.title('recover password')
top2.geometry('480x360')
hint = tk.Label(top2)
def newacc():
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
table = "CREATE TABLE {0}(SERVICE VARCHAR(100), USERNAME VARCHAR(100),PASSWORD VARCHAR(256))".format(str(oo.get()))
cursor.execute(table)
msql.close()
root = tk.Tk()
root.title('Cipher')
canvas = tk.Canvas(root, height=480, width=360)
canvas.pack()
msql = mysql.connect(user='root', password='7014403396', database='cipher')
cursor = msql.cursor()
coolimg = tk.PhotoImage(file='images-4.png')
settt = tk.PhotoImage(file='settings+icon-1320183238404656194_16.png')
img = tk.Label(root, image = coolimg)
img.place(relx = 0.3, rely = 0.07, relheight = 0.5, relwidth = 0.4)
Userid = tk.Label(root, text = 'User ID: ', width = 20)
Userid.place(relx = 0.2, rely = 0.6, relheight = 0.04, relwidth = 0.2)
passwd = tk.Label(root, text = 'Password: ', width = 20)
passwd.place(relx = 0.2, rely = 0.65, relheight = 0.04, relwidth = 0.2)
create = tk.Button(root, text = 'Create', font = 50, command = createusr)
create.place(relx = 0, rely = 0.9, relwidth = 0.3)
forgot = tk.Button(root, text = 'Forgot?', font = 50)
forgot.place(relx = 0.72 ,rely = 0.9, relwidth = 0.3)
usrent = tk.Entry(root, bg = 'gray')
usrent.place(relx = 0.37, rely = 0.6, relheight = 0.045, relwidth = 0.24)
psent = tk.Entry(root, bg = 'gray')
psent.place(relx = 0.37, rely = 0.65, relheight = 0.045, relwidth = 0.24)
sett = tk.Button(root, image = settt, command = settings)
sett.place(relx = 0.9, rely = 0.07, relheight = 0.05, relwidth = 0.05)
login = tk.Button(root, text = 'Log in', font = 50)
login.place(relx = 0.2, rely = 0.7, relheight = 0.05, relwidth = 0.48)
root.mainloop()
问题区域是:
def newacc():
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
table = "CREATE TABLE {0}(SERVICE VARCHAR(100), USERNAME VARCHAR(100),PASSWORD VARCHAR(256))".format(str(oo.get()))
cursor.execute(table)
msql.close()
'''
你能帮忙吗
check = str("DROP TABLE IF EXISTS" + str(oo.get()))
EXISTS关键字后需要有space
这是我对问题出在哪里的猜测
def newacc():
check = "DROP TABLE IF EXISTS {}".format(oo.get())
table = '''CREATE TABLE `{}`(
`SERVICE` VARCHAR(100),
`USERNAME` VARCHAR(100),
`PASSWORD` VARCHAR(256))'''.format(oo.get())
cursor.execute(table)
msql.close()
请尝试一下,如果有任何错误,请告诉我。
干杯