使用连接器时MySQL连接器语法错误python

问题描述

因此,我使用MysqL来存储数据,从而在python中制作了密码管理器。当我尝试使用MysqL连接器创建表时,出现以下错误

您的sql语法有误;检查与您的MysqL服务器版本相对应的手册以获取正确的语法,以在第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,relwidth = 0.24)
    pwd = tk.Label(top1,text = "enter password: ")
    pwd.place(relx = 0.08,rely = 0.18,relwidth = 0.24)
    pwde = tk.Entry(top1,textvariable = pss)
    pwde.place(relx = 0.3,relwidth = 0.24)
    hnt = tk.Label(top1,text = "hint: ")
    hnt.place(relx = 0.15,rely = 0.33,relwidth = 0.24)
    hnte = tk.Entry(top1,bg = 'gray')
    hnte.place(relx = 0.3,relwidth = 0.24)
    createacc = tk.Button(top1,text = 'Create Account',font = 50,command = newacc)
    createacc.place(relx = 0.1,rely = 0.42,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,relwidth = 0.2)
create = tk.Button(root,text = 'Create',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,relwidth = 0.3)
usrent = tk.Entry(root,bg = 'gray')
usrent.place(relx = 0.37,relheight = 0.045,relwidth = 0.24)
psent = tk.Entry(root,bg = 'gray')
psent.place(relx = 0.37,relwidth = 0.24)
sett = tk.Button(root,image = settt,command = settings)
sett.place(relx = 0.9,relheight = 0.05,relwidth = 0.05)
login = tk.Button(root,text = 'Log in',font = 50)
login.place(relx = 0.2,rely = 0.7,relwidth = 0.48)

root.mainloop()

问题所在:

    def newacc():
        check = str("DROP TABLE IF EXISTS" + str(oo.get()))
        table = "CREATE TABLE {0}(SERVICE VARCHAR(100),PASSWORD VARCHAR(256))".format(str(oo.get()))
        cursor.execute(table)
        msql.close()

'''

能帮忙吗

解决方法

check = str("DROP TABLE IF EXISTS" + str(oo.get())) EXISTS关键字后必须有一个空格。

,

这里是我对发生问题的猜测

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

请尝试一下,让我知道是否有错误。

欢呼