# basic python connect to a mysql database import MySQLdb # connect to db dbconnection = MySQLdb.connect(host="localhost", user="dbuser", passwd="dbpass", db="mydb") dbcursor = dbconnection.cursor() # create query sql = "select * from mytable" # execute query dbcursor.execute(sql) # get query results results = dbcursor.fetchall() # display results if len(results) == 0: print "there were no results." else: print "results:" for item in results: print item # clean up / close connection dbconnection.close()
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Wednesday, June 24, 2009
python - basic database connection example with MySQLdb
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment