Python MySQLdb Windows Installation tutorial and Solution

Source: Internet
Author: User

Python MySQLdb Windows Installation tutorial and Solution

This article describes how to install python MySQLdb in Windows and how to solve the problem. This article describes how to install mysql and install MySQLdb. For more information, see

Using python to access mysql requires a series of Installation

For installation of MySQLdb in linux, see

Python MySQLdb quick installation in Linux

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

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

In windows:

1. Install mysql

: Http://www.mysql.com/downloads/

A graphic tool can be installed along with me. I use MySQL-Front.

2. Install MySQLdb

Now, you have two choices.

A. Install the compiled version (one minute)

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

If the system is 32-bit and has a c ++ compiling environment, you can choose to compile and install the system on your own if you think the RP is good. Of course, it is inevitable that you may encounter problems.

If the system is 64-bit, we recommend that you compile the version.

2.1 install the compiled version:

Http://www.codegood.com/downloads

Download the package from your system and double-click it to install it.

Import MySQLdb to check whether the operation is successful.

My, win7, 64-bit, 2.7

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

2.2 self-compiled and installed

That is to say, there is a gap between off-the-shelf and self-compilation at half past one, especially 64-bit win7.

2.2.1 install setuptools

You must install setuptools before installing MySQLdb. Otherwise, a compilation error occurs.

Http://pypi.python.org/pypi/setuptools

The http://peak.telecommunity.com/dist/ez_setup.py uses this installation (this is required for 64-bit systems)

2.2.2 install MySQLdb

Download MySQLdb

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

Decompress the package and run cmd to enter the corresponding folder.

If a 32-bit system has a gcc compiling environment

The Code is as follows:

Python setup. py build

2.2.3 problem summary

A. 64-bit system, unable to read the Registry

The exception information is as follows:

The Code is as follows:

F: \ devtools \ MySQL-python-1.2.3> pythonsetup. py build

Traceback (most recent call last ):

File "setup. py", line 15, in

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

Solution:

In fact, after analyzing the code, we found that we only looked for the mysql installation address and modified setup_windows.py as follows:

Add a line to note the two lines, which is the first 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: \ devtools \ MySQL Server 5.5"

B. There is no gcc compiling environment

The Code is as follows:

Unable to find vcvarsall. bat

Solution: Install the compiling 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. (add the installation PATH to the 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 Add the following content to 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: \ devtools \ MinGW \ bin \ gcc.exe-mno-cygwin-mdll-O-Wall-Dversion_info = (1, 2, 3 ,'

Final', 0)-D _ version __= 1.2.3 "-IF: \ devtools \ 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

The parameter is a compilation parameter specific to vc. If mingw is used, it is not supported because it is gcc. You can remove it from setup_windows.py.

/Zl

Solution:

Change setup_windows.py to null

The Code is as follows:

# Extra_compile_args = ['/zl']

Extra_compile_args = ['']

I have encountered these problems.

3. add, delete, modify, and query code examples and results (just for test)

The Code is as follows:

Create table 'user '(

'Id' int (11) 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.exe cute (SQL, param)

Print n

# Update

SQL = "update user set name = % s where Id = 9001"

Param = ("ken ")

N = cursor.exe cute (SQL, param)

Print n

# Query

N = cursor.exe cute ("select * from user ")

For row in cursor. fetchall ():

For r in row:

Print r,

Print ""

# Delete

SQL = "delete from user where name = % s"

Param = ("ted ")

N = cursor.exe cute (SQL, param)

Print n

Cursor. close ()

# Disable

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.