Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 9th, 2007, 10:50 AM   #1
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
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.
__________________
if (u=an_asshole) then GOTO (hell)
snipertomcat is offline   Reply With Quote
Old May 9th, 2007, 12:17 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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.
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old May 9th, 2007, 12:49 PM   #3
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
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.
__________________
if (u=an_asshole) then GOTO (hell)
snipertomcat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python With MySQL Sane Python 7 Aug 25th, 2006 3:53 PM
MySQL with python? snipertomcat Python 2 Apr 13th, 2006 1:34 AM
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 11:35 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 12:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:29 PM.

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