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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

sqlcycli-2.1.1-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.1.1-cp313-cp313-macosx_10_13_universal2.whl (8.4 MB view details)

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

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

sqlcycli-2.1.1-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.1.1-cp312-cp312-macosx_10_13_universal2.whl (8.5 MB view details)

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

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

sqlcycli-2.1.1-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.1.1-cp311-cp311-macosx_10_9_universal2.whl (8.5 MB view details)

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

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

sqlcycli-2.1.1-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.1.1-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.1.1.tar.gz.

File metadata

  • Download URL: sqlcycli-2.1.1.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.1.1.tar.gz
Algorithm Hash digest
SHA256 a07efaa1028bb2c665ec2dbf5ffc621f9cc2af07abcd0af4994745148e9810ab
MD5 bbe8a0b7ffa74797fb6df15d9cb06a1f
BLAKE2b-256 524554b9c3684c257966f7874273111897289f7b4aeb85a69407e1c40fe64ef0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9ce78ebf42061f1a9ff47e07ca3f2d02fd500b5491d9ab7740af1d56da312158
MD5 774f54a815032b7189d5b741c3e94f59
BLAKE2b-256 5565589162810ba81cad1fb15dfdf4111daa29302032fa22c14390a598b607be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f08ed209bfa7e6a6a09dbc0f88ea208c00dfa4c39b0080eb0ca6ce6b8cde704c
MD5 52ef359fcb5280e6c4320a1622c96894
BLAKE2b-256 511dc904221b51cbc35e0fecc96f795f26c648e9c7a9bf041f364090fabd1eb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 96dc2490ce62611f56f3f2c173f2899fb503b2dd6666acaa6bab3d3268fa0f86
MD5 78bc445d683a9f639c88354063242a42
BLAKE2b-256 b58a49103aca0fe8eeb1b88aa6580f19a190bfe0e42d3c4102ffb632f7576c71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 9b2ca32d99fb23181a37e75f3ad891af805352818ee55d4f35cd260b52a8b199
MD5 9ce0a0d46a6b36270f1abb3202bc8fb5
BLAKE2b-256 7a5cd195e835a5ccfa4682f154945d8bb7510ffe5b771e8600ee0baf7ff10506

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4b1040dca70904ba8b1bc0dca520c582ae077a1c8a13767077fcdfb4072ab9a8
MD5 c5f6b8238ecd62ba0903468a3786e37a
BLAKE2b-256 b54c04f0d332625c4833a53640629ed862346b6c082250b1ec671e9b4ca5143b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c9d7f740a0593772175d1574fcd33ff8244961bbc54ab522ba87058660269e54
MD5 9c5375d43e28b1f4ade58a18f1dc9d5d
BLAKE2b-256 c8df5cdd7b47f30d701a9402998b0555a9c24ac9697c2c47bf2029e7e50f74df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7e2d59fb75b961f0c5850eee2b918dcbdec193d0904d632c87daad350260cb24
MD5 d6cbfada676db7a962e4999bcf6ade2f
BLAKE2b-256 298c0fd2173c9186c442066c8afc8f012e971a7e4c66904f0d4b1faf630e1af4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 483ec6add34f17d5c1e6e81230a7ec3ed90ad5ee8b6cd4e4bf4468b8b8c7d654
MD5 3a01ae482b65d188f1305e8aa1fa9780
BLAKE2b-256 97a08ea0ae74b80710a22c7f7aa3032c2f95cba0febde27f0fdea6f3ae938ea0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ce1ef3f99d584210fa549f2ba892522c2f5e085ddfda5f83320dfeaac8ee0770
MD5 2999a9190d6445684284f80df1283322
BLAKE2b-256 2f9b18488aadc9b546aaaef4ab574666ba46bd53ac60707f8057c1e32342cec9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5a1b1d9bbcd7e69600cb00bd267547b792d7503bb8d4871d29186e4b92056a53
MD5 b975476459236c09fd838eaec286d6ed
BLAKE2b-256 d5411255ef4cf793e4ee23fb2fb06fd7c2ff8639eef907f0ba7799fb9a9d0209

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c504ed4222dc283df466aaa3544552d5dd28b9a49b2ece7ef0bd54d30ec69b61
MD5 895779f13e9f67b3384fdfec4b1ab90f
BLAKE2b-256 eea59fad8bfed8be93446602618df9c92b8667420c6dc5ea32900a09d95fd1d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 53b314821cfc9c7a5bb91e0e5014e722b305fa72f4e587422703c097da351592
MD5 a5427625e5b805b1a68a3534dfa787ab
BLAKE2b-256 20a190a2bf13c18b5b544fd080395ed893624bc20afb2d693e6fc8bd0ef2b274

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqlcycli-2.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6bd1450026eba42e51e65a75d0addf69e58ae7529c1633014416cc50fefafe1f
MD5 97dff90bad611591a3b37ef05110f2cc
BLAKE2b-256 ef58e9c681ce5acd6a8e95308559a963809445793f52011413a73ad2f14bd271

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 96a1495bd24e490c2a5b193b3006675d6b6ed4eee5439b4d238d184c30c5eb37
MD5 c3644717ec4aae3853a30f8cbfb580f3
BLAKE2b-256 a9298494d83f82f3c5a7d243bda8d16738d7b616cbfbb4d45d1ef52ddb403a4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9efe8370fd8826be3905fd29af6b2a5e4d4fc94e4da18aae0532f14c9d645b19
MD5 70bbf1c23797971ff40c251d7adaf764
BLAKE2b-256 6a3715cee5e8c2d8c56f2022c4315725cea84edfaf89570b6aa1cedad6f0ad8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlcycli-2.1.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9837cb710d2f619561798ebd1dd559fcd8fedbe15d65ee0664ab8159e928e423
MD5 d719c27e223ff5b9103b944d702bfae2
BLAKE2b-256 badfb5220c9646939953babbad51256548a281401547e52a6f1466704622aa50

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