Skip to main content

Fast MySQL driver build in Cython (Sync and Async).

Project description

Fast MySQL driver build in Cython (Sync and Async).

Created to be used in a project, this package is published to github for ease of management and installation across different modules.

Installation

Install from PyPi

pip install sqlcycli

Install from github

pip install git+https://github.com/AresJef/SqlCyCli.git

Requirements

  • Python 3.10 or higher.
  • MySQL 5.5 or higher.

Features

  • Written in Cython for optimal performance (especially for SELECT/INSERT query).
  • All classes and methods are well documented and type annotated.
  • Supports both Sync and Async connection to the server.
  • API Compatiable with PyMySQL and aiomysql.
  • Support conversion (escape) for most of the native python types, and objects from libaray numpy and pandas. Does NOT support custom conversion (escape).

Benchmark

The following result comes from benchmark:

  • Device: MacbookPro M1Pro(2E8P) 32GB
  • Python: 3.12.4
  • PyMySQL: 1.1.1
  • aiomysql: 0.2.0
  • asyncmy: 0.2.9
# Unit: second | Lower is better
name        type    rows    insert-per-row  insert-bulk select-per-row  select-all
SQLCyCli    sync    50000   2.314604        0.352408    2.303785        0.045072
PyMySQL     sync    50000   2.564615        0.453932    5.005772        0.321882
SQLCyCli    async   50000   3.470987        0.349488    4.227539        0.112310
aiomysql    async   50000   3.655537        0.435786    5.882189        0.337962
asyncmy     async   50000   3.694391        0.414223    5.372794        0.278912
# Unit: second | Lower is better
name        type    rows    update-per-row  update-all  delete-per-row  delete-all
SQLCyCli    sync    50000   2.438695        0.463743    2.239835        0.129764
PyMySQL     sync    50000   2.728665        0.352855    2.415529        0.128943
SQLCyCli    async   50000   3.591613        0.536130    3.452353        0.134043
aiomysql    async   50000   3.678571        0.352699    3.567383        0.134994
asyncmy     async   50000   3.852939        0.321264    3.615992        0.134843

Usage

Use connect() to create a connection (Sync or Async) with the server.

import asyncio
import sqlcycli

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "password"

# Connection (Sync & Async)
async def test_connection() -> None:
    # Sync Connection - - - - - - - - - - - - - - - - - -
    with sqlcycli.connect(HOST, PORT, USER, PSWD) as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT 1")
            assert cur.fetchone() == (1,)

    # Connection closed
    assert conn.closed()

    # Async Connection - - - - - - - - - - - - - - - - -
    async with sqlcycli.connect(HOST, PORT, USER, PSWD) as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            assert await cur.fetchone() == (1,)

    # Connection closed
    assert conn.closed()

if __name__ == "__main__":
    asyncio.run(test_connection())

Use create_pool() to create a Pool for managing and maintaining connections (Sync or Async) with the server.

import asyncio
import sqlcycli

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "password"

# Pool (Context: Connected)
async def test_pool_context_connected() -> None:
    async with sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1) as pool:
        # Pool is connected: 1 free connection (min_size=1)
        assert not pool.closed() and pool.free == 1

        # Sync Connection - - - - - - - - - - - - - - - - - -
        with pool.acquire() as conn:
            with conn.cursor() as cur:
                cur.execute("SELECT 1")
                assert cur.fetchone() == (1,)

        # Async Connection - - - - - - - - - - - - - - - - -
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                assert await cur.fetchone() == (1,)

    # Pool closed
    assert pool.closed() and pool.total == 0

# Pool (Context: Disconnected)
async def test_pool_context_disconnected() -> None:
    with sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1) as pool:
        # Pool is not connected: 0 free connection (min_size=1)
        assert pool.closed() and pool.free == 0

        # Sync Connection - - - - - - - - - - - - - - - - - -
        with pool.acquire() as conn:
            with conn.cursor() as cur:
                cur.execute("SELECT 1")
                assert cur.fetchone() == (1,)

        # Async Connection - - - - - - - - - - - - - - - - -
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 1")
                assert await cur.fetchone() == (1,)
        # 1 free async connection
        assert pool.free == 1

    # Pool closed
    assert pool.closed() and pool.total == 0

if __name__ == "__main__":
    asyncio.run(test_pool_context_connected())
    asyncio.run(test_pool_context_disconnected())

Use the Pool class to create a Pool instance. Must close manually.

import asyncio
import sqlcycli

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "password"

