#-*-coding:utf-8-*-" "Created on 2015-03-19mysql database connection class" "ImportMySQLdbclassDboperate:dbhandle=None#establish and connect to the database system defConnect (self): Self.dbhandle= MySQLdb.connect ("localhost","username","Password","dbname" ) #get operation Cursor defcursor (self):Try: returnself.dbhandle.cursor ()except(Attributeerror, Mysqldb.operationalerror): Self.connect ()returnself.dbhandle.cursor ()defcommit (self):returnSelf.dbhandle.commit ()#Close Connection defClose (self):returnself.dbhandle.close ()#execute a single-line SQL statement defExecute (SELF,SQL): Conn=self.dbhandle.cursor () conn.execute (SQL)#Execute multiple Data defExecutemany (Self,sql, TMP): Conn=self.dbhandle.cursor () conn.executemany (SQL, TMP)#querying a few rows of SQL defQuerymany (self,sql,size): Conn=self.dbhandle.cursor () conn.execute (SQL) rows=conn.fetchmany (size)returnrows#query 1 rows of SQL defQueryone (SELF,SQL): Conn=self.dbhandle.cursor () conn.execute (SQL) rows=Conn.fetchone ()returnrows#querying all row SQL defQueryall (SELF,SQL): Conn=self.dbhandle.cursor () conn.execute (SQL) rows=Conn.fetchall ()returnRows
Python Connection Database