一个简单的MySQLdb封装类(3.2),mysqldb封装类3.2,[Python]代码__
一个简单的MySQLdb封装类(3.2),mysqldb封装类3.2,[Python]代码__
[Python]代码
__author__ = 'yang'import MySQLdbclass MySQLHelper: def __init__(self,host,user,password,charset="utf8"): self.host=host self.user=user self.password=password self.charset=charset try: self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.password) self.conn.set_character_set(self.charset) self.cur=self.conn.cursor() except MySQLdb.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) def selectDb(self,db): try: self.conn.select_db(db) except MySQLdb.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) def query(self,sql): try: n=self.cur.execute(sql) return n except MySQLdb.Error as e: print("Mysql Error:%s\nSQL:%s" %(e,sql)) def queryRow(self,sql): self.query(sql) result = self.cur.fetchone() return result def queryAll(self,sql): self.query(sql) result=self.cur.fetchall() desc =self.cur.description d = [] for inv in result: _d = {} for i in range(0,len(inv)): _d[desc[i][0]] = str(inv[i]) d.append(_d) return d def insert(self,p_table_name,p_data): for key in p_data: p_data[key] = "'"+str(p_data[key])+"'" key = ','.join(p_data.keys()) value = ','.join(p_data.values()) real_sql = "INSERT INTO " + p_table_name + " (" + key + ") VALUES (" + value + ")" #self.query("set names 'utf8'") return self.query(real_sql) def getLastInsertId(self): return self.cur.lastrowid def rowcount(self): return self.cur.rowcount def commit(self): self.conn.commit() def close(self): self.cur.close() self.conn.close()
相关内容
- Python通过ctypes调用windows api函数ShellExecute,ctypesshellex
- 同一库中,表结构类似的两个表的数据复制,结构,同一
- secure crt 脚本,securecrt,#$language =
- e-hentai抓取图片第二版,,[Python]代码im
- 输出乘法表到excel,输出乘法表excel,[Python]代码im
- python 基础学习第二弹:类属性和实例属性,python实例
- 淘宝面试题猜数字游戏,淘宝试题猜数字,guess.py#!/u
- darkBing SQL扫描器0.1,darkbing扫描器0.1,[Python]代码#!
- 深度优先遍历嵌套容器,深度历嵌套容器,[Python]代码
- mysql基本操作类,mysql基本操作,db.pyimport
评论关闭