rapptz / asqlite Goto Github PK
View Code? Open in Web Editor NEWA simple async wrapper for sqlite3
License: MIT License
A simple async wrapper for sqlite3
License: MIT License
await db.backup(target)
Makes a backup of db
to the target
database.
target
can be a Connection object.
Does it have any differences when compared to aiosqlite?
Why is all your module code in init.py? Why is pep-8 not honored in your code?
Current required Python in setup.py
is 3.5.3+
AsyncContextManager
was added in 3.5.4/3.6.2Literal
in 3.8I am unsure if this is intended or not, but Pylance notices the return type of fetchone()
and fetchall()
is hard set to a <'sqlite3.Row'>
and list[<'sqlite3.Row'>]
respectively.
But when using a query that is valid and a parameter that isn't in the tables column values; the fetchone()
returns a None<class 'NoneType'>
async with asqlite.connect(database="path") as conn:
_exists: Row | None = await conn.fetchone(f"""SELECT * FROM table WHERE column= ?""", (value,))
print(_exists)
if _exists is not None:
print(_exists.keys())
if _exists is None:
# The line below get's marked "unreachable" by the linter.
print(type(_exists))
I simply added | None
to the return type for the overloads for fetchone()
to solve my local issue.
After a particular attempt at helping someone over in the discord.py server where I noticed what was essentially
# global scoped
conn = asyncio.run_until_complete(asqlite.connect(...))
client = ...
# lots of other stuff which references conn in definitions, but doesn't run yet
# other library's code creating a new event loop and running all the stuff defined above.
client.run(...)
Suffice to say, this caused an issue for the user.
Not sure what the most elegant solution here would be.
Obviously, requiring use as a context manager solves it, but that has some ergonomic changes that may be a non-starter for some people.
Could check the event loop vs the running loop, but this requires a threading lock if done from the thread side: (see the global threading lock where asyncio compares event loops across threads introduced as part of: python/cpython#86558), which would add a non-insignificant overhead.
Could also just document this as a misuse, favoring ergonomics and performance in this case.
it would be very helpful
This doesn't work:
async def main():
async with asqlite.connect("test.sqlite3") as conn:
async with conn.cursor() as cur:
print((await cur.execute('SELECT * FROM stats')).fetchone())
asyncio.run(main())
# sqlite3.ProgrammingError: SQLite objects created in a thread
# can only be used in that same thread. The object was created
# in thread id 7764 and this is thread id 34968.
while this does:
async def main():
async with asqlite.connect("test.sqlite3") as conn:
async with conn.execute('SELECT * FROM stats') as cur:
print(await cur.fetchone())
asyncio.run(main())
@thegamecracks suggested in the discord server:
asqlite.Cursor.execute()
returns sqlite3.Cursor
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.