A simple database manager with sqlalchemy
Installation
python3 -m pip install sql_manager
Basic Usage
from sqlalchemy import Column, Integer, String
from sql_manager import DynamicModel, Manager
# create model
columns = {
'uid': Column(Integer, primary_key=True, comment='the unique identity'),
'name': Column(String(10), comment='the username', default='zoro')}
Data = DynamicModel('OnePiece', columns, 'user')
print(Data.get_table())
'''
+------+---------------------+-------------+-----------------------+
| Key | Comment | Type | Default |
+------+---------------------+-------------+-----------------------+
| uid | the unique identity | INTEGER | None |
| name | the username | VARCHAR(10) | ColumnDefault('zoro') |
+------+---------------------+-------------+-----------------------+
'''
data = Data(uid=1, name='luffy')
print(data)
'''
OnePiece <{'uid': 1, 'name': 'luffy'}>
'''
# insert one data
with Manager(Data, dbfile='test.db') as m:
m.insert(Data(uid=1, name='luffy'))
m.insert(dict(uid=2, name='zoro'))
'''
[2022-02-15 09:29:20 Manager insert DEBUG MainThread:98] >>> insert data: OnePiece <{'uid': 1, 'name': 'luffy'}>
[2022-02-15 09:29:20 Manager insert DEBUG MainThread:98] >>> insert data: OnePiece <{'uid': 2, 'name': 'zoro'}>
[2022-02-15 09:29:20 Manager __exit__ DEBUG MainThread:35] database closed.
'''
# insert multiple datas [slow for big data]
with Manager(Data, dbfile='test.db') as m:
datas = [Data(uid=uid, name=name) for uid, name in zip([3, 4, 5], ['sanji', 'chopper', 'nami'])]
m.insert(datas, key='uid')
'''
[2022-02-15 09:31:14 Manager insert DEBUG MainThread:98] >>> insert data: OnePiece <{'uid': 3, 'name': 'sanji'}>
[2022-02-15 09:31:14 Manager insert DEBUG MainThread:98] >>> insert data: OnePiece <{'uid': 4, 'name': 'chopper'}>
[2022-02-15 09:31:14 Manager insert DEBUG MainThread:98] >>> insert data: OnePiece <{'uid': 5, 'name': 'nami'}>
[2022-02-15 09:31:14 Manager __exit__ DEBUG MainThread:35] database closed.
'''
# insert bulk objects
with Manager(Data, dbfile='test.db') as m:
bulk_datas = [Data(uid=uid, name='demo') for uid in range(6, 100000)]
m.insert_bulk(bulk_datas)
'''
[2022-02-15 09:31:59 Manager insert_bulk DEBUG MainThread:114] >>> inserted 99994 objects ...
[2022-02-15 09:32:00 Manager __exit__ DEBUG MainThread:35] database closed.
'''
# insert bulk mappings
with Manager(Data, dbfile='test.db') as m:
bulk_datas = [dict(uid=uid, name='demo') for uid in range(100001, 200000)]
m.insert_bulk(bulk_datas)
'''
[2022-02-15 09:32:26 Manager insert_bulk DEBUG MainThread:117] >>> inserted 99999 mappings ...
[2022-02-15 09:32:26 Manager __exit__ DEBUG MainThread:35] database closed.
'''
# query, delete
with Manager(Data, dbfile='test.db') as m:
res = m.query('uid', 1)
print(res.all())
m.delete('uid', 2)
'''
[OnePiece <{'name': 'luffy', 'uid': 1}>]
[2022-02-15 09:55:27 Manager delete DEBUG MainThread:75] delete 1 row(s)
[2022-02-15 09:55:27 Manager __exit__ DEBUG MainThread:35] database closed.
'''
# use methods of raw session
with Manager(Data, dbfile='test.db') as m:
query = m.session.query(Data)
res = query.filter(Data.name.like('%op%')).limit(1)
print(res)
print(res.all())
'''
SELECT user.uid AS user_uid, user.name AS user_name
FROM user
WHERE user.name LIKE ?
LIMIT ? OFFSET ?
[OnePiece <{'name': 'chopper', 'uid': 4}>]
[2022-02-15 09:56:44 Manager __exit__ DEBUG MainThread:35] database closed.
'''
Document
Metadata
Release files for sql-manager 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sql_manager-1.0.5.tar.gz | 5.7 kB | Without provenance | Details for sql_manager-1.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sql_manager-1.0.5-py3-none-any.whl | Python 3 | none | any | Details for sql_manager-1.0.5-py3-none-any.whl |
Total release size: 11.1 kB
Release files / sql_manager-1.0.5.tar.gz
| Download URL | sql_manager-1.0.5.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be15833a5a7045a90ed738bb1dceb53fbfa2bab8c998815f832cf1fb6b0730d7
|
|
BLAKE2b-256 checksum How to use checksums |
bf0dd8d63ee6b3dab5faf77ec5534bef4639fffa1d725ad64bc60fdb51390886
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.3
|
Release files / sql_manager-1.0.5-py3-none-any.whl
| Download URL | sql_manager-1.0.5-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c1e34099f9676813e6f82557cc98e4100c1b65487f511fd10c1fcde0824521c2
|
|
BLAKE2b-256 checksum How to use checksums |
2eae4093979521af545cbe4a922f8fe335d453faabdf4f5e671298f037c6b471
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.3
|
Security for sql-manager 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.