Python mysqldb Windows installation tutorials and problem solving methods

Source: Internet
Author: User

This article mainly introduces the Python mysqldb Windows installation tutorial and problem solving method, this article explains the installation of database MySQL, installation mysqldb and other steps, the need for friends can refer to the

Using Python to access MySQL requires a series of installation

Linux under MYSQLDB installation see

Fast installation of Python MySQLdb under Linux



The following are in the Windows environment:

1. Install database MySQL

Download Address:

You can install a graphics tool, I use the Mysql-front

2. Install MySQLdb

Well, at this point, you have two choices.

A. Install a compiled version (one minute)

B. From the official website, compile and install yourself (... half an hour to half a day, depending on your system environment and RP)

If the system 32-bit, with C + + compiler environment, since the RP is good, you can choose to compile their own installation, of course, encountered problems or unavoidable, step by step or can be made out of

If the system 64-bit, what is wood, the proposed version of the compiler, do not toss

2.1 Install compiled version:


Download according to your system, double-click to install, fix

Then import MySQLdb to see if it was successful

My, win7,64 bit, 2.7 version.

2.2 Compile and install yourself

Say to make ready-made and own compile gap not 11:30 point, especially 64 Win7, got killed

2.2.1 Installation Setuptools

Setuptools must be installed before installing MYSQLDB, or there will be a compilation error

Http:// Use this installation (64-bit system must use this)

2.2.2 Installation MySQLdb

Download MySQLdb

After decompression, cmd into the corresponding folder

If the 32-bit system has a GCC compilation environment, direct

The code is as follows:

Python Build

2.2.3 Problem Summary

A. 64-bit system, unable to read the registry problem

The exception information is as follows:

The code is as follows:

f:devtoolsmysql-python-1.2.3> Build

Traceback (most recent call last):

File "", line, in

metadata, Options = Get_config ()

File "", Line7, in Get_config

Serverkey = _winreg. Openkey (_winreg. HKEY_LOCAL_MACHINE, options[' Registry_ke

Y '])

Windowserror: [Error 2] The system cannotfind the file specified


In fact, the analysis of the code, found just looking for MySQL installation address modified as follows

Note Two lines, join a row, for the first step MySQL installation location

The code is as follows:

#serverKey = _winreg. Openkey (_winreg. hkey_local_machine,options[' Registry_key '])

#mysql_root, dummy = _winreg. QueryValueEx (Serverkey, ' Location ')

Mysql_root = r "F:devtoolsmysqlmysql Server 5.5"

B. No GCC compilation environment

The code is as follows:

Unable to find Vcvarsall.bat

Workaround: Install the compilation environment (a foreigner's post)

1) the Ofall download MinGW. Youneed G++compiler and MinGW make in Setup.

2) If youinstalled MinGW For example to "C:mingw" then add "C:mingwbin" to your PATH in Windows. (Installation path joins environment variable)

3 Startyour Command Prompt and go directory where you have your residing.

4) Last Andmost important step: Install build--compiler=mingw32

Or add in Setup.cfg:

The code is as follows:


compiler = Mingw32

C.GCC:/zl:no suchfile or directory Error

The exception information is as follows

F:devtoolsmingwbingcc.exe-mno-cygwin-mdll-o-wall-dversion_info= (1,2,3, '

Final ', 0)-d__version__=1.2.3 "-if:devtoolsmysqlmysql Server 5.5include"-ic

:P Ython27include-ic:python27pc-c_mysql.c-o


Gcc:error:/zl:no such file or directory

Error:command ' gcc ' failed with exitstatus 1

Parameters are VC-specific compilation parameters, if the use of MinGW, because it is GCC does not support. Can be removed from the



Modify change to empty

The code is as follows:

#extra_compile_args = ['/zl ']

Extra_compile_args = [']

Now encountered these problems, hope to add

3. Additions and deletions to check code samples and results (just for test)

The code is as follows:

CREATE TABLE ' user ' (

' Id ' int (one) not NULL auto_increment,

' Name ' varchar (255) DEFAULT NULL,

' Age ' varchar (255) DEFAULT NULL,


) Engine=innodb auto_increment=7 DEFAULT Charset=utf8;

The code is as follows:


#dbtest. py

#just used for a MySQL test


Created on 2012-2-12

@author: Ken



Import time, MYSQLDB, sys


Conn=mysqldb.connect (host= "localhost", user= "root", passwd= "Test_pwd", db= "school", charset= "UTF8")

cursor = Conn.cursor ()


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 ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.