An opinionated sqlite3 wrapper.
Project description
mysqlite
An opinionated sqlite3 wrapper.
The main goal of this package is simply to have stmt.execute()
return bool
instead of a cursor object.
So that it can be useful to indicate True
if the query succeeds, or False
otherwise.
Empty results on INSERT
, UPDATE
, DELETE
, and SELECT
are considered as False
.
It adopts the prepare - execute pattern merely for the user experience.
Synchronous usage
from mysqlite import Database
db = Database('example.db')
def main():
try:
db.connect(timeout=30)
stmt = db.prepare(
'CREATE TABLE IF NOT EXISTS users ('
' id INTEGER PRIMARY KEY,'
' name TEXT NOT NULL,'
' age INTEGER NOT NULL'
');'
)
stmt.execute() # True
stmt = db.prepare('SELECT * FROM users')
stmt.execute() # False, no rows yet!
stmt = db.prepare('SELECT * FROM user')
stmt.execute() # False, no such table!
stmt = db.prepare(
'INSERT INTO users (name, age) VALUES (?, ?)'
)
stmt.execute(['Alice']) # False
stmt.execute(['Alice', 30]) # True
stmt = db.prepare('SELECT * FROM users LIMIT 10')
stmt.execute() # True
row = stmt.fetch()
while row:
print('*', row['name'], row['age'])
row = stmt.fetch()
finally:
db.close()
if __name__ == '__main__':
main()
Asynchronous usage
Asyncronous usage is powered by awaiter.
The following example uses an asynchronous context manager, although the try - finally approach as above can still be used.
import asyncio
from mysqlite import AsyncDatabase
async def main():
async with AsyncDatabase('example.db') as db:
stmt = db.prepare(
'CREATE TABLE IF NOT EXISTS users ('
' id INTEGER PRIMARY KEY,'
' name TEXT NOT NULL,'
' age INTEGER NOT NULL'
');'
)
await stmt.execute(timeout=30)
stmt = db.prepare(
'INSERT INTO users (name, age) VALUES (?, ?)'
)
await stmt.execute(['Alice', 30])
stmt = db.prepare('SELECT * FROM users LIMIT 10')
await stmt.execute()
row = await stmt.fetch()
while row:
print('*', row['name'], row['age'])
row = await stmt.fetch()
if __name__ == '__main__':
asyncio.run(main())
Install
python3 -m pip install --upgrade mysqlite
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mysqlite-0.0.0.tar.gz
.
File metadata
- Download URL: mysqlite-0.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a16016d13eaf48cd79b13e05835b05c864ae548141464e7fe3479b97b54f9a48 |
|
MD5 | 104b75d5b4fc2c1664b71f165c54b853 |
|
BLAKE2b-256 | c91e627d25e640f83ec778b2637b01135283dfb830abe64355d1d2c174bb4f95 |
File details
Details for the file mysqlite-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: mysqlite-0.0.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfe096e9e4290403376b0455ad673e6573a5dd32aace8b4d0c8f4e4b22a1486c |
|
MD5 | b7dcd204f450c6efad71a674a0f5a64f |
|
BLAKE2b-256 | 48cc0aecaa3b7d6a18beb1924df88d61d18d5e134eac760bf7ecef432e70c91b |