1. Import MySQL Module
Python 2.7.5 (Default, June, 18:11:42) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on Linux2type "help", "copyright", " Credits "or" license "for more information.>>> import Mysqldbtraceback (most recent call last): File" <stdin& gt; ", line 1, in <module>importerror:no module named mysqldb>>>
Installing the MySQL module
[[email protected] ~]# wget https://pypi.python.org/packages/source/m/mysql-python/mysql-python-1.2.5.zip[[email protected] ~]# unzip MySQL-python-1.2.5.zip[[email protected] MySQL-python-1.2.5]# python setup.py build[[email protected] mysql-python-1.2.5]# python setup.py install# or so (centos/redhat) [[email protected] ~]# yum install mysql-python # pre-configured Yum source # re-import successfully [[email protected] ~]# pythonpython 2.7.5 (default, jun 24 2015, 00:41:19) [GCC 4.8.3 20140911 (red hat 4.8.3-9)] on linux2type "Help", "copyright", " Credits " or " license " for more information.>>> import mysqldb
2, the use of MYSQLDB module
Import Mysqldbconn = MySQLdb.connect (host,user,passwd,dbname,port) cur = conn.cursor () cur.execute (' SELECT * FROM Mysql.user where user= "Shaw") print Cur.fetchone () # printout Cur.close () Conn.close ()
Import Mysqldbtry:conn = MySQLdb.connect (host,user,passwd,dbname,port) cur = conn.cursor () cur.execute (' SELECT * From Mysql.user where user= "Shaw" ') print Cur.fetchone () # printout cur.close () Conn.clos E () except Mysqldb.error,e:print ' MySQL Error Msg: ', E
Python module ' mysqldb '