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()
原文地址:https://www.cnblogs.com/flyhgx/p/6734596.html