Python connects MySQL to connector there are many, we choose MYSQLDB
Let Python support MySQLdb module
#pip2.7 Install Mysql-python
See if the modules available with python2.7 are present MySQLdb
# ipythonwarning: ipython history requires sqlite, your history will not be savedPython 2.7.11 (default, mar 10 2016, 09:45:30) type "Copyright", "credits" or "License" for more information. Ipython 4.1.2 -- an enhanced interactive python.? -> introduction and overview of ipython ' s features.%quickref -> quick reference.help -> python ' S own help system.object? -> details about ' object ', use ' object?? ' for extra details. In [1]: help (' modules ') please wait a moment while i gather a list of all available modules ... Mysqldb calendar marshal
4. Simple package module, Test no problem
script-name:mysql-conn.py
#!/usr/bin/python27#-*-coding:utf-8-*-import mysqldb //import MySQL driver native module class mysqldb ():d EF __init__ (self,host= ' 192.168.17.1 ', port=4306,user= ' root ', passwd= ' Zcy '): //constructor Set initial value Self.host = hostself.port = portself.user = userself.passwd = passwdself.conn1 = mysqldb.connect (HOST=SELF.HOST,PORT=SELF.PORT,USER=SELF.USER,PASSWD=SELF.PASSWD) // The instance begins with the connection and cursor properties Self.cur1 = self.conn1.cursor () Def find (self,field,db,table): Define a SELECT query statement "" "in MySQL field -> query fielddb -> query databasetable -> query table "" "self.field = fieldself.db = dbself.table = Tableself.cur1.execute (' select ' + self.field + ' from ' + self.db + '. ' + self.table) Return self.cur1.fetchall () Def createdb (self,db): // Build Library Self.cur1.execute ('create database if not exists ' + db) def createtable (self,db,table): //Build Table self.conn1.select_db (db) Self.cur1.execute (" create table ' + table + ' (Id int (5) not null primary key auto_increment,name char (Ten) not null,phone varchar ( not null,class) char (()); ") Def insert (self,db,table,*l): //Insert Data self.cur1.execute (' insert into ' + db + '. ' + table + ' values (null,%s,%s,%s) ' , *l) Self.conn1.commit ()
This article is from the "zcy.gy" blog, make sure to keep this source http://1064187464.blog.51cto.com/9108437/1786181
Simple Package MYSQLDB module operation MySQL Database