Skip to main content

A simple database manager with sqlalchemy

Project description

PyPI GitHub last commit

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

https://sql-manager.readthedocs.io/en/latest/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql_manager-1.0.5.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

sql_manager-1.0.5-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file sql_manager-1.0.5.tar.gz.

File metadata

  • Download URL: sql_manager-1.0.5.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for sql_manager-1.0.5.tar.gz
Algorithm Hash digest
SHA256 be15833a5a7045a90ed738bb1dceb53fbfa2bab8c998815f832cf1fb6b0730d7
MD5 93e6221ade707498101ee6c74cf7de6c
BLAKE2b-256 bf0dd8d63ee6b3dab5faf77ec5534bef4639fffa1d725ad64bc60fdb51390886

See more details on using hashes here.

File details

Details for the file sql_manager-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: sql_manager-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for sql_manager-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e34099f9676813e6f82557cc98e4100c1b65487f511fd10c1fcde0824521c2
MD5 ec3e4e9a240f188f16461072b18e2a78
BLAKE2b-256 2eae4093979521af545cbe4a922f8fe335d453faabdf4f5e671298f037c6b471

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page