设为首页 加入收藏

TOP

Python学习之MySQLdb模块
2014-11-23 22:06:54 来源: 作者: 【 】 浏览:9
Tags:Python 习之 MySQLdb 模块

sudo yum install MySQL-python


可以参考http://www.mikusa.com/python-mysql-docs/index.html 获取更多信息


MySQL-python 为Python提供MySQL驱动程序,主要包括两个部件,_mysql和MySQLdb


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


....:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇AttributeError: 'module'.. 下一篇Python学习之socket模块

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: