Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   MySQL & Python (http://www.programmingforums.org/showthread.php?t=13134)

snipertomcat May 9th, 2007 10:50 AM

MySQL & Python
 
Hi.

I am currently using Python 2.5 & MySQL 5.0 w/ the latest MySQLdb module. I am trying to insert rows into the database via MySQLdb.

What's odd is that using this code I can successfully run a select query, describe tables, and even create tables:

:

import MySQLdb

conn = MySQLdb.connect(host='localhost',user='root',passwd='passwd',db='test')

cursor = conn.cursor()

cursor.execute("select * from test")

result = cursor.fetchall()

for record in result:
    print record[0],record[1]

cursor.close()

conn.close()


For some reason though, I cannot seem to insert or delete rows using MySQLdb. The program won't error out or anything, it exits w/ code 0. I've noticed that the changes seems to take in python because when I run a program to pull the rows I just inserted, it will pull them up. If I open the MySQL command line and manually query the table, the table will return an empty set.

Is there maybe some code I have to run to update the MySQL database?

Any help would be appreciated.

big_k105 May 9th, 2007 12:17 PM

I think the problem could be that after you insert or delete you have to commit the query by running connection.commit().

http://mysql-python.sourceforge.net/...ection-objects

I could be wrong as I have never used MySQLdb but it might be worth a shot


edit: check this out
http://mysql-python.sourceforge.net/...-won-t-go-away
it says that MySQLdb disabled autocommits in version 1.2.0 so you will have to run the connection.commit() before you close the connection or all changes will not be committed to the db.

snipertomcat May 9th, 2007 12:49 PM

Holy Crap dude it worked like a friggin charm, thanks.

I've been through a few tutorials and none of them mentioned the commit()
function. Really appreciate it.


All times are GMT -5. The time now is 12:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC