python实战学习mysql测试
1、python的mysql在windows下安装比较不爽,如果直接下载源码包安装会出现error: Unable to find vcvarsall.bat错误,搜索到原因是因为python的mysql组件编译需要vs2008环境,不科学啊,即使我本地安装了vs2010设置SET VS90COMNTOOLS=%VS100COMNTOOLS%也不行,会出现“ fatal error C1083: 无法打开包括文件:“config-win.h”” 错误。
还是下载安装版本 MySQL-python-1.2.3.win-amd64-py2.7.exe
http://www.codegood.com/download/11/
直接安装
所有版本下载地址在:http://www.codegood.com/downloads
2、测试连接代码
import MySQLdb try: #connect conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8") cursor = conn.cursor() #add sql = "insert into user(name,age) values(%s,%s)" param = ("tom",str(20)) n = cursor.execute(sql,param) print n #更新 sql = "update user set name=%s where Id=9001" param = ("ken") n = cursor.execute(sql,param) print n #查询 n = cursor.execute("select * from user") for row in cursor.fetchall(): for r in row: print r, print "" #删除 sql = "delete from user where name=%s" param =("ted") n = cursor.execute(sql,param) print n cursor.close() #关闭 conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])
3、运行代码会打印结果以逗号分隔
(0)条评论 订阅