|
| 1 | +[](https://pymysql.readthedocs.io/) |
| 2 | +[](https://coveralls.io/github/PyMySQL/PyMySQL?branch=main) |
| 3 | + |
| 4 | +# PyMySQL |
| 5 | + |
| 6 | +This package contains a pure-Python MySQL client library, based on [PEP |
| 7 | +249](https://www.python.org/dev/peps/pep-0249/). |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- Python -- one of the following: |
| 12 | + - [CPython](https://www.python.org/) : 3.7 and newer |
| 13 | + - [PyPy](https://pypy.org/) : Latest 3.x version |
| 14 | +- MySQL Server -- one of the following: |
| 15 | + - [MySQL](https://www.mysql.com/) \>= 5.7 |
| 16 | + - [MariaDB](https://mariadb.org/) \>= 10.3 |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Package is uploaded on [PyPI](https://pypi.org/project/PyMySQL). |
| 21 | + |
| 22 | +You can install it with pip: |
| 23 | + |
| 24 | + $ python3 -m pip install PyMySQL |
| 25 | + |
| 26 | +To use "sha256_password" or "caching_sha2_password" for authenticate, |
| 27 | +you need to install additional dependency: |
| 28 | + |
| 29 | + $ python3 -m pip install PyMySQL[rsa] |
| 30 | + |
| 31 | +To use MariaDB's "ed25519" authentication method, you need to install |
| 32 | +additional dependency: |
| 33 | + |
| 34 | + $ python3 -m pip install PyMySQL[ed25519] |
| 35 | + |
| 36 | +## Documentation |
| 37 | + |
| 38 | +Documentation is available online: <https://pymysql.readthedocs.io/> |
| 39 | + |
| 40 | +For support, please refer to the |
| 41 | +[StackOverflow](https://stackoverflow.com/questions/tagged/pymysql). |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +The following examples make use of a simple table |
| 46 | + |
| 47 | +``` sql |
| 48 | +CREATE TABLE `users` ( |
| 49 | + `id` int(11) NOT NULL AUTO_INCREMENT, |
| 50 | + `email` varchar(255) COLLATE utf8_bin NOT NULL, |
| 51 | + `password` varchar(255) COLLATE utf8_bin NOT NULL, |
| 52 | + PRIMARY KEY (`id`) |
| 53 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
| 54 | +AUTO_INCREMENT=1 ; |
| 55 | +``` |
| 56 | + |
| 57 | +``` python |
| 58 | +import pymysql.cursors |
| 59 | + |
| 60 | +# Connect to the database |
| 61 | +connection = pymysql.connect(host='localhost', |
| 62 | + user='user', |
| 63 | + password='passwd', |
| 64 | + database='db', |
| 65 | + cursorclass=pymysql.cursors.DictCursor) |
| 66 | + |
| 67 | +with connection: |
| 68 | + with connection.cursor() as cursor: |
| 69 | + # Create a new record |
| 70 | + sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" |
| 71 | + cursor.execute(sql, ('webmaster@python.org', 'very-secret')) |
| 72 | + |
| 73 | + # connection is not autocommit by default. So you must commit to save |
| 74 | + # your changes. |
| 75 | + connection.commit() |
| 76 | + |
| 77 | + with connection.cursor() as cursor: |
| 78 | + # Read a single record |
| 79 | + sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s" |
| 80 | + cursor.execute(sql, ('webmaster@python.org',)) |
| 81 | + result = cursor.fetchone() |
| 82 | + print(result) |
| 83 | +``` |
| 84 | + |
| 85 | +This example will print: |
| 86 | + |
| 87 | +``` python |
| 88 | +{'password': 'very-secret', 'id': 1} |
| 89 | +``` |
| 90 | + |
| 91 | +## Resources |
| 92 | + |
| 93 | +- DB-API 2.0: <https://www.python.org/dev/peps/pep-0249/> |
| 94 | +- MySQL Reference Manuals: <https://dev.mysql.com/doc/> |
| 95 | +- MySQL client/server protocol: |
| 96 | + <https://dev.mysql.com/doc/internals/en/client-server-protocol.html> |
| 97 | +- "Connector" channel in MySQL Community Slack: |
| 98 | + <https://lefred.be/mysql-community-on-slack/> |
| 99 | +- PyMySQL mailing list: |
| 100 | + <https://groups.google.com/forum/#!forum/pymysql-users> |
| 101 | + |
| 102 | +## License |
| 103 | + |
| 104 | +PyMySQL is released under the MIT License. See LICENSE for more |
| 105 | +information. |
0 commit comments