python-db配置文件

Posted monkeybron

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-db配置文件相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python27
# -*- encoding: utf-8 -*-

import sys
import ConfigParser
import MySQLdb
import MySQLdb.cursors

cf = ConfigParser.ConfigParser()
cf.read("/tmp/report/dbs.conf")
ouser = cf.get(client,user)
opwd = cf.get(client,password)
ohost = cf.get(client,host)
oport = cf.get(client,port)
odb = cf.get(client,database)

def mysqlLib(sql,host=ohost,port=oport,db=odb,user=ouser,pwd=opwd):
    try:
        con = MySQLdb.connect(host, user,pwd, db,charset="utf8");
        cur = con.cursor()
        cur.execute(sql)
        data = cur.fetchall()
        con.close()
        return data
    except Exception as e:
        print e



def mysqlDictLib(sql,host=ohost,port=oport,db=odb,user=ouser,pwd=opwd):
    try:
        con = MySQLdb.connect(host, user,pwd, db,cursorclass = MySQLdb.cursors.DictCursor,charset="utf8");
        cur = con.cursor()
        cur.execute(sql)
        data = cur.fetchall()
        con.close()
        return data
    except Exception as e:
        print e






if __name__ == __main__:
    sql = select name from members where id in (select member_id from member_tags where tags_id=199      ) limit 5 
    # db = mysqlLib(sql)
    db = mysqlDictLib(sql)
    print db[0]["name"]
[client]
database=xxx
user=xx
password=xx
port=3306
host=xxx

 

以上是关于python-db配置文件的主要内容,如果未能解决你的问题,请参考以下文章

python-DB模块实例

Python-DB接口规范

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置

VS Code配置markdown代码片段