PDA

View Full Version : python & mysql


idalatob
02-04-2008, 11:08 PM
Normally I use PHP & mysql but I thought I would experiment with python. The problem is that I cant seem to get python to apply changes to the database... it seems like it does, but it actually doesnt...


#mysql connection with python
#first step, import MySQLdb.
import MySQLdb
db = MySQLdb.connect(host="localhost",user="root",passwd="idalatob",db="python_test")
def showAll():
cursor = db.cursor()
cursor.execute("SELECT * FROM `names`")
results = cursor.fetchall()
cursor.close()
for value in results:
print "ID- \t" , value[0]
print "Name- \t", value[1]
def addRow(name_input):
cursor = db.cursor()
cursor.execute("""INSERT INTO `names` (`name`) VALUES (%s)""", (name_input,))
print cursor.execute

def clearTable():
cursor = db.cursor()
cursor.execute("""TRUNCATE TABLE `names`""")
cursor.close()

if you call addRow('Harry') and then showAll() it shows 'Harry' but as soon as you close the program it just dissapears.

Fumigator
02-05-2008, 07:49 PM
If I had to guess (and it's truly a guess, since I've never worked with Python), I'd say Python functions in a transaction-type mode, and the work being done within the transaction is getting rolled back rather than committed.