Home > Blockchain >  Why is Python giving me a " TypeError: 'str' object is not callable"?
Why is Python giving me a " TypeError: 'str' object is not callable"?

Time:05-04

I am brand new to coding, I am stumped why the line: my_cursor.execute(a, b) gives me a "TypeError: 'str' object is not callable" at run time. And, no I have not used the "STR" keyword anywhere in my program. Thanks for any help.

def save_to_db():
mydb=mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="stocks")
a = "INSERT INTO portfolio_table (Row, Name, Ticker, Shares, Ex-Date, Pay Date, Price, Value, 
Dividend, Income, Notes) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
b=(rowEntry.get(),nameEntry.get(),tickerTextbox.get(),shareTextbox.get(),exDateTextbox.get(),
    payDateTextbox.get(),priceTextbox.get(),totalValue,divTextbox.get(),
totalIncome, notesTextbox.get())
my_cursor.execute(a, b)
mydb.commit()
mydb.close()

CodePudding user response:

Larsks, Thank you. Here is the code where I setup a MYSQL connection (it actually works!) and defined/initalized "my_cursor".

 mydb=mysql.connector.connect(
 host="localhost",
 user="root",
 passwd="",
 database="stocks")
 print(mydb)
 #create a cursor
 my_cursor=mydb.cursor()
 my_cursor.execute("CREATE DATABASE IF NOT EXISTS stocks")

CodePudding user response:

Ry and furas, Thank you. Here is the full error copied from eclipse.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in__call__
return self.func(*args)
File "C:\Users\rand\eclipse-workspace\PYTHON\FirstGui\stockstracker.py", line 
98, in save_to_db
my_cursor.execute(a, b)
TypeError: 'str' object is not callable
  • Related