Transmission Door
# yum-y Install Mysql-python
Run the above command to install the RPM installation package Centos7 comes with, but the default is to install it for python2.*
Connect MySQL Mini Demo
1 #-*-encoding:utf-8-*-2 ImportOS, sys, string3 ImportMySQLdb4 5 Try:6conn = MySQLdb.connect (host="localhost", user="Root", passwd="123456", db="MyDatabase")7 exceptException, E:8 Printe;9 sys.exit ()Ten One #Get cursor obj to operator database Acursor =conn.cursor () -sql ="CREATE table if not EXISTS test (name varchar (+) primary key, age int (3))" - cursor.execute (SQL) the - #insert data to DB -sql ="INSERT into Test (name, age) VALUES ('%s ',%d)"% ("Coode", 25) -SQL2 ="INSERT into Test (name, age) VALUES ('%s ',%d)"% ("Ben", 19) + Try: - cursor.execute (SQL) + conn.commit (); A Cursor.execute (SQL2) at Conn.commit () - exceptException, E: - Printe - - #Select data from DB -sql ="SELECT * FROM Test" in cursor.execute (SQL) -result =Cursor.fetchall () to #The result is a list + ifResult: - forRsinchResult: the PrintRs[0], rs[1] * $ cursor.close ()Panax NotoginsengConn.close ()
Install Mysql-python to connect MySQL