栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pythonsqlite3总结_python3 sqlite3?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pythonsqlite3总结_python3 sqlite3?

环境需求:需要sqlite3和数据库连接池的第三方库DBUtils。

读懂代码你需要了解:

1.单例设计模式

2.Python上下文管理器

模板代码:

from dbutils.persistent_db import PersistentDB
import sqlite3


class Pool(object):       # 数据库连接池
    __pool = None     # 记录第一个被创建的对象引用
    config = {
        'database': 'D:/software/SQLite3/mySQLite.sqlite3'  # 数据库文件路径
    }

    def __new__(cls, *args, **kwargs):
        """创建连接池对象  单例设计模式(每个线程中只创建一个连接池对象)  PersistentDB为每个线程提供专用的连接池"""
        if cls.__pool is None:    # 如果__pool为空,说明创建的是第一个连接池对象
            cls.__pool = PersistentDB(sqlite3, maxusage=None, closeable=False, **cls.config)
        return cls.__pool


class Connect:
    def __enter__(self):
        """自动从连接池中取出一个连接"""
        db_pool = Pool()
        self.conn = db_pool.connection()
        self.cur = self.conn.cursor()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """自动释放当前连接资源 归还给连接池"""
        self.cur.close()
        self.conn.close()


# 查询一条数据
def find_one(table, data):
    sql = 'select * from ' + table + ' where ip=?'
    with Connect() as db:   # 从连接池中取出一个连接
        db.cur.execute(sql, (data,))    # sqlite用元组接受字符串
        result = db.cur.fetchone()
    return result[0]


# 插入一条数据
def insert_one(table, data):
    sql = 'insert or ignore into ' + table + '(ip) values(?)'  # 不重复插入
    with Connect() as db:
        db.cur.execute(sql, (data,))
        db.conn.commit()


# 删除一条数据
def delete_one(table, data):
    sql = 'delete from ' + table + ' where ip=?'
    with Connect() as db:
        db.cur.execute(sql, (data,))
        db.conn.commit()


"""
SQLite 由于线程安全机制 不支持 PooledDB 线程共享连接模式   故使用PersistentDB 线程专用连接模式 为每个线程单独开连接池
SQLite 只支持读并发 即:写独占,读共享,所以要在修改操作时使用互斥锁。 为了体现精简性,这里就不演示了
PooledDB()中的参数解释自行查找
"""

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号