Python2使用mysqldb讲义
本文主要给大家介绍,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在创新互联行业资讯里面关注我的更新文章的。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站制作、巴州网络推广、成都小程序开发、巴州网络营销、巴州企业策划、巴州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供巴州建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 云服务器的一个库,Python2中则使用mysqldb
话不多说,直接上代码来说明用途
#!/user/bin/env python
#coding=utf-8
from pymysql import connect,cursors
from pymysql.err import OperationalError
import os
import configparser as cparser
from builtins import int
from framework.logger import Logger
import time
'''
========读取config.ini文件中mysql配置========
'''
base_dir = str(os.path.dirname(os.path.dirname(file)))
file_path = base_dir + "\config\config.ini"
cf = configUtil(file_path)
host = cf.get("sitmysqlconf", "host")
port = cf.get("sitmysqlconf", 'port')
db = cf.get("sitmysqlconf", 'db_name')
user = cf.get("sitmysqlconf", 'user')
password = cf.get("sitmysqlconf", 'password')
logger = Logger(logger="mysqlUtils").getlog()
'''
===========封装MySQL基本操作=============
'''
class mysqlUtils:
def __init__(self):
'''
初始化获得mysql连接
'''
try:
self.conn = connect(host=host,
port=int(port),
user=user,
password=password,
db=db,
charset='utf8mb4',
cursorclass=cursors.DictCursor
)
except OperationalError as e:
print (e)
def cursor(self):
'''
获得游标
'''
self.conn.cursor()
def getDict(self,tableName,systemID,ColumnNameKey,ColumnNameValue):
'''
公共方法,获取id的字典
'''
with self.conn.cursor() as cursor:
cursor.execute("select * from %s WHERE system_id = %s and %s = %s",(tableName,systemID,ColumnNameKey,ColumnNameValue))
Dict = cursor.fetchone()
self.conn.commit()
return Dict
def AttentionLibraryDelete(self,system_id,merchant_id):
'''非正常删除数据,即直接操作数据库删除'''
with self.conn.cursor() as cursor:
cursor.execute("delete from tableName where system_id = %s and merchant_id = %s;",(system_id,merchant_id))
self.conn.commit()
def addMerchantTOIT(self,merchant_id):
'''把商家关联到XXX行业中'''
#realSQL = "INSERT INTO tableName (system_id, merchant_id, business_id, status, creator_id, create_date, updater_id, update_date) VALUES ('7b6a99f3bce14915863cde5104bdf2c3', %s, '11', 'A', '8', unix_timestamp(now())*1000, '8', unix_timestamp(now())*1000);" % repr(merchant_id)
with self.conn.cursor() as cursor:
cursor.execute("INSERT INTO t_sys_merchant_business (system_id, merchant_id, business_id, status, creator_id, create_date, updater_id, update_date) VALUES ('7b6a99f3bce14915863cde5104bdf2c3', %s, '11', 'A', '8', unix_timestamp(now())*1000, '8', unix_timestamp(now())*1000);",(merchant_id))
self.conn.commit()
logger.info('把商家【%s】关联到xxx成功'%merchant_id)
def close(self):
'''
关闭mysql数据库
'''
self.conn.close()
看了以上关于Python2使用mysqldb讲义,希望能给大家在实际运用中带来一定的帮助。本文由于篇幅有限,难免会有不足和需要补充的地方,如有需要更加专业的解答,可在官网联系我们的24小时售前售后,随时帮您解答问题的。
当前名称:Python2使用mysqldb讲义
当前路径:http://azwzsj.com/article/jeipjd.html