Python 对mysql数据库的操作

Python 对mysql数据库的操作

#!/usr/bin/python # -*- coding: utf-8 -*- import Mysqldb class 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         self.mysqldb = MySQLdb.connect(host, self.username, self.password, self.dbname, charset="utf8")     # 查询操作     def query(self):         try:             cursor = self.mysqldb.cursor()             cursor.execute(self.sql)             data = cursor.fetchall()             return data                      except Exception as e:             print e     # 插入操作     def insert(self):         try:             cursor = self.mysqldb.cursor()             cursor.execute(self.sql)             self.mysqldb.commit()             self.mysqldb.close()             return 'ok'         except Exception as e:             print e     # 删除操作     def delete(self):         try:             cursor = self.mysqldb.cursor()             cursor.execute(self.sql)             self.mysqldb.commit()             self.mysqldb.close()         except Exception as e:             print e     # 修改操作     def update(self):         try:             cursor = self.mysqldb.cursor()             cursor.execute(self.sql)             self.mysqldb.commit()             self.mysqldb.close()         except Exception as e:             print e if __name__=="__main__": pass