CentOS下安装
sudo yum install MySQL-Python
可以参考http://www.mikusa.com/python-mysql-docs/index.html 获取更多信息
MySQL-python 为Python提供MySQL驱动程序,主要包括两个部件,_mysql和MySQLdb
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
Python下使用MySQLdb模块 http://www.linuxidc.com/Linux/2012-06/63620.htm
1.连接数据库
In [56]: import MySQLdb
In [57]: db=MySQLdb.connect(host='127.0.0.1',user='xxx',passwd='xxx',db='xxx')
2.创建游标
为了能够在多处使用同一个连接,可以创建一个游标对象
In [60]: cur=db.cursor()
3.执行MySQL查询操作
创建数据库表
In [62]: cur.execute("CREATE TABLE song (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,title TEXT NOT NULL)")
In [67]: songs=('Purple Haze','All Along the Watch Tower','Foxy Lady')
In [68]: for song in songs:
....: cur.execute("INSERT INTO song(title) VALUES (%s)",song)
....: print "Auto Increment ID: %s" %cur.lastrowid
....:
Python 的详细介绍:请点这里
Python 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104692.htm