简体   繁体   English

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

[英]Mysql connector syntax error python while using connector

So im making a password manager in python, using mysql to store data.所以我在 python 中制作了一个密码管理器,使用 mysql 来存储数据。 when i try to use mysql connector to create table, i get the following error:当我尝试使用 mysql 连接器创建表时,出现以下错误:

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 '' 附近使用的正确语法

my complete code is我的完整代码是

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

the area of problem is:问题领域是:

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

''' '''

can you please help你能帮忙吗

check = str("DROP TABLE IF EXISTS" + str(oo.get())) There needs to be a space after the EXISTS keyword. check = str("DROP TABLE IF EXISTS" + str(oo.get())) EXISTS 关键字后面需要有一个空格。

Heres my guesses as to what went wrong这是我对出了什么问题的猜测

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

Do give it a try and let me know if any errors.请尝试一下,如果有任何错误,请告诉我。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM