Python mysqldb Windows installation tutorial and problem solving method

Source: Internet
Author: User
Using Python to access MySQL requires a series of installation

Linux under MYSQLDB installation see
Python mysqldb fast installation under Linux
Http://www.jb51.net/article/65743.htm

-------------------------------------------------------------
The following are the Windows environments:

1. Install database MySQL
Download Address: http://www.mysql.com/downloads/
I can put a graphic tool in the Mysql-front.

2. Installing MYSQLDB

Well, by this step, you have two choices.
A. Install the compiled version (one minute)
B. From the official website, build your own installation (...). Half an hour to half a day, depending on your system environment and RP)

If the system 32-bit, have C + + compiler environment, since the RP is good, you can choose to build their own installation, of course, encountered problems or inevitable, step by step or can be made out of
If the system is 64-bit, everything is wood, the proposed compilation version, don't toss

2.1 Install the compiled version:
Http://www.codegood.com/downloads
Download according to your system, double-click Install, fix
Then import MySQLdb to see if it was successful

My, win7,64 bit, version 2.7

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

2.2 Compiling your own installation
Words to make ready and their compilation gap is not 11:30 point, especially 64-bit Win7, killed

2.2.1 Installation Setuptools

You must install Setuptools before installing MYSQLDB, or a compilation error will occur
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 enters the corresponding folder
If the 32-bit system has a gcc-compiled environment, direct
Copy the Code code as follows:


Python setup.py Build

2.2.3 Problem Summary
A. 64-bit system, unable to read registry problems
The exception information is as follows:
Copy the Code code as follows:


f:\devtools\mysql-python-1.2.3>pythonsetup.py Build
Traceback (most recent):
File "setup.py", line.
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


Workaround:
In fact, the analysis of the code, found just looking for MySQL installation address only modified setup_windows.py as follows
Note Two lines, add a line, for the first step MySQL installation location
Copy CodeThe 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:\devtools\MySQL\MySQL Server 5.5"


B. No GCC compilation environment
Copy CodeThe code is as follows:


Unable to find Vcvarsall.bat


Workaround: Install the compilation environment (a foreigner's post)
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. (Install path add environment variable)
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
Or join in the SETUP.CFG:
Copy CodeThe code is as follows:


[Build]
compiler = Mingw32

C.GCC:/zl:no suchfile or directory Error
The exception information is as follows
F:\devtools\MinGW\bin\gcc.exe-mno-cygwin-mdll-O-wall-dversion_info= (the "
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


Parameters are VC-specific compilation parameters, if you use MinGW because it is GCC is not supported. Can be removed from the setup_windows.py
/zl

Workaround:
Change setup_windows.py to Empty
Copy CodeThe code is as follows:


#extra_compile_args = ['/zl ']
Extra_compile_args = ["]


At present, we are confronted with these problems and hope to add

3. Adding and removing changes to code examples and results (just for test)
Copy CodeThe 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;


Copy CodeThe 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.