1,下载源码包
wget 'http://downloads.sourceforge.net/project/mysql-python/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz?use_mirror=nchc'
2,解压安装 (注意README,养成查看README的习惯)
tar -zxvf MySQL-python-1.2.3c1.tar.gz
cd MySQL-python-1.2.3c1
more setup.py
python setup.py install
3,验证是否安装成功。
a,查看 /usr/lib64/python2.6/site-packages下是否有内容,如有内容则ok
b,从python shell中import MySQLdb,如不报错则ok
常见使用:
# -*- coding: utf-8 -*-
#mysqldb
import time, MySQLdb
#连接
conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8")
cursor = conn.cursor()
#写入
sql = "insert into user(name,created) values(%s,%s)"
param = ("aaa",int(time.time()))
n = cursor.execute(sql,param)
print n
#更新
sql = "update user set name=%s where id=3"
param = ("bbb")
n = cursor.execute(sql,param)
print n
#查询
n = cursor.execute("select * from user")
for row in cursor.fetchall():
for r in row:
print r
#删除
sql = "delete from user where name=%s"
param =("aaa")
n = cursor.execute(sql,param)
print n
cursor.close()
#关闭
conn.close()