Reading from a Database : MySQL « Database « Python






Reading from a Database



import MySQLdb

db= MySQLdb.connect(host="localhost", user="python-test", passwd="python",
db="python-test")

cursor = db.cursor()
stmt = "SELECT * from books"
cursor.execute(stmt)

rows = cursor.fetchall ()
for row in rows:
    print "Row: "
    for col in row :
        print "Column: %s" % (col)
    print "End of Row"
print "Number of rows returned: %d" % cursor.rowcount

cursor.close()

db.close()

 








Related examples in the same category

1.Updating a Database
2.Writing to a Database