Thursday, May 25, 2017

python mysql connect

sudo apt-get install libmysqlclient-dev
pip install MySQL-python

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","root","p@ssowr9d","store_db")
cursor = db.cursor()
sql = "select * from food_tbl"
try:
    cursor.execute(sql)
    #data = cursor.fetchone()
    data = cursor.fetchall()
    for row in data:
        id = row[0]
        name = row[1]
        catagory = row[2]
        price = row[3]
        print "%d %s %s %d " % (id, name, catagory, price)
except:
    print "Error: unable to fetch data"

db.close()

No comments:

Post a Comment