# Pool (Instance: Connected)
async def test_pool_instance_connected() -> None:
    pool = await sqlcycli.create_pool(HOST, PORT, USER, PSWD, min_size=1)
    # Pool is connected: 1 free connection (min_size=1)
    assert not pool.closed() and pool.free == 1

    # Sync Connection - - - - - - - - - - - - - - - - - -
    with pool.acquire() as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT 1")
            assert cur.fetchone() == (1,)

    # Async Connection - - - - - - - - - - - - - - - - -
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            assert await cur.fetchone() == (1,)

    # Close pool manually
    await pool.close()
    assert pool.closed() and pool.total == 0


# Pool (Instance: Disconnected)
async def test_pool_instance_disconnected() -> None:
    pool = sqlcycli.Pool(HOST, PORT, USER, PSWD, min_size=1)
    # Pool is not connected: 0 free connection (min_size=1)
    assert pool.closed() and pool.free == 0

    # Sync Connection - - - - - - - - - - - - - - - - - -
    with pool.acquire() as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT 1")
            assert cur.fetchone() == (1,)

    # Async Connection - - - - - - - - - - - - - - - - -
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT 1")
            assert await cur.fetchone() == (1,)
    # 1 free async connection
    assert pool.free == 1

    # Close pool manually
    await pool.close()
    assert pool.closed() and pool.total == 0

if __name__ == "__main__":
    asyncio.run(test_pool_instance_connected())
    asyncio.run(test_pool_instance_disconnected())

Use the sqlfunc module to escape values for MySQL functions.

import asyncio
import datetime
import sqlcycli
from sqlcycli import sqlfunc

HOST = "localhost"
PORT = 3306
USER = "root"
PSWD = "Password_123456"

# SQLFunction
def test_sqlfunction() -> None:
    with sqlcycli.connect(HOST, PORT, USER, PSWD) as conn:
        with conn.cursor() as cur:
            cur.execute("SELECT %s", sqlfunc.TO_DAYS(datetime.date(2007, 10, 7)))
            # SQLFunction 'TO_DAYS()' escaped as: TO_DAYS('2007-10-07')
            assert cur.executed_sql == "SELECT TO_DAYS('2007-10-07')"
            assert cur.fetchone() == (733321,)

    # Connection closed
    assert conn.closed()

if __name__ == "__main__":
    test_sqlfunction()

Acknowledgements

SQLCyCli is build on top of the following open-source repositories:

SQLCyCli is based on the following open-source repositories:

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

