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.1.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.1-cp313-cp313-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.13Windows x86-64

sqlcycli-2.3.1-cp313-cp313-musllinux_1_2_x86_64.whl (19.2 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

sqlcycli-2.3.1-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.1-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.1-cp312-cp312-win_amd64.whl (5.9 MB view details)

Uploaded CPython 3.12Windows x86-64

sqlcycli-2.3.1-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.1-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.1-cp312-cp312-macosx_10_13_universal2.whl (8.6 MB view details)

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

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

Uploaded CPython 3.11Windows x86-64

sqlcycli-2.3.1-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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

sqlcycli-2.3.1-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.1-cp310-cp310-win_amd64.whl (6.0 MB view details)

Uploaded CPython 3.10Windows x86-64

sqlcycli-2.3.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: sqlcycli-2.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 542a78fad58b607ca76b7c44029e18e26b51708da933d0cdd08b5945f4a717ba
MD5 a2c76b6cafa57414fca2ebd7e0641553
BLAKE2b-256 6be261d6a65de60f06b3d7e817f4a98bb5f3916cde7bf89aad9c70a2b6f711a2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcycli-2.3.1-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.12

File hashes

Hashes for sqlcycli-2.3.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9caa8a495bace229f75e02ddb75ff2f0c27e0bbed887249dd030cb259f0dc487
MD5 52cce0908009188a0b97503846d17284
BLAKE2b-256 2d459d3fa411bd75f07ade42c7cad215fc5cc36c03c12b61e776160680684d67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eaeba58759ef1fc5d6f4a5db63d19325e84a59ffaf924af0dc91296dd6cfe55a
MD5 a1023b476155dc67efb3915c74abf3b8
BLAKE2b-256 1b350ba8998547c9c707cc5c2606c2692d0f509b65e511666e3d103b17851c0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78c566671352845b481cfb7e18c6d19202f9c22ef1e37e194fe2677548f313b0
MD5 34d0e996cb439f55504cef0bfbcf203a
BLAKE2b-256 71bd9e1f22bb5513c40879e72e3a9e9ea55ba079d16425c3d9c6ccbe08f86896

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 1578bb2577c75500b34119debf0525b3bee37e281eb425a64990f1ab7993cd50
MD5 5e9e0467b25957d48f56bb27c773c716
BLAKE2b-256 4eff9e8b1ad27609ab6c0c6416cd646080ce693a437d695035166f9742d225b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcycli-2.3.1-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.12

File hashes

Hashes for sqlcycli-2.3.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8eef79fd01fc453dd3990622309f1c2809edd26e703f287bd231312cea37a2eb
MD5 df39692702952c233db0c1e4d0b706f3
BLAKE2b-256 36506c7172213c28594fad8e917fed8a0aad24d204a4572e322938f1ff2bc01d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2e8a1ccf2bf7e7469da607b67688c273cfaf2fa8b33fe1683dca5f460eda319e
MD5 868cc0126182eace92ef6b26fc9f1058
BLAKE2b-256 aeebb096c4c1aafc9c616a0450e64fceee49c68ae8f77a223bf3f68acb356531

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ee2d36d45b56061366cda87d679443ee9689dc97fe1c33e01a402840d5811b3e
MD5 0b8abc5420bf0c779b88481d2e35e4eb
BLAKE2b-256 c5fb1144974a20bf92defc65c4f5a27b3d44401dde8051f1a54ad9ad565992c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 ec4f33ac7c46792db0088e0395040886d1c5945b398ce9b19adbe5795f472411
MD5 943f9092e1becc719e0a6521f51bf9ff
BLAKE2b-256 d3cec68f0b974df41af6c32f96aa6f80e37ff6f2b2b16777714ec27219beefb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcycli-2.3.1-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.12

File hashes

Hashes for sqlcycli-2.3.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5d8dca0194c1cbb60cf03bcbace61024322bf8cba6d6c7d6a299be13ff5a107c
MD5 6da7512bb70ffef6e089e299153502c8
BLAKE2b-256 2d42bc804d058f48556d3acd327d58a8e7506eed165fa8cb75cb17134c8d6570

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 05a7ffeb748ce45e126eee28e2c1dd81b7c071a6f8211143850077b25089bab1
MD5 e831cdef32edd345a9bf0e46199e67d8
BLAKE2b-256 4adc187a14b145f054feecd260eeccbb2c1faa39339bc732df08f157fb1060fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f20d0cc6916524309929e6d0ae30968f12c00e2f766ac42c7a2ea705247f1069
MD5 da0fc1a43aa5c358fe73874704fe4e59
BLAKE2b-256 a684ad3db01a9a5d98eea28a20e95f358db83499ef0c0568056fb7138956bdd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 e921b9eafeb8594219f5f09d7f0b94ab866eecd7d471a3ea3fd4b3ba028e063f
MD5 74b7b37d7fdd683e84b0a7fe7a2e106c
BLAKE2b-256 85dfc02441665e8c9068a1aea49557ca40051f754b59ecc60066b30bfb860d5e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcycli-2.3.1-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.12

File hashes

Hashes for sqlcycli-2.3.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 50c8bdfe989a2c20a822047ade11b78b3cc65b5d02301ae23c1fd4fd97df24fe
MD5 e926c9356254ba617c8424e0935b8587
BLAKE2b-256 67e57a1851db0901441b8467dceff3d604132b1f5a1d37441448a6ba1c7c37da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8b4eee330365163b947ca8b9e76711531eca0bb58414a1c850183f4100a6a0e2
MD5 0119df1ed0e09391890fd74d8fbbc79d
BLAKE2b-256 361e4baa7dd5ade2c1f85bbbef5b3e16e5351c9227811dd1498d15339d1e206c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3571439f87374f53b219a33eabb1cfe07da7cf7cc0eeede21941cfa190a46b51
MD5 341e0214577fac4d50463b27de51192c
BLAKE2b-256 9bfcb9893e26df8d4277676496fae11bec0d8e70acd18760de784c09903ce158

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.3.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7add4bb1ce2a13ddcc8237665e45c1675001a3a321f336f75faafdf6945f75ec
MD5 0efecaea9402e20ba5db3dfb3541e35b
BLAKE2b-256 4520f8db49d1668b551bb4b97d8fcb1462edd29edac0258e35e7b7b3818193c0

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