This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/core/db.py
2014-03-06 11:45:00 +13:00

26 lines
624 B
Python

import os
import sqlite3
import _thread
threaddbs = {}
def get_db_connection(conn, name=''):
"""returns an sqlite3 connection to a persistent database"""
if not name:
name = '{}.db'.format(conn.name)
threadid = _thread.get_ident()
if name in threaddbs and threadid in threaddbs[name]:
return threaddbs[name][threadid]
filename = os.path.join(bot.data_dir, name)
db = sqlite3.connect(filename, timeout=10)
if name in threaddbs:
threaddbs[name][threadid] = db
else:
threaddbs[name] = {threadid: db}
return db
bot.get_db_connection = get_db_connection