微爬(Wepy)python-Mysql操作

发布:2018-08-07 08:07   点击247次   评论:0

import copy
from core import argv
#import pymysql, sqlite3, pymssql ... # 动态加载: 

def edb(key):
    cfgs = config.init()
    cdb = dict(copy.deepcopy(cfgs['cdb']), **cfgs[key])
    return dbm(cdb)

class dbm:
    def __init__(self, cfgs={}):
        cfgs = cfgs if cfgs else argv.cfgs['cdb'] #g.cdb
        self.cfgs = cfgs
        self.dbcon()

    def dbcur_comm(self, cfgs):
        drive = __import__(cfgs['type'])
        host = cfgs['host']; user = cfgs['user']; pwd = cfgs['pass']; name = cfgs['name']
        self.con = drive.connect(host=host, user=user, password=pwd, database=name, charset="utf8")
        self.cur = self.con.cursor(cursor=drive.cursors.DictCursor)

    def dbcur_sqlite3(self, cfgs):
        self.con = __import__('sqlite3').connect(cfgs['name'])
        def cur_dict(cursor, row): 
            d = {} 
            for idx, col in enumerate(cursor.description): 
                d[col[0]] = row[idx] 
            return d
        self.con.row_factory = cur_dict
        self.cur = self.con.cursor()

    def dbcon(self):
        cfgs = self.cfgs
        func = 'dbcur_' + cfgs['type']
        if func in dir(self):
            method = getattr(self, func) 
            method(cfgs)
        else:
           self.dbcur_comm(cfgs)
        if not self.cur:
            print('Database Error!')
            #raise(NameError, "Database Error!")

    def close(self):
        self.cur.close()
        self.con.close()

    def table(self, tbname, sql=1):
        pre = self.cfgs['tbpre']
        fix = self.cfgs['tbfix']
        if sql: # {base_catalog}
            return tbname.replace('{', pre).replace('}', fix)
        else:
            return pre + tbname + fix

    def get(self, sql, parms=(), re=None): # re = 1,n,None
        cur = self.cur
        cur.execute(self.table(sql), parms)
        if not re:
            res = cur.fetchall()
        elif re==1:
            res = cur.fetchone()
        else:
            res = cur.fetchmany(re)
        return res

    def exe(self, sql, parms=(), mod=None):
        cur = self.cur
        if type(parms) == list :
            res = cur.executemany(self.table(sql), parms)
        else:
            res = cur.execute(self.table(sql), parms)
        self.con.commit()
        return res

    def script(self, sql, parms=''):
        self.exe(sql)





其中cdb 配置片段为:


[cdb]
type = pymysql
host = localhost
name = txext_main
port = 3306
user = peace
pass = 123456
tbpre = 
tbfix = _ys
# type : pymysql, sqlite3, pymssql
# name : txmao_main, 
# 127.0.0.1, localhost, 


更多参考:微爬(Wepy)

http://imcat.txjia.com/chn.php?topic.2018-3j-g9b1




关于 GitHub 导航 部门 反馈

提示:`/home.php`入口数据仅为演示功能,不构成任何交易凭证,也不承担相关风险和责任!

Copyright © 2011-2018 xxxxx.com All rights reserved.

Run:2.205/28.690(ms); 7(sql)/2.89(MB); comm:news/detail; Upd:2024-11-15 16:24:21