码迷,mamicode.com
首页 > 数据库 > 详细

python-db配置文件

时间:2019-09-26 23:52:06      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:char   port   配置文件   dict   igp   def   pwd   exce   import   

#!/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配置文件

标签:char   port   配置文件   dict   igp   def   pwd   exce   import   

原文地址:https://www.cnblogs.com/monkeybron/p/11594901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!