python2.7安装mysql的数据驱动

来源:互联网 发布:linux 卸载 工具 编辑:程序博客网 时间:2024/06/11 20:38

刚上手python不久,写下一些blog来记录自己学习python的一些体会和操作,看了很多博主的一些blog,觉得对自己的帮助很大,所以希望自己的文章也能给你们提供一些帮助

我的系统版本是 WIN10 64bit

用的python版本是python 2.7 64bit

怎么查看你的python版本呢?直接在命令行输入python就可以查看了(前提是你的python已经可以正常使用了哦)

下面你需要下载python的mysql驱动包了,记得也是下载64bit的哦,不然会报错,安装不上的

我直接下载的特别好用的是http://download.csdn.net/detail/seven_zhao/6607625(需要一个积分哦)

下载就可以直接点击.exe进行安装,不过很有可能会提示你Python version 2.7 required, which was not found in the registry,找不到注册表的问题

这时你需要建一个register.py文件

里面的代码写上(网上有很多啦,我就直接粘贴了)
## script to register Python 2.0 or later for use with win32all# and other extensions that require Python registry settings## written by Joakim Loew for Secret Labs AB / PythonWare## source:# http://www.pythonware.com/products/works/articles/regpy20.htm## modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html import sys from _winreg import * # tweak as necessaryversion = sys.version[:3]installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (    installpath, installpath, installpath) def RegisterPy():    try:        reg = OpenKey(HKEY_CURRENT_USER, regpath)    except EnvironmentError as e:        try:            reg = CreateKey(HKEY_CURRENT_USER, regpath)            SetValue(reg, installkey, REG_SZ, installpath)            SetValue(reg, pythonkey, REG_SZ, pythonpath)            CloseKey(reg)        except:            print "*** Unable to register!"            return        print "--- Python", version, "is now registered!"        return    if (QueryValue(reg, installkey) == installpath and        QueryValue(reg, pythonkey) == pythonpath):        CloseKey(reg)        print "=== Python", version, "is already registered!"        return    CloseKey(reg)    print "*** Unable to register!"    print "*** You probably have another Python installation!" if __name__ == "__main__":    RegisterPy()
然后直接运行

这样注册表就安装好了,就可以直接运行MySQL-python-1.2.3.win-amd64-py2.7.exe,现在就不会出现问题啦

我们测试一下mysql的数据驱动可以用了没,在python.exe运行代码



没有报错,说明安装成功啦!

下面写一段测试代码来连接数据库吧,mysql.py

# -*- coding: utf-8 -*-import MySQLdbconn=MySQLdb.connect(host='localhost',          user='root',          passwd='',          db='db_spring')cursor = conn.cursor()cursor.execute ("SELECT VERSION()")row = cursor.fetchone ()print "server version:", row[0]cursor.close()conn.close()
运行代码



这样,整个python的mysql数据库驱动安装和连接就结束了