Check whether the environment supports the MySQLdb module. If not, install the module as follows:
[Root @ bw-vm-soft ~] # Wgethttp: // response
[Root @ bw-vm-soft ~] # Tar zxvf MySQL-python-1.2.3.tar.gz
[Root @ bw-vm-soft ~] # Cd MySQL-python-1.2.3
Modify the mysql_config path in site. cfg
Vi site. cfg
Mysql_config =/usr/local/webserver/mysql/bin/mysql_config (depending on the mysql installation path)
[Root @ bw-VMS-soft MySQL-python-1.2.3] # python setup. py build
[Root @ bw-VMS-soft MySQL-python-1.2.3] # python setup. py install
After confirming that the installation is successful
Add/usr/local/webserver/mysql/lib/mysql in/etc/ld. so. conf and execute ldconfig
Then you can go to the topic to operate/manage Mysql.
Example 1: insert data
######################################## ###################################
#! /Usr/bin/env python
# _ * _ Coding: UTF-8 _*_
Import MySQLdb as mdb
Import sys
Con = mdb. connect ('localhost', 'root', '123', 'pythontest ')
With con:
Cur = con. cursor ()
Cur.exe cute ("create table if not exists \
Users (Id int primary key AUTO_INCREMENT, Name VARCHAR (25 ))")
Cur.exe cute ("insert into Users (Name) VALUES ('Richard shen ')")
Cur.exe cute ("insert into Users (Name) VALUES ('zhangsan ')")
Cur.exe cute ("insert into Users (Name) VALUES ('Li si ')")
Cur.exe cute ("insert into Users (Name) VALUES ('wang dongdong ')")
######################################## ###################################
Example 2: View data
#! /Usr/bin/env python
# _ * _ Coding: UTF-8 _*_
Import MySQLdb as mdb
Import sys
Con = mdb. connect ('localhost', 'root', '123', 'pythontest ')
With con:
Cur = con. cursor ()
Cur.exe cute ("SELECT * FROM Users ")
Numrows = int (cur. rowcount)
For I in range (numrows ):
Row = cur. fetchone ()
Print row [0], row [1]
Execution result:
1 Richard shen
2 Zhang san
3 Li si
4 Wang dongdong
This is the most basic operation of the database.