0
votes

I would need your help with updating data in my mysqldb through python. Everything works fine including reading, inserting, etc. The following query does not work...

cursor.execute("UPDATE einzel.check SET Kursbuch = %s WHERE analysen.Nummer = %s" (Decimal(kurs),i[8]))

I tried with several optiosn for kurs and i[8], always I get below message:

cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s" (Decimal(kurs),i[12])) TypeError: 'str' object is not callable

str(i[8]) does not work either.

"kurs" is a decimal. In the program I used it with Decimal(kurs) I could calculate without problems. The value to be written in the database "Kurs bei empf" has the format decimal(10,2)

i[12] is a part of an entry of a database which I fetched before. in the database the format is int(11)

Thanks in advance for your help!

Grüße!

3
Not clear for me. Decimal is the function for conversion: - Gigi Hofleitner
Have you tried to run the same query without Decimal and by placing some test value like 1.01? - Vor
from decimal import * Then it is possible to convert from string to decimal using Decimal (xxx) - Gigi Hofleitner
good point! Now I tried cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s" (1.01, 178317)) it does not work either. but now I see the problem, I think it is the spaces in 'Kurs bei empf'. could that be the case? - Gigi Hofleitner

3 Answers

1
votes

try this instead

cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s"% (Decimal(kurs),i[12]))

you were missing the extra % that you need for string formatting however it is recommended to use mysql formats

cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s", (Decimal(kurs),i[12]))
1
votes

The error is that you're missing a comma between the UPDATE statement and the tuple. Please change the code to:

cursor.execute("""UPDATE einzelanalyse.analysen 
                     SET Kurs bei empf = %s 
                   WHERE analysen.Nummer = %s""", (Decimal(kurs),i[12]))
                                              # ^ this is where the comma is necessary

What has previously happened is that you had a string, e.g. "abc" and then parens/brackets after it, e.g. "abc" (...) which looks like you're trying to call a string. As such the error TypeError: 'str' object is not callable makes sense.

0
votes
#Create strings for values
empf = Decimal(kurs)
analysen = i[12]

#python notation for placeholders should be noted as {0} {1} etc. instead of %s 
#use .format(string,string) to point back to {0} and {1}
# Create a query string
query = "update einzelanalyse.analysen set kurs bei empf = {0} where  analysen.nummer {1}".format(empf,analysen)

# Let the cursor just run the prebuild query string
cursor.execute(query)