Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import aiosqlite
import asyncio

class SQLite3:
def __init__(self, name, table):
class SQLite:
def __init__(self, name, table, loop=None):
aiosqlite.core.LOG.disabled = True
self.loop = loop
self.table = table
self._path = os.path.join(os.path.dirname(os.path.abspath(__file__)), name)
self._queue = asyncio.Queue(maxsize=1)
Expand All @@ -22,16 +23,16 @@ def _queueInit(self, queue):
except:
pass

async def execute(self, sql, write=False):
async def _execute(self, sql, write=False):
value = db = cursor = field = None
try:
if write is True:
await self._queue.get()
db = await aiosqlite.connect(database=self._path, check_same_thread=False, timeout=60)
db = await aiosqlite.connect(database=self._path, check_same_thread=False, timeout=60, loop=self.loop)
cursor = await db.execute(sql)
await db.commit()
else:
db = await aiosqlite.connect(database=self._path, check_same_thread=False, timeout=60)
db = await aiosqlite.connect(database=self._path, check_same_thread=False, timeout=60, loop=self.loop)
cursor = await db.execute(sql)
if cursor.description:
field = [i[0] for i in cursor.description]
Expand All @@ -46,29 +47,34 @@ async def execute(self, sql, write=False):
if db is not None:
await db.close()
if write is True:
try:
self._queue.task_done()
except:
pass
await self._queue.put(None)
del field, cursor, db
return value

async def Init(self, tbl='(row0 text primary key, row1 text, row2 text)'):
async def Init(self, tbl):
try:
vacuum = await self.execute('''PRAGMA auto_vacuum;''')
vacuum = await self._execute('''PRAGMA auto_vacuum;''')
if vacuum[0]['auto_vacuum'] == 0:
await self.execute('''PRAGMA auto_vacuum = 1;''', True)
await self.execute('''PRAGMA encoding = "UTF-8";''', True)
await self.execute('''PRAGMA journal_mode = WAL;''', True)
await self.execute('''PRAGMA temp_store = MEMORY;''', True)
await self.execute('''PRAGMA synchronous = NORMAL;''', True)
await self.execute('''PRAGMA page_size = 4096;''', True)
create_tbl = '''create table %s %s''' % (self.table, tbl)
await self.execute(create_tbl, True)
await self._execute('''PRAGMA auto_vacuum = 1;''', True)
await self._execute('''PRAGMA encoding = "UTF-8";''', True)
# await self._execute('''PRAGMA journal_mode = WAL;''', True)
await self._execute('''PRAGMA temp_store = MEMORY;''', True)
await self._execute('''PRAGMA synchronous = NORMAL;''', True)
# await self._execute('''PRAGMA page_size = 4096;''', True)
create_tbl = '''create table ''' + self.table + ''' ({})'''.format(tbl)
await self._execute(create_tbl, True)
except Exception as e:
if 'already exists' not in str(e):
print("init:", e)


if __name__ == "__main__":
SQL = SQLite3("MoeClub.db", "MoeClub")
loop = asyncio.get_event_loop()
loop.run_until_complete(SQL.Init())

asyncio.run(SQL.execute("select * from MoeClub;"))
SQL = SQLite("MoeClub.db", "MoeClub", loop=loop)
loop.run_until_complete(SQL.Init('row0 text primary key, row1 text, row2 text'))
loop.run_until_complete(SQL.execute("select * from MoeClub;"))