1

Hi im having issues with a sql query it works perfect in console, but when i implement into python it seems to work perfect no errors but when i check the database it hasnt worked, yet with the console it does work the same no errors yet when i check db the data is there... exact same query i use.

Any ideas?

UPDATE ex SET fbsiteurl = stringvarible, fbsitesource = '' WHERE id = 23123;

in python:

 cur = con.cursor()
 sqlquery = "UPDATE ex SET fbsiteurl = '"+somevarible+"', fbsitesource =        '"+somevarible+"' WHERE id = %d;" % recordid                                
 print sqlquery
 cur.execute(sqlquery)

query shows up fine in print no issues, if i copy the print out and paste it into a mysql console it works perfect everytime, just come python it acts like it works but dosnt really 0_o

1
  • 1
    Please don't add strings together to create your query. That's incredibly dangerous. Read your database library's documentation on "parameterized queries". For instance, that might be written as "sqlquery = 'UPDATE ex SET fbsiteurl = %s, fbsitesource= %s WHERE id = %s'; cur.execute(sqlquery, (somevariable, anothervariable, recordid))". This is critically important! Commented Jul 28, 2012 at 23:57

3 Answers 3

4

connection.autocommit(), or you need to do connection.commit()

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me as well, why do you need to do this?
1

Been there :) you need to close the cursor

1 Comment

closing the curser didnt help, how very strange this all is.
0

This little gotcha continues to this day. Just to clarify, I had to use both of the above answers, as in:

        cur = self.db.cursor()
        try:
            cur.execute(sqlcommand)
            self.db.commit()
            res = cur.fetchall()
        except res is not None:
            print(res)
        finally:
            cur.close()

Comments

Your Answer

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.