Python的MySQLdb模块安装和使用

2025-04-21 04:39:59

1、先去下载python_mysql.exe官方地址:https://pypi.python.org/pypi/MySQL-python#downloads我是用python_mysql 1.2.3,版本看您的需要

Python的MySQLdb模块安装和使用

2、安装python_m鲻戟缒男ysql.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 MySQLdbcmd 运行没有报错说明安装成功

Python的MySQLdb模块安装和使用

4、测试数据库是否连接成功,直接上代码:#coding=utf-8import MySQLd水瑞侮瑜b as md芟鲠阻缒bcon = 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。
相关推荐
猜你喜欢