zoukankan      html  css  js  c++  java
  • python的dbutil

    目录机构如下:

     dbutil代码如下:

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import configparser
    import pymysql
    
    class dbutil:
        # dbsection为配置文件中的section
        def __init__(self,dbsection):
            self._conn=self.dbConn(dbsection)
            if(self._conn):
                self._cursor=self._conn.cursor()
        def dbConn(self,dbsection):
            #读取db.ini文件
            cf = configparser.ConfigParser()
            cf.read("../config/db.ini")
            dbhost = cf.get(dbsection,"host")
            dbport = cf.getint(dbsection,"port")
            dbuser = cf.get(dbsection,"user")
            dbpassword = cf.get(dbsection,"password")
            dbname = cf.get(dbsection,"dbname")
            dbcharset = cf.get(dbsection,"charset")
    
            #开始数据库连接
            try:
                conn = pymysql.Connect(host=dbhost,port=dbport,user=dbuser,passwd=dbpassword,db=dbname,charset=dbcharset)
            except:
                print("连接失败 host=%s,port=%d,user=%s,dbpassword=%s,db=%s"%(dbhost,dbport,dbuser,dbpassword,dbname))
                conn=False
            return conn
    
        # 获取查询结果集
        def fetch_all(self, sql):
            res = ''
            if (self._conn):
                try:
                    self._cursor.execute(sql)
                    res = self._cursor.fetchall()
                except Exception as data:
                    res = False
                    print("query database exception, %s" % data)
            return res
    
        # 执行更新语句
        def update(self, sql):
            flag = False
            if (self._conn):
                try:
                    self._cursor.execute(sql)
                    self._conn.commit()
                    flag = True
                except Exception as data:
                    flag = False
                    print("update database exception, %s" % data)
            return flag
    
        # 关闭数据库连接
        def close(self):
            if (self._conn):
                # print(type(self._conn)=='object')
                try:
                    # if (type(self._cursor) == 'object'):
                    self._cursor.close()
                    # if (type(self._conn) == 'object'):
                    self._conn.close()
                except Exception as data:
                    print("close database exception, %s,%s,%s" % (data, type(self._cursor), type(self._conn)))
    
    
    if __name__=="__main__":
        dbutil=dbutil("biz_mysql")
        res=dbutil.fetch_all("select * from wm_people limit %d"%(2))
        dbutil.close()
        print(len(res))
        for dd in res:
            print(dd[0])

    对应的db.ini如下

    [biz_mysql]
    host = 10.15.17.xxx
    port = 3306
    user = xxx
    password = 123456
    dbname = xxx
    charset = utf8
    
    
    [data_mysql]
    host = 10.15.17.xxx
    port = 3306
    user = biz
    password = 123456
    dbname = xxx
    charset = utf8
  • 相关阅读:
    单例模式-静态内部类方式
    单例模式-懒汉式(双重检验)
    单例模式-懒汉式
    sonarqube7.2版本web api简析
    sonarqube集成maven插件,上传扫描结果
    sonarQube快速入门7.2版本下载pmd插件,并设置只使用pmd规则
    gradle使用dokka插件出现Exception while loading package-list from ExternalDocumentationLinkImp
    让linux后解压的资源权限就是777(工作笔记)
    java并发 无锁cas的最简单理解
    java 必须要懂点的包 今日研究小结
  • 原文地址:https://www.cnblogs.com/zipon/p/7590601.html
Copyright © 2011-2022 走看看