search
HomeBackend DevelopmentPython Tutorialpython MySQLdb Windows下安装教程及问题解决方法

使用python访问mysql,需要一系列安装

linux下MySQLdb安装见 
Python MySQLdb在Linux下的快速安装
http://www.jb51.net/article/65743.htm

-------------------------------------------------------------
以下是windows环境下的:

1. 安装数据库mysql
下载地址:http://www.mysql.com/downloads/
可以顺带装个图形工具,我用的是MySQL-Front
 
2. 安装MySQLdb
 
好了,到了这一步,你有两个选择
A. 安装已编译好的版本(一分钟)
B. 从官网下,自己编译安装(介个…..半小时到半天不等,取决于你的系统环境以及RP)
 
若是系统32位的,有c++编译环境的,自认为RP不错的,可以选择自己编译安装,当然,遇到问题还是难免的,一步步搞还是能搞出来的
若是系统64位的,啥都木有的,建议下编译版本的,甭折腾
 
2.1安装已编译版本:
http://www.codegood.com/downloads
根据自己系统下载,双击安装,搞定
然后import MySQLdb,查看是否成功
 
我的,win7,64位,2.7版本

MySQL-python-1.2.3.win-amd64-py2.7.exe
 
2.2自己编译安装
话说搞现成的和自己编译差距不一一点半点的,特别是64位win7,搞死了
 
2.2.1安装setuptools

在安装MySQLdb之前必须安装setuptools,要不然会出现编译错误
http://pypi.python.org/pypi/setuptools
http://peak.telecommunity.com/dist/ez_setup.py 使用这个安装(64位系统必须用这个)
 
2.2.2安装MySQLdb

下载MySQLdb
http://sourceforge.net/projects/mysql-python/
 
解压后,cmd进入对应文件夹
如果32位系统且有gcc编译环境,直接

复制代码 代码如下:

python setup.py build

2.2.3问题汇总
A. 64位系统,无法读取注册表的问题
异常信息如下:

复制代码 代码如下:

F:\devtools\MySQL-python-1.2.3>pythonsetup.py build
Traceback (most recent call last):
 File "setup.py", line 15, in
   metadata, options = get_config()
 File "F:\devtools\MySQL-python-1.2.3\setup_windows.py", line7, in get_config
   serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options[' registry_ke
y'] )
WindowsError: [Error 2] The system cannotfind the file specified

解决方法:
其实分析代码,发现只是寻找mysql的安装地址而已  修改setup_windows.py如下
注解两行,加入一行,为第一步mysql的安装位置
复制代码 代码如下:

   #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,options['registry_key'] )
   #mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
   mysql_root = r"F:\devtools\MySQL\MySQL Server 5.5"

B.没有gcc编译环境
复制代码 代码如下:

unable to find vcvarsall.bat

解决方法:安装编译环境(一个老外的帖子)
1)  First ofall download MinGW. Youneed g++compiler and MingW make in setup.
2)  If youinstalled MinGW for example to “C:\MinGW” then add “C:\MinGW\bin”to your PATH in Windows.(安装路径加入环境变量)
3)  Now startyour Command Prompt and go the directory where you have your setup.py residing.
4)  Last andmost important step:
setup.py install build --compiler=mingw32
或者在setup.cfg中加入:
复制代码 代码如下:

[build]
    compiler = mingw32
 
C.gcc: /Zl: No suchfile or directory错误
异常信息如下
F:\devtools\MinGW\bin\gcc.exe -mno-cygwin-mdll -O -Wall -Dversion_info=(1,2,3,'
final',0) -D__version__=1.2.3"-IF:\devtools\MySQL\MySQL Server 5.5\include" -IC
:\Python27\include -IC:\Python27\PC -c_mysql.c -o build\temp.win-amd64-2.7\Rele
ase\_mysql.o /Zl
gcc: error: /Zl: No such file or directory
error: command 'gcc' failed with exitstatus 1

参数是vc特有的编译参数,如果使用mingw的话因为是gcc所以不支持。可以在setup_windows.py中去掉
/Zl
 
解决方法:
修改setup_windows.py  改为空的
复制代码 代码如下:

#extra_compile_args = [ '/Zl' ]
    extra_compile_args = [ '' ]

 目前就遇到这几个问题,望补充
 
3.  增删改查代码示例及结果(just for test)
复制代码 代码如下:

CREATE TABLE `user` ( 
  `Id` int(11) NOT NULL AUTO_INCREMENT, 
  `name` varchar(255) DEFAULT NULL, 
  `age` varchar(255) DEFAULT NULL, 
  PRIMARY KEY (`Id`) 
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; 

复制代码 代码如下:

#-*- coding:utf-8 -*- 
#dbtest.py 
#just used for a mysql test 
'''''
Created on 2012-2-12
 
@author: ken
''' 
#mysqldb     
import time, MySQLdb, sys   
        
#connect  
conn=MySQLdb.connect(host="localhost",user="root",passwd="test_pwd",db="school",charset="utf8")   
cursor = conn.cursor()     
        
#add 
sql = "insert into user(name,age) values(%s,%s)"    
param = ("tom",str(20))     
n = cursor.execute(sql,param)     
print n     
        
#更新     
sql = "update user set name=%s where Id=9001"    
param = ("ken")     
n = cursor.execute(sql,param)     
print n     
 
#查询     
n = cursor.execute("select * from user")     
for row in cursor.fetchall():     
    for r in row:     
        print r,    
print "" 
 
 
#删除     
sql = "delete from user where name=%s"    
param =("ted")     
n = cursor.execute(sql,param)     
print n     
cursor.close()     
        
#关闭     
conn.close() 
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools