python之数据库db模块

本文详细介绍了Python中用于数据库操作的db模块,包括如何连接数据库、执行SQL语句、处理查询结果以及关闭数据库连接。通过实例演示了基本的CRUD操作,适合初学者快速掌握Python数据库编程。
摘要由CSDN通过智能技术生成
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import  time, threading, uuid, logging,functools

class Dict(dict):
    def __init__(self, names=(), **kw):
        super(Dict, self).__init__(**kw)
        for k, v in zip(names, values):
            self[k] = v 

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'dict' object has no attribute'%s' " % key)

    def __setattr__(self, key, value):
        self[key] = value

def next_id(t=None):
    if t is None:#
        t = time.time() 
    return '%015d%s000' % (int(t * 1000), uuid.uuid4().hex)

def _profiling(start, sql =''):
    t = time.time() - start
    if t > 0.1:
        logging.warning('[PROFILING] [DB] %s: %s' % (t,sql))
    else:
        logging.info('[PROFILING] [DB] %s: %s' %(t, sql))

class DEBrror(Exception):
    pass

class MulticolumnsError(DBError):
    pass

class _LasyConnection(object):

    def __init__(self):
        self.connection = None

    def cursor(self):
        if self.connection is None:
            connection = engine.connect() 
            logging.info('open connection <%s>...' % hex(id(connection)))
            self.connection = connection
        return self.connection.cursor() 

    def commit(self):
        self.connection.commit() 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值