[Python] using MySQL

Source: Internet
Author: User

MySQLdb is the package used in Python to manipulate the MYSQL Database

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

MySQLdbinstallation

Under Windows is the EXE version of the one-click Installation . linux requires manual installation, installation process encountered some problems, here is a brief description of the installation under Linux .

wget http://XXX# Download

Tar zvxf xxx.tar.gz# decompression

CD xxx/# into the folder

Python setup.py build# error

Error:can not find Mysql_config

The parameters to configure for MySQL .

Find/-name mysql_config# start looking for mysql_config files from the root directory

Found, then edit the previous file site.py:

Mysql_config =/usr/bin/xxx

Not found, to download these two packages:Libmysqld-dev,libmysqlclient-dev

sudo apt-get install Ibmysqld-dev

sudo apt-get install Libmysqlclient-dev

Then modify the site.py:

Mysql_config.path= "/usr/bin/mysql_config"

Continue running:

Python setup.py build# error

Error:command 'gcc'failed with exit status 1

Continue installing these two packages:Build-essential,python-dev

sudo apt-get install build-essential

sudo apt-get install Python-dev


Continue running:

Python setup.py Build

Python setup.py Install

(Reference Blog: http://blog.163.com/[email protected]/blog/static/1216503422012626105639255/ )


MySQLdbUse

MySQLdb of the poor document,sourceforge only a brief introduction to English, a lot of features are tried out by themselves.

English document:http://sourceforge.net/p/mysql-python/mysqldb-2/ci/default/tree/doc/MySQLdb.txt#l61

Simple operation:

Importing packages

Import MySQLdb


Establish a connection

conn = MySQLdb.connect (host= ' 192.168.1.108 ', user= ' MDX ', passwd= ' med ', db= ' mydb ', port=3306, charset= "UTF8")


Creating a Cursor Object

cursor = Conn.cursor ()


Perform

sql = ' Select%^*$^&^ '

Cursor.execute (SQL)


Get return information

Results = Cursor.fetchall ()

# The results here is a list, and a row of information in the database is a tuple in the list.


Parameter form input

sql = ' INSERT INTO table1 (ID, name) VALUES (%s%s) '

param = (ten, ' Huang ')


Exporting data

The database that is written on the local machine, to be put on the server side, uses the import and the export, realizes this function.

First, the export database on the local machine is in . SQL format:

You can use the MySQL Workbench graphical interface or the command line.

Mysqldump-uroot-p (password)-h127.0.0.1 database1 > Mydb.sql

Get the mydb.sql Export File


Uploading SQL files to the server using FTP


Then import the SQL file on the server:

Mysql-uroot-p (password)-h127.0.0.1;

MySQL > CREATE database mydb# creating new Databases

MySQL > Source mydb.sql# import sql file

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.