【问题标题】:My submit button from tkinter to sqlite3 causes str can't be called error我从 tkinter 到 sqlite3 的提交按钮导致 str 不能被称为错误
【发布时间】:2017-10-18 19:34:31
【问题描述】:

我的 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))这行
  • @BryanOakley 的建议是您 edit 您的问题并将实际的 complete 回溯粘贴到那里。它包含的信息不仅仅是发生错误的行。请提供出现错误的所有行(即完整的回溯)。
  • 对数据库访问一无所知,我猜你忘记了sql后面的逗号。试试cursor.execute(sql, (g_name, g_surname, g_dob, g_tutorg, g_email))
  • @JohanL 添加了更多错误:sqlite3.OperationalError: near ",": syntax error 和 @iled 我还是不明白,所以这里是完整的错误代码 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__ return self.func(*args) File "C:\Users\Jimmy\Downloads\tk window (2).py", line 46, in submit cursor.execute(sql (g_name, g_surname, g_dob, g_tutorg, g_email)) TypeError: 'str' object is not callable

标签: python tkinter sqlite


【解决方案1】:

问题可能在你的行中:

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)

它工作正常。

【讨论】:

  • 增加了更多错误:sqlite3.OperationalError: near ",": syntax error 就像我在另一行的 cmets 中发布的一样
【解决方案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)'

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多