python2.7安装mysql的数据驱动

在WIN10 64bit环境下,使用Python 2.7 64bit安装MySQL数据驱动。遇到注册表问题时,通过创建register.py文件解决。成功安装后,通过测试代码验证驱动连接数据库的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

我的系统版本是 WIN10 64bit

用的python版本是python 2.7 64bit

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

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

我直接下载的特别好用的是http://download.youkuaiyun.com/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 necessary
version = 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 MySQLdb
conn=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数据库驱动安装和连接就结束了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值