Python installation in Mamp mysqldb

Source: Internet
Author: User

The Python standard database interface for Python Db-api,python DB-API provides developers with a database application programming interface.

MYSQLDB is the interface for the Python link MySQL database, which implements the Python database API specification V2.0, built on the MySQL C API.

First, how to install MySQLdb?

In order to write MySQL scripts with Db-api, you must make sure that MySQL is already installed. Copy the following code and execute:

#!/usr/bin/python#-*-coding:utf-8-*-import mysqldb

If the output after execution is as follows, it means that you do not have the MySQLdb module installed:

Traceback (most recent):  File "test.py", line 3, <module>    import mysqldbimporterror:no module Named MySQLdb

To install MYSQLDB, visit Http://sourceforge.net/projects/mysql-python, (Linux platform can access: https://pypi.python.org/pypi/ Mysql-python) From here you can choose the installation package for your platform, which is divided into precompiled binaries and source code installation packages.

If you choose a binary file release, the installation process will complete with the basic installation instructions. If you are installing from source code, you will need to switch to the top-level directory of the MYSQLDB release and type the following command:

$ gunzip mysql-python-1.2.2.tar.gz$ tar-xvf mysql-python-1.2.2.tar$ cd mysql-python-1.2.2$ python setup.py build$ python setup.py Install

Note: Make sure that you have root privileges to install the above modules.

Run error message:

_mysql.c:44:10:fatal error: ' my_config.h ' File not found

The reason is because MySQL is not installed separately, is installed a MAMP integrated development environment package, in Mamp php and MySQL header files are not, so will report this error. We have two solutions, one is to recompile MySQL, one is to install mysq-connector-c alone, because the development environment package has been brought to MySQL, we will no longer compile the installation, separate installation is OK.

execute the following statement:

Brew Install Mysql-connector-c

Second, the database connection

The following error message appears:

    Sh:mysql_config:command not found      Traceback (most recent):        File ' setup.py ', line <module>          metadata, Options = Get_config ()        File "/users/macbook/downloads/mysql-python-1.2.5/setup_posix.py", line +, in get_config          libs = Mysql_config (" Libs_r ")        File"/users/macbook/downloads/mysql-python-1.2.5/setup_posix.py ", line, in Mysql_config          raise EnvironmentError ('%s not found '% (Mysql_config.path,))      Environmenterror:mysql_config not found  

Modify the setup_posix.py file with the following code

Mysql_config.path = "/applications/mamp/library/bin/mysql_config"

If you do not know its directory, you can use the command to find

sudo find/-name mysql_config

You may still get an error after completing this step:

    Traceback (Recent):        file "./hello.py", line 3, <module>          import mysqldb        File "build/bd ist.macosx-10.10-intel/egg/mysqldb/__init__.py ", line <module>        File" build/ bdist.macosx-10.10-intel/egg/_mysql.py ", line 7, in <module>        File" build/bdist.macosx-10.10-intel/egg/_ mysql.py ", line 6, in __bootstrap__      Importerror:dlopen (/users/macbook/.python-eggs/mysql_ Python-1.2.5-py2.7-macosx-10.10-intel.egg-tmp/_mysql.so, 2): Library not Loaded:libmysqlclient.18.dylib        Referenced from:/users/macbook/.python-eggs/mysql_python-1.2.5-py2.7-macosx-10.10-intel.egg-tmp/_mysql.so        Reason:image not found  

At this point, you need to execute the command

sudo ln-s/usr/local/mysql/lib/libmysqlclient.18.dylib/usr/lib/libmysqlclient.18.dylib

Note: The actual directory portion of the soft connection is subject to the directory on its own machine

In addition: Mamp The following error message appears:

ERROR 2002 (HY000): Can ' t connect to local MySQL server through socket '/tmp/mysql.sock '

Unable to find the socket file, find the location of the native file

sudo find/-name Mysql.sock

Passing a file address as a parameter to a database connection statement

Con=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' studentdb ', port=3306,unix_socket= '/ Applications/mamp/tmp/mysql/mysql.sock ", charset= ' UTF8 ')

Third, examples:

The following example links the MySQL Studentdb database:

#!/usr/bin/python#-*-coding:utf-8-*-import mysqldb import sysimport sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') Con=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' studentdb ', port=3306,unix_socket= '/ Applications/mamp/tmp/mysql/mysql.sock ", charset= ' UTF8 ') #获取 MySQL link object with con: #获取执行查询的对象cur = con.cursor () #执行那个查询, Here is the SELECT statement Cur.execute ("SELECT * from Student") #使用 Cur.rowcount Gets the number of bars of the result set numrows = Int (cur.rowcount) #循环 numrows times, each time it is taken out One row of data for I in range (numrows): #每次取出一行, put into row, this is a tuple (id,name) row = Cur.fetchone () #直接输出两个元素print row[0], row[1]

Mamp in Python installation mysqldb

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.