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.3.0.tar.gz (3.9 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.3.0-cp313-cp313-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.13Windows x86-64

sqlcycli-2.3.0-cp313-cp313-musllinux_1_2_x86_64.whl (19.1 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

sqlcycli-2.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

sqlcycli-2.3.0-cp313-cp313-macosx_10_13_universal2.whl (8.5 MB view details)

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

sqlcycli-2.3.0-cp312-cp312-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.12Windows x86-64

sqlcycli-2.3.0-cp312-cp312-musllinux_1_2_x86_64.whl (19.3 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

sqlcycli-2.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

sqlcycli-2.3.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.3.0-cp311-cp311-win_amd64.whl (6.1 MB view details)

Uploaded CPython 3.11Windows x86-64

sqlcycli-2.3.0-cp311-cp311-musllinux_1_2_x86_64.whl (19.5 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

sqlcycli-2.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

sqlcycli-2.3.0-cp311-cp311-macosx_10_9_universal2.whl (8.6 MB view details)

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

sqlcycli-2.3.0-cp310-cp310-win_amd64.whl (6.1 MB view details)

Uploaded CPython 3.10Windows x86-64

sqlcycli-2.3.0-cp310-cp310-musllinux_1_2_x86_64.whl (18.2 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

sqlcycli-2.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

sqlcycli-2.3.0-cp310-cp310-macosx_10_9_universal2.whl (8.6 MB view details)

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

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.3.0.tar.gz
Algorithm Hash digest
SHA256 331ef3c104a110b1d4edc9c67a7b4ff3f8a559def5777d9c84877504b43f8b29
MD5 7e7ebaa7310f44c989db7949338ef809
BLAKE2b-256 91b453cd8498561a9dfa5dc168ede961b69bdedbc61deb980b07d953bcef5754

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4c2a28444b712b96984779ec63a3a76383f216c034b7b78f53c873c7b90a7650
MD5 a0413499d9638aa52b9549269a05995e
BLAKE2b-256 ec49555ffe763d0bed21f5911cedbd46f2f6bd8eb63c50167b29bc92e92195ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d94409ced0add2dd83fd5ca62d2656c8676700d094d1c12d7b5a8ac70836386d
MD5 57e661d77ed0e66c45040046d09ec816
BLAKE2b-256 f809b0e0dea8ef75882b9f15127daf3292212e535413fb0a8f19859bdcef76ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5456a17c30e55ead77afababb9a25e5d4f7b0181929012c335d539144a52ebad
MD5 f2ebd6390705bd2d3a07784fa3378125
BLAKE2b-256 f107a1d1f54e46ca0091b3f55071b326130b600d6dcef4bc50b461c6da113046

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 6810e74acc035816bbfd64843206051f6e94b9adc4fa15871f9db47b91fc7881
MD5 b7a80f43630509616ec512fbd6e2b77e
BLAKE2b-256 a58c7a4a918e7794953162edda0830c0a97280f3cf293e0c698d659c7b5af63b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3afa6e21275bf2cde34be1ea351009394e7e776181fd7e02ae332e3c0fcd7536
MD5 de86fcb0f12471dc27d53f6474a73e5a
BLAKE2b-256 b7793cb479d8b5878f76c3b8645deb1144cfe7f5a7627f51533b2b55d71cdcaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1d57411defe196ff8f3e8435a0c7574eea700a194cf4e60123f7676d7402d33b
MD5 60745efda0fcc44efd9fb4ee721631a6
BLAKE2b-256 948d2054083514ba34dfe72dfc5357fd4d7f89a5614ddfd6d58523b1d52ec501

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 76fe784ea4a8e0c5d6deb1f2fee07234aac97f95f3be4813578fc7d50b66e0de
MD5 3ede5ca5aa4ea5f29e84fb4a714dd05a
BLAKE2b-256 7bacb5834211ab4c43216ec8c659ae4df1d58dc6138ea29ddcba12c80eef5afa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 1eef35a1dfe7168e72f3db358eee196522bc268e8a0a3d79a748952e30f7ca99
MD5 37092e9ce17f995a21f219108e4cd0e8
BLAKE2b-256 b562db42d62891ce92e06ff661ebbc8dd685736527f5955e043661e5c9f29fdc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 57be6931d659a6aa09649a12e741f6710768566a93652c9f43d1e2e1a979ff78
MD5 7ced79c426951d01fb23842c40586e0f
BLAKE2b-256 c72357917ef7ac1ff20f6a2101d396d66fc673d6298d4e474870a2c1665476c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e1a44d2bfc16d97b96cc980bb1dec7d04e9fc6b176a9dc16cd4316f2dc20427
MD5 e589fba0ea88c1208362794978950ae2
BLAKE2b-256 1ea0732bd047bfda914feabcf8c1ccec25f2f7b8f860c9dcf9af9d409047e3c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a7c8128bd1af1e3f98aaf65cd115c753c45e64540a9133c02ba9d3e1ff6faad0
MD5 2bb8c84dbfc89348df65cc30a0d02e60
BLAKE2b-256 ffbe953572beff5992279ace2724f36c511a9b9eaf9baecda1979c1d3f1a486f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 08b7d61951d8327203fda3199c75c74a0c84c996d1941e7920ffe44e672d276c
MD5 a3ff25e1ba678e4dba91970389f266cb
BLAKE2b-256 bfa29aee4069281de503354f865c13d4959ee747ad52b9ceaebf50038f0921dc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 20bd766a353b840c3757f6932b0b9b62d5d25fa7139dc3d6dddabf8be3406561
MD5 df3937c90e4b743f6afc3d4f3fc637d4
BLAKE2b-256 d34d33466b78259bfd6739f7e2adcdc20520fd3c7b2ee41d44d710c43fcc099c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 81efa0c8a13a6fb70b50cee23d8522ac869d3f1266517ee1f06b75b1a6d6ee22
MD5 c9fae616a645ea7e61a95a773eb34374
BLAKE2b-256 4a7f21fac83ccde763b53375a1303c4f97dc7f1b5201c5419b4b037a951bddc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3d4bbff0d1eaabb522588c9ab896dbc42cd9752a4fb8097fdf361407484b0153
MD5 296d65a82a3e5fd972771b6d349b294c
BLAKE2b-256 83a0c35a3d11bea67aed1934d8174b10b202c9be0e74a3dd22403db26073dcc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 a7a903fa5057d86f5c6fdd3b4dea030ee8df393dae899ae248ce8c4a3762d963
MD5 00d213ba84a6117040f06ff20900fbca
BLAKE2b-256 a09441c651b547e11d4624e17146ead061ea2511c248a29c88a07fbe96695414

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