Python operations on the MySQL database
#!/usr/bin/python#-*- coding: utf-8 -*-import mysqldbclass mysql: def __init__ (self,sql,host= ' 127.0.0.1 ', username= ' root ', password= ' root ', dbname= ' dbname '): self.username=username self.password=password self.dbname=dbname self.sql=sql mysql.db= MySQLdb.connect (self.host,self.username,self.password,self.dbname,charset= "UTF8") #查询操作 def query (self): try: cursor=mysql.db.cursor () cursor.execute (Self.sql) d Ata=cursor.fetchall () return Data mysql.db.close () except Exception as e: print e #插入操作 def insert (self): try: cursor=mysql.db.cursor () cursor.execute (Self.sql) Mysql.db.commit () mysql.db.close () return ' OK ' except Exception as e: print e #删除操作 def delete (self): try: cursor=mysql.db.cursor () cursor.execute (Self.sql) mysql.db.commit () Mysql.db.close () except Exception as e: print e #修改操作 def update (self): try: cursor=mysql.db.cursor () &nbSp; cursor.execute (Self.sql) Mysql.db.commit () mysql.db.close () except Exception as e: print eif __name__== "__main__":p
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1845184
Python operations on the MySQL database