python mysqldb Quick installation, problem resolution in Windows environments
Using Python to access MySQL requires a series of installation
Linux under MYSQLDB installation see
Python mysqldb fast installation under Linux
http://blog.csdn.net/wklken/article/details/7271019
-------------------------------------------------------------
The following are the Windows environments:
1. Install database MySQL
: 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
Python setup.py Build
2.2.3 Problem Summary
A. 64-bit system, unable to read registry problems
The exception information is as follows:
f:\devtools\mysql-python-1.2.3>pythonsetup.py Build
Traceback (most recent):
File "setup.py", line <module>
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
#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
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:
[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
#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)
[SQL]View Plaincopyprint?
- 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;
CREATE TABLE ' user ' ( ' Id ' int (one) ' NOT null auto_increment, ' name ' varchar (255) DEFAULT NULL, ' age ' varchar (2 Default NULL, PRIMARY KEY (' Id ')) Engine=innodb auto_increment=7 default Charset=utf8;
[Python]View Plaincopyprint?
- #-*-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)
- 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 ()
Python mysqldb quick installation in a Windows environment