我的提交按钮从 tkinter 到 sqlite3 导致 str 不能被称为错误



我的 Python 程序无法正常工作,它是带有提交按钮的东西,它给了我一个错误,说:

TypeError: 'str' object is not callable

请帮忙。以下是代码中不起作用的部分:

def submit():
    g_name = ent0.get()
    g_surname = ent1.get()
    g_dob = ent2.get()
    g_tutorg = ent3.get() #Gets all the entry boxes
    g_email = ent4.get()
    cursor = db.cursor()
    sql = '''INSERT into Students, (g_name, g_surname, g_dob, g_tutorg, g_email) VALUES (?,?,?,?,?)'''
    cursor.execute(sql (g_name, g_surname, g_dob, g_tutorg, g_email))
    #Puts it all on to SQL
    db.commit()
    mlabe2=Label(mGui,text="Form submitted, press exit to exit").place(x=90,y=0)

我不确定您还需要什么,所以这是创建表的SQL部分的其余部分

cursor = db.cursor()
cursor.execute("""
    CREATE TABLE IF NOT EXISTS Students(
        StudentID integer,
        Name text,
        Surname text,
        DOB blob,
        Tutor_Grop blob,
        Email blob,
        Primary Key(StudentID));
    """) #Will create if it doesn't exist
db.commit()

我已经尝试了很长时间,但找不到解决此问题的方法,所以如果您能提供帮助,那就太好了,谢谢

问题可能在你的行中:

cursor.execute(sql (g_name, g_surname, g_dob, g_tutorg, g_email))

尝试像这样更改它:

cursor.execute(sql, (g_name, g_surname, g_dob, g_tutorg, g_email))

编辑:

我使用以下代码在我的简单应用程序中调用 SQLite 插入:

data = (None, spath, sfile, sfilename, sha256hash, )
cur.execute("INSERT INTO filesoid VALUES (?, ?, ?, ?, ?)", data)

它工作正常。

您没有正确传递变量的值。你称呼cursor.execute(sql())的方式让解释器认为这是一个函数。

您需要正确设置sql字符串的格式:

sql = '''INSERT into Students, ({}, {}, {}, {}, {}) VALUES (?,?,?,?,?)'''.format(g_name, g_surname, g_dob, g_tutorg, g_email)

然后使用: cursor.execute(sql)

编辑:或者,您可能需要传递包含数据的元组:

sql = '''INSERT into Students VALUES (?,?,?,?,?)'''

'data = (g_name, g_surname, g_dob, g_tutorg, g_email),然后使用 cursor.execute(sql, data)'

这取决于这些值的实际是什么,如果不看到数据库,我就无法判断。

相关内容

最新更新