Linux under Python play turn mysqldb

Source: Internet
Author: User

(0) Catalogue

Installing Ubuntu's blood-vomiting experience under VMware

0 Basic Learning Shell programming

The magical magic of makefile under Linux

Linux debugging artifact--GDB

10 minutes Learn the basic types of Python

Distributed version management artifact--git

Three states of git files & git ssh key issues

10 minutes to learn the advanced syntax of Python

Configure SSH password-free access and Linux hotkey, restart, kill process

A comparison between the indefinite length parameters of Java and the indefinite length parameters of Python

Linux under Python play turn MySQLdb

One: Cause

(1) Linux installation of Python third-party module----MYSQLDB, I went a lot of detours, here to summarize, I hope for beginners like me to be helpful

(2) For examples of MySQL database connections in Python, see Linux commune or my personal github source code

(3) Mysql-python 1.2.5, also a resource site for pipe python

II: MYSQLDB installation in a Linux environment

(1) Linux installed MySQL database, in addition to download the installation package for installation, the General Linux warehouse will have MySQL, installation command:

Ubuntu\zyp>>sudo apt-get Install mysql-server >>sudo apt-get install  mysql-client

>>mysql-u root-p to verify that MySQL services and clients are installed and powered on

(2) Mysql-python Drive module installation

: https://pypi.python.org/pypi/MySQL-python/

Unzip directly after downloading the Mysql-python-1.2.5.zip file. Enter the mysql-python-1.2.5 directory:

>>python setup.py Install-----Report Error

(3-1) Python installation module error (Importerror:no module named setuptools) workaround

installation See http://blog.csdn.net/ab198604/article/details/8681851 or a more scientific approach

(3-2) Prompt error message: Mysql_config not found

This is due to a lack of libmysqld_dev, libmysqlclient_dev two development packages caused by

Download the missing development package and run two commands: (if the package is not found, please review the Change 163 source in detail)

sudo apt-get insatll Libmysqld-dev
sudo apt-get install Libmysqlclient-dev

(4) Modify the setup_posix.py file under the source code directory and modify the Mysql_config.path to Mysql_config.path= "/usr/bin/mysql_config"

Run the Python setup.py build again in the source code directory

The system again prompts the error message: Error:command ' gcc ' failed with exit status 1

You need to install two resource files:

sudo apt-get install build-essential sudo apt-get install Python-dev
(5) Once again to the source file directory to execute

sudo python setup.py buildsudo python setup.py install

At this point, the MySQL module installation is complete!

(6) Check whether the MySQLdb module can be imported normally

>>> Import MySQLdb

(7) Example

Import mysqldb# Establish connection with mysqlconn = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' root ') # get The cursor of operatorcur = Conn.cursor () # Execute SQL and CREATE database Name/drop database Namecur.execute ("" "Create D Atabase if not exists python "" ") # Select Database Use databaseconn.select_db (' Python ') # Execute and create Tablecur.execut E ("" "CREATE TABLE Test (ID int,ifo varchar)" "") value = [1, "inserted?"] # Insert One recordcur.execute ("INSERT into test values (%s,%s)", value) values=[]for I in Range: Values.append ((i, ' Hello mysqldb,i am Record ' + str (I)) # Insert Multi Recordscur.executemany ("" "INSERT into test values (%s,%s)" ", values) # C Lose Cursorcur.close () print "Create successfully!"

Three: Linux Operational database commands

(1) Create a database

Command: Create databases < database name >

(2) Create a database and assign users

GRANT Select,insert,update,delete,create,drop,alter on
Database name. * To database name @localhost identified by ' password ';

(3) Summary of basic operations

# Common Command---the same to cmd consel----mysql-u root-p--# mysql-show databases;  --Create DATABASE name--use dbname--# drop database name--CREATE TABLE name (ID int, name varchar)--# SELECT * From Tbname (where)/delete from Tbname where--desc tbname# insert INTO tbname values (1, ' ZYP ')/update tbname set id= The Where id=9

Linux under Python play turn mysqldb

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.