1、pip install MySQL-python
2、如果提示如图显示,需要安装Microsoft Visual C++ 9.0 is required:
3、使用pip来安装提示如下错误:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
经查资料是确实一个驱动引起的:MySQL-python-1.2.3.win-amd64-py2.7.exe
下载地址:http://vdisk.weibo.com/s/aBSXQ0shtv7cN
写的一个简单的调用查询mysql的模块,仅供参考(这个是python 2中使用):
# -*- coding:utf-8 -*- """ @author:yfk @time:2017/12/27 """ import MySQLdb class OperateDatabase(object): def __init__(self,host,port,user,passwd,db): self.conn = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd, db=db) # 获取游标 self.cur = self.conn.cursor() def executeSQL(self,sql): try: self.cur.execute(sql) except: print "your sql was wrong" def resultData(self,sql,keyword): self.executeSQL(sql) if keyword == "selectOne": data = self.cur.fetchone() if data ==None: print "unable to fetch data" else: return data if keyword == "selectAll": data = self.cur.fetchall() if data == None: print "unable to fetch data" else: return data if keyword == "update": try: self.cur.execute(sql) # 提交到数据库 self.conn.commit() except: # 发生错误时回滚 self.conn.rollback() print "update fail" print "update success" if keyword == "delete": try: self.cur.execute(sql) # 提交到数据库 self.conn.commit() except: # 发生错误时回滚 self.conn.rollback() print "delete fail" print "delete success" else: print "Not available keywords" self.cur.close() self.conn.close() main = OperateDatabase if __name__ == "__main__": main(module==None) # data = OperateDatabase().resultData("SELECT * FROM wms_inv_inventory WHERE sku_code = 'A403';",'selectOne') # print data