[win]python2.7.13安装MySQLdb模块及使用

命令行安装

    pip install python-mysql

或者在pycharm包中安装
源码安装方式
访问: http://www.lfd.uci.edu/~gohlke/pythonlibs/,下载MySQL_python-1.2.5-cp27-none-win_amd64.whl

将其拷贝到Python安装目录下的Scripts目录下,在文件位置打开cmd,执行pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

验证,python(command line)输入import MySQLdb,没报错,说明安装成功。

#coding:utf-8
 
import MySQLdb
#建立和数据库的连接
db = MySQLdb.connect(host= 'localhost',user="root",passwd="111111",db = "test")
#获取操作游标
cursor = db.cursor()
#执行sql
cursor.execute("select * from test.student")
print cursor.fetchall()
try:
    result =cursor.execute("insert into student (name,age) VALUES ('helloworld',24)")
    db.commit()
    print result
except Exception as e:
    db.rollback()
#关闭连接,释放资源
db.close()

分类:  python

未经允许不得转载:图摄派 » [win]python2.7.13安装MySQLdb模块及使用

分享到: 更多 (0)
avatar

热门文章

  • 评论 抢沙发

    评论前必须登录!

    立即登录   注册

    登录

    忘记密码 ?

    切换登录

    注册

    我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活