scott
scott

Reputation: 1478

SQL query works in console but not in python

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

Upvotes: 1

Views: 1977

Answers (3)

J B
J B

Reputation: 440

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

Upvotes: 0

scott
scott

Reputation: 1478

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

Upvotes: 4

Anze Jarni
Anze Jarni

Reputation: 1157

Been there :) you need to close the cursor

Upvotes: 1

Related Questions