Skip to main content

A thread safe sql executor for Python with connection pool. It helps you automatically manage connections and transactions.

Project description

Usage Sample ''''''''''''

.. code:: python

import executor

if name == 'main': executor.init_db('test.db', driver='sqlite3', debug=True)

   # or
   executor.init_db("postgres://user:password@127.0.0.1:5432/testdb", driver='psycopg2', pool_size=5, debug=True)

   # or
   executor.init_db(host='127.0.0.1', port='5432', user='xxx', password='xxx', database='testdb', driver='psycopg2')

   # if driver is 'pymysql' or 'mysql.connector' of MySQL, the select_key is 'SELECT LAST_INSERT_ID()'
   select_key = "SELECT currval('person_id_seq')"

   id = executor.save(select_key, 'INSERT INTO person(name, age) VALUES(%s,%s)', 'wangwu', 38)

   persons = executor.select('select id, name, age from person')
   # result:
   # (3, 'zhangsan', 15)
   # (4, 'lisi', 26)
   # (5, 'wangwu', 38)
   # (6, 'zhaoliu', 45)

   persons = executor.select_one('select id, name, age from person where name = %s', 'zhangsan')
   # result:
   # (3, 'zhangsan', 15)

   args = [
    ('李四', 55),
    ('王五', 35),
   ]
   executor.batch_execute('INSERT INTO person(name,age) VALUES(%s,%s)', *args)

   executor.close()

Transaction '''''''''''

.. code:: python

from executor import with_transaction, transaction

@with_transaction def test_transaction(): insert_func(....) update_func(....)

def test_transaction2(): with transaction(): insert_func(....) update_func(....)

If you want to operate MySQL database like Mybatis, may be you need MySqlx: https://pypi.org/project/mysqlx

If you want to operate PostgreSQL database like Mybatis, may be you need PgSqlx: https://pypi.org/project/pgsqlx

If you want to execute SQL like Mybatis, may be you need sqlx-batis: https://pypi.org/project/sqlx-batis

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

sqlx-executor-0.0.1.tar.gz (11.4 kB view hashes)

Uploaded Source

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