标签:.com python param get init one created none ctc
import MySQLdb import statConfig import pymysql # MySQLdb和 pymysql 任选一个 需要安装 pymysql,mysqlclinet,pymysql任何一个 class mysqlHelper: _conn = None def __init__(self,confdic=None): if not confdic: confdic = statConfig.statConfig() self._conn = pymysql.connect(host=confdic.getnode("host"),user=confdic.getnode("user"),passwd=confdic.getnode("pwd"),db=confdic.getnode("db"),port=int(confdic.getnode("port")),charset="utf8") def close(self): self._conn.close() def ExecuteNoneQuery(self,sql,param): #sql = "insert into user(name,created) values(%s,%s)" #param = ("aaa",int(time.time())) cmd = self._conn.cursor() n = cmd.execute(sql,param) self._conn.commit() cmd.close() return n def Query(self,sql): cmd = self._conn.cursor(cursor=pymysql.cursors.DictCursor) #指定返回的结果是dic类型 n = cmd.execute(sql) rows = cmd.fetchall() cmd.close() return rows
调用:
import mysqlHelper db=mysqlHelper.mysqlHelper() sql="insert into users(name,address) value(%s,%s)" par=("张三","ddd") db.ExecuteNoneQuery(sql,par) querysql="select * from users where name=‘张三‘" query=db.Query(querysql) for x in query: print(x["name"])
标签:.com python param get init one created none ctc
原文地址:https://www.cnblogs.com/wlzhang/p/10281920.html