0

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!

asked Sep 2, 2014 at 16:32
4
  • Not clear for me. Decimal is the function for conversion: Commented Sep 2, 2014 at 16:41
  • Have you tried to run the same query without Decimal and by placing some test value like 1.01? Commented Sep 2, 2014 at 16:41
  • from decimal import * Then it is possible to convert from string to decimal using Decimal (xxx) Commented Sep 2, 2014 at 16:42
  • 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? Commented Sep 2, 2014 at 16:46

3 Answers 3

1

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]))
answered Sep 2, 2014 at 16:45
Sign up to request clarification or add additional context in comments.

2 Comments

At EXACTLY the same time. That's never happened to me before. I gave you a +1
Great! It works! The more cryptic it gets, the more likely I have a bug somewhere... Thanks to everybody for the support!
1

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.

answered Sep 2, 2014 at 16:45

Comments

0
#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)
answered Oct 30, 2015 at 9:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.