温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python2使用mysqldb讲义

发布时间:2020-04-26 17:00:29 来源:亿速云 阅读:218 作者:三月 栏目:软件技术

本文主要给大家介绍,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在亿速云行业资讯里面关注我的更新文章的。

PyMySQL 是在 Python3.x 版本中用于连接 MySQL 云服务器的一个库,Python2中则使用mysqldb
话不多说,直接上代码来说明用途


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小时售前售后,随时帮您解答问题的。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI