Skip to main content

A simple database manager with sqlalchemy

Project description

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:
    data = Data(uid=1, name='luffy')
    m.insert(Data, 'uid', data)
'''
[2021-06-21 16:10:48 Manager insert DEBUG MainThread:95] >>> insert data: OnePiece <{'uid': 2, 'name': 'luffy'}>
[2021-06-21 16:10:48 Manager __exit__ DEBUG MainThread:34] database closed.
'''

# insert multiple datas
with Manager(Data, dbfile='test.db') as m:
    datas = [Data(uid=uid, name=name) for uid, name in zip([2, 3, 4], ['sanji', 'chopper', 'nami'])]
    m.insert(Data, 'uid', datas)
'''
[2021-06-21 16:19:08 Manager insert DEBUG MainThread:95] >>> insert data: OnePiece <{'uid': 2, 'name': 'sanji'}>
[2021-06-21 16:19:08 Manager insert DEBUG MainThread:95] >>> insert data: OnePiece <{'uid': 3, 'name': 'chopper'}>
[2021-06-21 16:19:08 Manager insert DEBUG MainThread:95] >>> insert data: OnePiece <{'uid': 4, 'name': 'nami'}>
[2021-06-21 16:19:08 Manager __exit__ DEBUG MainThread:34] database closed.
'''

# query, delete
with Manager(Data, dbfile='test.db') as m:
    res = m.query(Data, 'uid', 1)
    print(res.all())
    m.delete(Data, 'uid', 2)    
'''
[OnePiece <{'uid': 1, 'name': 'luffy'}>]
[2021-06-21 16:19:36 Manager delete DEBUG MainThread:76] delete 1 row(s)
[2021-06-21 16:19:36 Manager __exit__ DEBUG MainThread:34] database closed.
'''


# other origin methods
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 <{'uid': 3, 'name': 'chopper'}>]
[2021-06-21 16:24:07 Manager __exit__ DEBUG MainThread:34] 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.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

sql_manager-1.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sql_manager-1.0.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

File hashes

Hashes for sql_manager-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ec1f34d5b96a54f0de9df222899ebdbf28d86301bdd8a7a22db44a2017c6466b
MD5 5f161d3ae897849121d27fac6d1d5626
BLAKE2b-256 19a9b35423cbaaba4bf43cfd33ee135c8269e9db916532691f75f70e869b92a2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sql_manager-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

File hashes

Hashes for sql_manager-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d2e6161edbba4bc34be7178b76350acd49c0ab9ee1c980a0543673099f136b2e
MD5 2b7fd17b61ea4f0ef9ad4005e1885401
BLAKE2b-256 ecd8322c31ea42c48fb4ae6f4f27fb2d137612189781a63e09e081fae88901a9

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