Python mysqldb quick installation in a Windows environment

Source: Internet
Author: User

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?
    1. create table  ' user '   (  
    2.    ' Id '   int (one)  not null auto_increment,   
    3.    ' name '   varchar (255)  default null,  
    4.    ' age '  varchar (255)  default null,  
    5.    primary key  (' Id ')   
    6. )  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?
  1. #-*-Coding:utf-8-*-
  2. #dbtest. py
  3. #just used for a MySQL test
  4. "' "
  5. Created on 2012-2-12
  6. @author: Ken
  7. ‘‘‘
  8. #mysqldb
  9. Import time, MYSQLDB, sys
  10. #connect
  11. Conn=mysqldb.connect (host="localhost", user="root", passwd="Test_pwd", db="School", charset=" UTF8 ")
  12. cursor = Conn.cursor ()
  13. #add
  14. sql = "INSERT into User (Name,age) VALUES (%s,%s)"
  15. param = ("Tom", str)
  16. n = cursor.execute (Sql,param)
  17. Print n
  18. #更新
  19. sql = "Update user set name=%s where id=9001"
  20. param = ("Ken")
  21. n = cursor.execute (Sql,param)
  22. Print n
  23. #查询
  24. n = cursor.execute ("SELECT * from user")
  25. For row in Cursor.fetchall ():
  26. For R in row:
  27. Print R,
  28. Print ""
  29. #删除
  30. sql = "Delete from user where name=%s"
  31. param = ("Ted")
  32. n = cursor.execute (Sql,param)
  33. Print n
  34. Cursor.close ()
  35. #关闭
  36. Conn.close ()

Python mysqldb quick installation in a Windows environment

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.