Skip to main content

pymysql_kits is a tool based on PyMySQL. It can quickly establish a database connection pool and conveniently execute SQL statements.

Project description

pymysql_kits

pymysql_kits is a tool based on PyMySQL. It can quickly establish a database connection pool and conveniently execute SQL statements.

Contributing:

Requirements

  • Python – one of the following:
    • CPython : 2.7 and >= 3.4
    • PyPy : Latest version
  • MySQL Server – one of the following:
    • MySQL >= 5.5
    • MariaDB >= 5.5
  • PyMySQL >= 0.8.1

Installation

pip install pymysql-kits

Example

The following examples make use of a simple table

CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(255) COLLATE utf8_bin NOT NULL,
    `password` varchar(255) COLLATE utf8_bin NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;

PyMySQLConnectionPool Demo

from pymysql_kits.pooling import PyMySQLConnectionPool

db_conf = {
    "host": "localhost",
    "user": "user",
    "password": "passwd",
    "database": "db",
    "port": 3306,
    "autocommit": True,
    "charset": "utf8mb4",
}

# Create connection pool
db_pool = PyMySQLConnectionPool(pool_size=5, pool_name='local_pool', **db_conf)

# Get a connection
connection = db_pool.get_connection()
try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('webmaster@python.org',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

Connections and Transaction Demo

from pymysql_kits import Connections

db_conf = {
    "host": "localhost",
    "user": "user",
    "password": "passwd",
    "database": "db",
    "port": 3306,
    "autocommit": True,
    "charset": "utf8mb4",
}

# Create a connection poll
conn = Connections(pool_size=5, **db_conf)

# Query
result = conn.fetchall("SELECT `id`, `password` FROM `users` WHERE `email`=%s", ('webmaster@python.org',))
print(result)

# Insert
conn.insert("INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)", ('webmaster@python.org', 'very-secret'))

# For transaction
transaction = conn.begin()
try:
    transaction.insert("INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)", ('webmaster@python.org', 'very-secret'))
except:
    transaction.rollback()
else:
    transaction.commit()
finally:
    transaction.close()

# or
with conn.begin() as transaction:
    transaction.insert("INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)", ('webmaster@python.org', 'very-secret'))

License

pymysql_kits is released under the MIT License. See LICENSE for more information.

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

pymysql_kits-0.1.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

pymysql_kits-0.1.4-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pymysql_kits-0.1.4.tar.gz.

File metadata

  • Download URL: pymysql_kits-0.1.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for pymysql_kits-0.1.4.tar.gz
Algorithm Hash digest
SHA256 afa34abfaaed47bec81eddcd2837d8ada3b7582a392a2d184eb43b72037643a7
MD5 0f143f5aec68b0208845dd3b82941b5f
BLAKE2b-256 b1af54a9cf2b550f908e738ccf50b280c08adeb428ba4c989e38b69bf9adb930

See more details on using hashes here.

File details

Details for the file pymysql_kits-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: pymysql_kits-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for pymysql_kits-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aadcdc054db10f7738b391f08bf2200268e6e6500807733f43c3702545b0bca1
MD5 3a26ccc9f4df47fd6219747da8a0ba07
BLAKE2b-256 1890a42118f9092c772d20e8f51a271a9f0d8f9109cf0dde4178f89f68cda746

See more details on using hashes here.

Supported by

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