Python的MySQLdb模块安装和使用

2025-10-23 04:11:04

1、先去下载python_mysql.exe

官方地址:https://pypi.python.org/pypi/MySQL-python#downloads

我是用python_mysql  1.2.3,版本看您的需要

Python的MySQLdb模块安装和使用

2、安装python_mysql.exe时:软件安装时  找不到注册表 

网上搜了一下 ,解决方法

{

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!"

}

启动命令切到register.py文件目录下执行:

重启安装python_mysql就不会有这个错误

Python的MySQLdb模块安装和使用

3、安装完成  测试python_mysql 是否安装成功

import  MySQLdb

cmd 运行

没有报错说明安装成功

Python的MySQLdb模块安装和使用

4、测试数据库是否连接成功,直接上代码:

#coding=utf-8import  MySQLdb as mdbcon = mdb.connect(    host = 'localhost',    port = 3306,    user = 'root',    passwd = '123456',    db = 'ceshi',    charset='utf8')cur = con.cursor()  #执行链接数据的命令

Python的MySQLdb模块安装和使用

5、到这里pythonan安装python_mysql就结束

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