EDIT: Solved. Added db.commit() and db.close() to the code below
I'm looking for recommendations for the interactions between my python scripts and functions and the mysql data base.
I figured the MySQLdb library was a pretty good place to start. I can read tables just fine. Inserting seems to work fine in that particular MYSQL session, but I'm unable to make persistent changes.
Code example below, using the simplest possible example I could think of, a two column table of type varchar(20).
import MySQLdb db = MySQLdb.connect(host="mysql.server", user="jansimple", passwd="******", db="jansimple$lekegrind") cur = db.cursor() cur.execute("insert into test values( 'Another PYTHON', 'MySQLdb insert')") cur.execute("SELECT * FROM test") rows = cur.fetchall() print rows # Must commit changes to make them stick. db.commit() # Good form to close the database db.close()
The last print command shows my table as I expect it to be after the successful insert command. However, as that session terminates those changes evaporate...
Do I need to run a magic mysql command to make those changes permanent?
Should I use another python library? (I figured mySQLdb was probably the right place to start, going really simple and learn along the way...)