从tkinter到sqlite3的submit按钮导致无法调用str

2024-10-02 04:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我已经试了这么久,找不到解决这个问题的办法,所以如果你能帮忙,那就太好了,谢谢


Tags: textnameexecutedbsqlgetemailit
2条回答

您没有正确传递变量的值。您调用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)'

这取决于这些值实际上是什么,如果没有看到数据库,我就说不出来。你知道吗

你的问题可能是:

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)

它工作正常。你知道吗

相关问题 更多 >

    热门问题