Skip to main content

A simple thread safe sql executor for Python with connection pool. Support MySQL, PostgreSQL, SQLite etc.

Project description

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

.. code::

   import sqlexecutorx as db

   if __name__ == '__main__':
      db.init('test.db', driver='sqlite3', debug=True)

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

      # or
      db.init(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 = db.save(select_key, 'INSERT INTO person(name, age) VALUES(%s,%s)', 'wangwu', 38)

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

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

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

      db.close()

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

.. code::

   from sqlexecutorx import with_transaction, transaction

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


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

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

sqlexecutorx-1.7.0.tar.gz (13.2 kB view details)

Uploaded Source

File details

Details for the file sqlexecutorx-1.7.0.tar.gz.

File metadata

  • Download URL: sqlexecutorx-1.7.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for sqlexecutorx-1.7.0.tar.gz
Algorithm Hash digest
SHA256 9854b8b38373291e80e1c5f5f427d449ae749db92c92c7a006cb82671c46a3d6
MD5 50041e99837bb1e906dcf47784cc11bb
BLAKE2b-256 f5cf54ef5171cc5fb6ae96a48df016de699021be3e531bfd6755ccc824e1d721

See more details on using hashes here.

Supported by

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