项目作者: zebozhuang

项目描述 :
PySQL is a MySQL client library providing easy and convenient CURD operations without writing raw SQL.
高级语言: Python
项目地址: git://github.com/zebozhuang/pysql.git
创建时间: 2017-09-09T04:05:17Z
项目社区:https://github.com/zebozhuang/pysql

开源协议:

下载


PySQL

PySQL is a MySQL client library written in Python, it can make
CURD operations easy and convenient without writing raw SQL.

Tutorial

1. Configure

1.1. Configure in a toml file

  1. host = "127.0.0.1"
  2. port = 3306
  3. user = "root"
  4. passwd = "123123"
  5. charset = "utf8"
  6. db = "bar"
  7. mincached = 3
  8. maxcached = 3
  9. # maxshared = 0

Please install Python toml to read this file.

1.2. Configure in a dictionary form

  1. config = {
  2. "host": "127.0.0.1",
  3. "port": 3306,
  4. "user": "root",
  5. "passwd": "123123",
  6. "charset": "utf8",
  7. "db": "bar",
  8. "mincached": 3,
  9. "maxcached": 3,
  10. "maxshared": 0
  11. }

2. Instance

  1. from pysql.pool import SQLPool
  2. pool = SQLPool(**config)

3. Insert

  1. obj = {'name': 'abc', 'age': 10}
  2. insert_id = pool.insert(table='t1', obj=obj)
  1. objs = [
  2. {'name': 'a', 'age': 1},
  3. {'name': 'b', 'age': 2},
  4. {'name': 'c', 'age': 3},
  5. ]
  6. insert_ids = pool.insertmany(table='t1', objs=objs)

4. Update

obj is the updating data and where is query condition.

  1. Case: id = 3
  2. obj = {'age': 10}
  3. where = {'id': 3} or where = {'id__eq': 3}
  4. affected_rows = pool.update(table='t1', where=where, obj=obj)
  1. # Case: 12 <= id <= 20
  2. where = {'id__gte': 12, 'id__lte': 20}
  3. affected_rows = pool.update(table='t1', where=where, obj=obj)
  1. # Case: id != 4
  2. where = {'id__neq': 4}
  3. affected_rows = pool.update(table='t1', where=where, obj=obj)

5. Delete

  1. # Case: id in (1, 2, 3)
  2. affected_rows = pool.delete(table='t1', where={'id__in': [1, 2, 3]})

6. Select

  1. affected_rows = pool.query(table='t1', where={'id__in': [1, 2, 3]})