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

Http://www.jb51.net/article/65743.htm

-------------------------------------------------------------

The following are in the Windows environment:

1. Install database MySQL

Download Address: http://www.mysql.com/downloads/

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:

Http://www.codegood.com/downloads

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.

Mysql-python-1.2.3.win-amd64-py2.7.exe

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://pypi.python.org/pypi/setuptools

http://peak.telecommunity.com/dist/ez_setup.py Use this installation (64-bit system must use this)

2.2.2 Installation MySQLdb

Download MySQLdb

http://sourceforge.net/projects/mysql-python/

After decompression, cmd into the corresponding folder

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

The code is as follows:

Python setup.py 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>pythonsetup.py Build

Traceback (most recent call last):

File "setup.py", line, in

metadata, Options = Get_config ()

File "f:devtoolsmysql-python-1.2.3setup_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

Workaround:

In fact, the analysis of the code, found just looking for MySQL installation address modified setup_windows.py 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 setup.py residing.

4) Last Andmost important step:

setup.py Install build--compiler=mingw32

Or add in Setup.cfg:

The code is as follows:

[Build]

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 Buildtemp.win-amd64-2.7rele

Ase_mysql.o/zl

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 setup_windows.py

/zl

Workaround:

Modify setup_windows.py 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,

PRIMARY KEY (' Id ')

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

The code is as follows:

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

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.