What is MySQLdb
MYSQLDB is the interface for the Python link MySQL database, which implements the Python database API specification V2.0, built on the MySQL C API.
How to install MySQLdb
install MYSQLDB, please visit http://sourceforge.net/projects/mysql-python , (Linux platform can access: https://pypi.python.org/pypi/mysql-python) From here you can choose the installation package for your platform, divided into precompiled binaries and source code installation packages.
$ gunzip mysql-python-1.2.2.tar.gz$ tar-xvf mysql-python-1.2.2.tar$ cd mysql-python-1.2.2$ python setup.py build$ python setup.py Install
Operation Syntax:
Import MySQLdb as MySQLdb
Con = mysqldb.connect (host = "localhost", user = "root", passwd= "123456", db = "test") #打开数据库
cur = db.cursor () #使用cursor () method gets the operation cursor
sql = select * from Ceshi #定义sql语句
Cur.execute (SQL) #使用execute方法执行SQL语句
data = Cur.fetchone () #使用fethcone () method gets a database
Cur.fetchall () Get all data
Con.commit () #提交到数据库
Con.rollback () #错误回滚
Con.close () #关闭数据库
Python operation MySQL database