Github: https://github.com/PyMySQL/PyMySQL
PyMySQL
このパッケージには純粋なPythonのMySQLクライアントライブラリが含まれています。 PyMySQLの目標は、MySQLdbのドロップイン置換であり、CPython、PyPy、IronPythonで動作することです。
注:PyMySQLは低レベルのAPIをサポートしていません_mysqlはdata_seek、store_result、およびuse_resultのように提供しています。 PEP 249で定義された上位レベルのAPIを使用する必要があります。 しかし、 PEP 249はユースケースをカバーしていないため、自動コミットやpingなどのAPIがサポートされています。
要件
- Python – 以下のいずれか:
- CPython > = 2.6または> = 3.3
- PyPy > = 4.0
- IronPython 2.7
- MySQLサーバ – 以下のいずれか:
インストール
最後の安定したリリースはPyPIで利用可能で、 pip
でインストールすることができます:
$ pip install PyMySQL
ドキュメンテーション
ドキュメンテーションはオンラインで入手できます: http : //pymysql.readthedocs.io/
サポートについては、 StackOverflowを参照してください。
例
次の例では、単純なテーブルを使用しています
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 ;
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
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()
次の例では、
{'password': 'very-secret', 'id': 1}
リソース
DB-API 2.0: http : //www.python.org/dev/peps/pep-0249
MySQLリファレンスマニュアル: http : //dev.mysql.com/doc/
MySQLクライアント/サーバプロトコル: http : //dev.mysql.com/doc/internals/en/client-server-protocol.html
MySQL Community Slackの “Connector”チャンネル: http : //lefred.be/mysql-community-on-slack/
PyMySQLメーリングリスト: https ://groups.google.com/forum/#!forum/pymysql-users
ライセンス
PyMySQLはMITライセンスでリリースされています。 詳細については、ライセンスを参照してください。