sqlcycli-2.2.0.tar.gz (3.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sqlcycli-2.2.0-cp313-cp313-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.13Windows x86-64

sqlcycli-2.2.0-cp313-cp313-musllinux_1_2_x86_64.whl (19.0 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

sqlcycli-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

sqlcycli-2.2.0-cp313-cp313-macosx_10_13_universal2.whl (8.4 MB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

sqlcycli-2.2.0-cp312-cp312-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.12Windows x86-64

sqlcycli-2.2.0-cp312-cp312-musllinux_1_2_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

sqlcycli-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

sqlcycli-2.2.0-cp312-cp312-macosx_10_13_universal2.whl (8.5 MB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

sqlcycli-2.2.0-cp311-cp311-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.11Windows x86-64

sqlcycli-2.2.0-cp311-cp311-musllinux_1_2_x86_64.whl (19.3 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

sqlcycli-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

sqlcycli-2.2.0-cp311-cp311-macosx_10_9_universal2.whl (8.5 MB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

sqlcycli-2.2.0-cp310-cp310-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.10Windows x86-64

sqlcycli-2.2.0-cp310-cp310-musllinux_1_2_x86_64.whl (18.0 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

sqlcycli-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

sqlcycli-2.2.0-cp310-cp310-macosx_10_9_universal2.whl (8.5 MB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file sqlcycli-2.2.0.tar.gz.

File metadata

  • Download URL: sqlcycli-2.2.0.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcycli-2.2.0.tar.gz
Algorithm Hash digest
SHA256 bb3b31096ad7c0a72b52f259b197c06c7b685379142b0635e961eb5429c8d927
MD5 a204e4a4b88ef9ad31b348420877c1f3
BLAKE2b-256 1d668e718acbdc19e46827dfe980f08d7835da20533a11056ecd66ac2ddc0a4a

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: sqlcycli-2.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 5.9 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcycli-2.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b715920a73a207fe9d17ce59f9356f575ed923a84820a7d1a41928ff382eefdb
MD5 20b22a9554fbfcc33d4f122dc1471c39
BLAKE2b-256 c631b6c5085db62c322143212c1edc8020b71b81e2e1ae626ddfcf3da6d02cc4

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7616f73e8a1b1374d00654d29629ccbd6d5d1d41fcea46f6a1d58dbebbdbbc6f
MD5 729b649b484a6e7593126401de3f0cb9
BLAKE2b-256 d82c6c6468435cfda556ab62445a56f1ef7dd8bb7e394a6c31558ce4a00d9b59

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 061a6efbb02cc51fd60ef1cef3a97e3a2dfdd722cb8c856a2465411f0a957a23
MD5 2fe27f02a7390362bb3d89844f8985b8
BLAKE2b-256 2baeecab7460d8b127e5169b686be6606e25cc9150762d2fa0d4cd95b79f381c

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 c47f81e6da23fbb2c992d67e29d0e7f6040d6a94efa5ee2d5ac7d4b5ab19713d
MD5 07d941aa4b71b886c8a4b02424c1b46f
BLAKE2b-256 e09a670b66ca706672fa108586190720954f83ea4b3a2d59bba1d02b905c8fee

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: sqlcycli-2.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 5.9 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcycli-2.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f615a852e96273f901da4f25691c33f9c99ba2e03303aede2efe438ce11a0328
MD5 a9ca01a06e8ddf07d9ebb88d3ce2c616
BLAKE2b-256 f759c31f824ab9fc8f522b69e75d5e88bc7ba675ae238aa65cee116463f9f91a

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 269bc4d27faaa39676cdb0f7cdcbc9c001759aee526d730439fa9a2d8ff5fc5e
MD5 f14a984a12d5646c9b9e8e7eadffd3bd
BLAKE2b-256 f2b487054a57b93b5575889fc2847e316b5f02a0494fee3d85718bd518ff8bf2

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c44a3d1c3bb7258efacea3872fd48fb086eff14c3e01d467bad8f21a5d8b9507
MD5 81aa9cf0f1d723a6ff5e2c3cce401682
BLAKE2b-256 617847139cc0d434426f89db78c35c9281777736946f88fc8e3df708fd1de651

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 607c4ce3550aa86afdc8d7213c5fec51dcc18e59543824d378ad1537e5c949f1
MD5 ef427b6e01c38e22c85714a3d6422e7e
BLAKE2b-256 75672250c36b2aa940ad924ade934ea33aeed3488844cad01201b351aa6210da

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: sqlcycli-2.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 6.0 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcycli-2.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3d830757f2e8ddfa1c1ddc2114327f9d82b50d451125a7c6b74a8236ce59cce0
MD5 d298ac55466c5788bd626e4394a61e61
BLAKE2b-256 0193ad353af12d6c07e521b09206f6374d6ed1bca3f356543ff46d6488578359

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8cf9f3ee2655b01782b58fb23ac4119517d550f81bdaf97796ec9103bb6f0d96
MD5 0d2f331fe4f9eaa9c16497a21c6490e5
BLAKE2b-256 3cd4e2179cf8adf9171e63b6004662d638ee50964c7285596b54440351e5e9b4

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 edd2a302518378fbeb3e2e049e100b255738b67a10caef58d47b1ab644e2a221
MD5 e8de7b46e969b7f56294a74af8b40323
BLAKE2b-256 a4f7f17476475dad92060ab94e46fd75eb36198f0448101ea15dfee0d1435774

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 64e861086c0da3a417cbf126a179d5a3cc3d8b6e6b2befef4286c2d1684c26e7
MD5 3374cb78eec50c0bd5a611800825a27f
BLAKE2b-256 f5c99bdc3d51bca4ee70424299da900e142d1fa174fc56bcc89633a687c2766a

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: sqlcycli-2.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 6.0 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcycli-2.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3e03703dad93d517d5cbf2b1b9e72bd6570ed67860aa91130c6fe9737ba36898
MD5 6ee06dca3439004070c29ca953d68594
BLAKE2b-256 27edcab8179d2ac92f0c9813ecbe4a87bf10386492e4cdce644501c2931122c9

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 17c40a86292ad1a38ba63af3e53ef29a134ce46da58c94e8f9c85c73c770353b
MD5 f1911ec30ac20f90f824f66b9a078cc6
BLAKE2b-256 2c30555da369a831921f83ca37627e57e74399bae4af86053825cb85309ff69e

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fb7c4cc6f7226915ac7a54d6b61362c35b63776863a23bae0e2b820a7a1af1e8
MD5 e2ca9acaa93ee267d1de5378b4192aa6
BLAKE2b-256 2742e2c689ae1b751f2716c99600bd7eb1a156a5954fdc7edc16e0baf6aeb53b

See more details on using hashes here.

File details

Details for the file sqlcycli-2.2.0-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sqlcycli-2.2.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7fb80d5533c7659236532877fe9ed1dcdf3147229fd07b7c9ac238282a9bf699
MD5 e2a68e31a1f2b122895e654fa312ae0c
BLAKE2b-256 93835b2b432bbea2b27fde01e9041c2906f8066076277df2954f571ab7058309

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