Ubuntu python install mysql

python安裝mysql資料庫驅動器

sudo pip install mysql-python

如果出現_mysql.c:29:20: fatal error: Python.h的錯誤訊息,
可查詢是否有安裝python-dev:
sudo dpkg -l | grep python-dev
如果沒有,請安裝:
sudo apt-get install python-dev

MySQLdb的輸入宣告
#!/usr/bin/python
import MySQLdb
import MySQLdb.cursors


db 的連結:
db = MySQLdb.connect("localhost","db_name","db_password","db_usert",compress=1,
    cursorclass=MySQLdb.cursors.DictCursor )


db 的查詢:
# prepare a cursor object using cursor() method
cursor = db.cursor()


sql = "SELECT * FROM user WHERE del_mark = 0 order by id desc "
# execute SQL query using execute() method.

cursor.execute(sql)

rows = cursor.fetchall()
for row in rows: 
    print 'user name : ' + row['name']

# disconnect from server
db.close()

留言

熱門文章