Python connection to MySQL database example

Source: Internet
Author: User

First online Python-mysql Library

Official Library Address

http://mysql-python.sourceforge.net/

Download the package and unzip it.

Direct execution

The code is as follows Copy Code
Python setup.py Build

There are times when you encounter problems like this:

The code is as follows Copy Code

$ sudo Python setup.py build
Building a distribute egg in/users/mc2/downloads/mysql-python-1.2.4b4
/users/mc2/ Downloads/mysql-python-1.2.4b4/distribute-0.6.28-py2.7.egg
Sh:mysql_config:command not found
Traceback ( Most recent called last):
  File "setup.py", line, in <module>
    metadata, options = Get_config ()
  File "/users/mc2/downloads/mysql-python-1.2.4b4/setup_posix.py", line, in Get_config
& nbsp;   libs = mysql_config ("Libs_r")
  File/users/mc2/downloads/mysql-python-1.2.4b4/setup_ posix.py ", line, in Mysql_config
    raise EnvironmentError ("%s not found "% (Mysql_config.path,)) br> environmenterror:mysql_config not found


Follow the Mysql-python document step by step, still encounter this problem, in fact, the reason for the mysql_config.
First you need to navigate to the local mysql_config, this file is located in the MySQL installation bin directory (assuming MySQL installed under/data/mysql, the path is/data/mysql/bin/mysql_config)

Modify the Site.cfg file to set the Mysql_config on line 12:

Mysql_config.path = "/data/mysql/bin/mysql_config" My computer's Mysql_config file location is

/applications/xampp/xamppfiles/bin/mysql_configmysql_config seems to be an executable of the MySQL configuration gadget, find this file to position the location well. Just fine.

Run again

Python setup.py build says Mysql_config.h not found

Linux's failure is this, all kinds of software, need a variety of packages. Oh, trouble. How good the official integration is. You see, the success of the Apache organization and Microsoft is the integration of a variety of software packages.

It seems the structure of XAMPP, it does not recognize

Download a pure MySQL install on

The path is changed to MySQL, just fine.

The code is as follows Copy Code
Python setup.py Install

Installation Complete

The code is as follows Copy Code

#!/usr/bin/env python
#-*-Encoding:utf-8-*-

Import MySQLdb Www.111cn.net
conn = MySQLdb.connect ("localhost", "urldb", "123456", "Lifesp")
C=conn.cursor ()
C.execute ("Set NAMES GBK")
C.execute ("" "Select * from Spider" ")

Print "Rows selected:", C.rowcount
For row in C.fetchall ():
Print "Note:", row[0], row[1]
C.close () "Can" T connect to the local MySQL server through socket '/tmp/mysql.sock

found that he will automatically find the system installed MySQL.

Link a mysql.sock to the TMP directory
View the MY.CNF configuration file under the current XAMPP
Find the location of Mysql.sock

The code is as follows Copy Code

/applications/xampp/xamppfiles/var/mysql/mysql.sock

Create a link

The code is as follows Copy Code
Ln-s/applications/xampp/xamppfiles/var/mysql/mysql.sock/tmp/mysql.sockok

Had

Finally print out the stuff of the database.

Local installation of MySQL How to start, stop

There is a mysqlstartupitem.pkg file in the downloaded folder to install


command to start and stop the MySQL service on Mac OS X

Summary: command to start and stop the MySQL service on Mac OS X

Start the MySQL service

The code is as follows Copy Code

sudo/library/startupitems/mysqlcom/mysqlcom start

Stop MySQL Service

The code is as follows Copy Code

Sudo/library/startupitems/mysqlcom/mysqlcom stop

Restart the MySQL service

The code is as follows Copy Code
sudo/library/startupitems/mysqlcom/mysqlcom restart

Of course, this is installed in order to start the Python connection program. I usually use the xampp mysql well.

Related Article

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.