python3 mysql使用

apt-get install default-libmysqlclient-dev
pip install mysql
conda install mysql
pip install DBUtils==1.3
pip install pymysql

import pymysql
from DBUtils.PooledDB import PooledDB
import logging
#打开数据库连接
class MySQL:
    host = '47.75.158.180'
    user = 'vae'
    port = 3306
    password = 'vae123456789'
    db = 'movie'
    charset = 'utf8'
    #create database db_test default character set utf8 collate utf8_general_ci;
    pool = None
    limit_count = 3
    def __init__(self):
        pass
    
    def init_DB(self):
        try:
            self.pool = PooledDB(
                pymysql,
                self.limit_count,
                host=self.host,
                user=self.user,
                passwd=self.password,
                db=self.db,
                port=self.port,
                charset=self.charset,
                use_unicode=True)
        except Exception as e:
            logging.error("mysql init_DB error %s" % e) 

    def select(self, sql):
        try:
            logging.debug("mysql exec sql:  %s" % sql)
            conn = self.pool.connection()
            cus = conn.cursor()
            count = cus.execute(sql)
            fc = cus.fetchall()
            return fc            
        except e:
            logging.error("mysql select table  error %s" % e) 
            return None
        finally:
            cus.close()
            conn.close()
    def selectByParam(self, sql,param):
        try:
            logging.debug("mysql exec sql:  %s" % sql)
            conn = self.pool.connection()
            cus = conn.cursor()
            count = cus.execute(sql, param)
            fc = cus.fetchall()
            return fc            
        except e:
            logging.error("mysql select table  error %s" % e) 
            return None
        finally:
            cus.close()
    def update2ByParam(self, sql, params,sql1,params1):
        try:
            logging.debug("update2ByParam sql: %s, %s"% (sql,params))
            logging.debug("update2ByParam sql: %s, %s"% (sql1,params1))
            conn = self.pool.connection()
            cus = conn.cursor()
            count = cus.execute(sql, params)
            count1 = cus.execute(sql1, params1)
            conn.commit()
            return count, count1            
        except e:
            conn.rollback()
            logging.error("mysql update by param  table  error %s" % e) 
            return None
        finally:
            cus.close()
    def updateByParam(self, sql, params):
        try:
            logging.debug("updateByParam sql: %s, %s"% (sql,params))
            conn = self.pool.connection()
            cus = conn.cursor()
            count = cus.execute(sql, params)
            conn.commit()
            return count            
        except e:
            conn.rollback()
            logging.error("mysql update by param  table  error %s" % e) 
            return 0
        finally:
            cus.close()

    def update(self, sql):
        try:
            logging.debug("mysql update sql:  %s" % sql)
            conn = self.pool.connection()
            cus = conn.cursor()
            count = cus.execute(sql)
            conn.commit()
            return count            
        except e:
            conn.rollback()
            logging.error("mysql update table  error %s" % e) 
            return 0
        finally:
            cus.close()
            cus.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值