Jimmy Lin
Jimmy Lin

Reputation: 1

My submit button from tkinter to sqlite3 causes str can't be called error

my python program isn't working properly and it's something with the submit button and it gives me an error saying:

TypeError: 'str' object is not callable

help please. Here is the part of the code that doesn't work:

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)

I'm not sure what else you need so here's the rest of the SQL part that creates the table

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

I've been trying so long and couldn't find a solution to this problem so if you can help that would be great thanks

Upvotes: 0

Views: 63

Answers (2)

Mogarrr
Mogarrr

Reputation: 383

You're not passing the values for your variables correctly. The way you've called cursor.execute(sql()) makes the interpreter think it's a function.

You need to format the sql string correctly:

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

then use: cursor.execute(sql)

EDIT: or you may need to pass a tuple with data:

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

'data = (g_name, g_surname, g_dob, g_tutorg, g_email) and then use cursor.execute(sql, data)'

It depends on what those values actually are, and without seeing the database, I can't tell.

Upvotes: 1

Pavel Stárek
Pavel Stárek

Reputation: 1039

problem maybe in your line:

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

try change it like this:

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

Edit:

I call SQLite inserts in my simple app with this code:

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

and it works ok.

Upvotes: 1

Related Questions