Tuesday, November 16, 2010

Python My SQL Access - Part II

the following code will help you to connect and fetch records from the db

def connection():
#open a connection if it does not have one already
return MySQLdb.connect(host='localhost', user='root' , passwd='xxxx', db='MyFirst')

def insert_test_records(rcount=10):

db = connection()
cursor = db.cursor()
for i in range(0,rcount):
cursor.execute("insert into PythonTest(col1)values('"+str(i) +"')")

print get_record_count()

def get_record_count():
db = connection()
cursor = db.cursor()
cursor.execute("select * from PythonTest")
numrows = int(cursor.rowcount)
return numrows

def main():
insert_test_records()

the methods are almost self explantory...more scripts coming up...cheers

No comments:

Post a Comment