Python operation MySQL

Source: Internet
Author: User

First you need to install zlib, ZLIB-DEVL, Python, and Python-devel.

Yum Install zlib Zlib-devel


Install the Use package

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

Get a connection connection to MySQL package

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.2/MySQL-python-1.2.2.tar.gz

You can select a high version of 1.2.3 without encountering error 1 and error 2

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz


Specific Operation command:

# tar ZXVF setuptools-0.6c11.tar.gz

# CD SETUPTOOLS-0.6C11

# python setup.py Build

# python setup.py Install


# tar ZXVF mysql-python-1.2.2.tar.gz

# CD mysql-python-1.2.2

# python setup.py Build

# python setup.py Install


Test scripts

#!/usr/bin/env python

#-*-Coding:utf-8-*-

Import Os,sys

Import MySQLdb

Try

db = MySQLdb.connect (host = ' localhost ', user= ' root ' passwd = ' root ', db = ' mysql ')

Except Mysqldb.error,e:

Print "Error%d:%s"% (E.args[0],e.args[1])

Exit (1)

cursor = Db.cursor ()

Cursor.execute (' select * from user ')

Result_set=cursor.fetchall ()

Print Result_set

Cursor.close ()

Db.close ()



Error 1.

>>> Import MySQLdb

Traceback (most recent):

File "", Line 1, in

File "build/bdist.linux-i686/egg/mysqldb/__init__.py", line +, in

File "build/bdist.linux-i686/egg/_mysql.py", line 7, in

File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__

Importerror:libmysqlclient_r.so.16:cannot open Shared object file:no such file or directory


Workaround

Find the module first

# Find/-name libmysqlclient_r.so.16

/usr/local/src/mysql-5.1.44-linux-i686-glibc23/lib/libmysqlclient_r.so.16

/usr/local/mysql/lib/libmysqlclient_r.so.16

To add a module path to a dynamic link library to share

Vim/etc/ld.so.conf

Join: libmysqlclient_r.so.16 Directory

Ldconfig-v


Error 2.

>>> Import MySQLdb

/USR/LOCAL/LIB/PYTHON2.6/SITE-PACKAGES/MYSQL_PYTHON-1.2.2-PY2.6-LINUX-I686.EGG/MYSQLDB/__INIT__.PY:34: Deprecationwarning:the Sets module is deprecated


Workaround

Enter the./mysql-python-1.2.2/mysqldb Directory

1) in the file "__init__" will:

From sets import Immutableset

Class Dbapiset (Immutableset):

Replaced by

Class Dbapiset (Frozenset)

3) in the file "converters.py", change "set" to "set"

Change "set" to "set" in return set ([i-I in S.split (', ') if I])

Change Set:set2str, "set" to "set"

2) Remove from File "converters.py":

From sets import Baseset, Set

After the modification is done, compile and install.


Error 3.

_MYSQL.C:36:23: Error: my_config.h: no file or directory


Workaround

Set the Include header file path, as follows

Export c_include_path=/usr/local/mysql/include/


Error 4.

>>> cur.execute (' select * from user ')

Segmentation fault


Workaround

Writing the statement as "select User,host from user" can be detected normally.

This issue is found on 64-bit servers that are installed. I do not know why there is a paragraph error. Are all 64-bit services

This problem, I have not tested


This article is from the "7727197" blog, please be sure to keep this source http://7737197.blog.51cto.com/7727197/1663093

Python operation MySQL

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.