MySQL is a relational database management system that supports multiple programming languages, including Python. Operating MySQL in Python requires the use of the MySQL client library. There are two ways to use the MySQL client library in Python: Python precompiled MySQL client module (MySQLdb or mysqlclient), or Python implemented MySQL client module (PyMySQL). This article will explain how to install Python's MySQL client library.
The following is the installation process:
Use pip to install MySQLdb
MySQLdb has stopped updating, so the installed MySQLdb version may be older than the Python version. Use the following command to install MySQLdb:
pip install MySQL-python
Install mysqlclient
mysqlclient is a faster and more stable MySQL client library, and the installed version will be newer than MySQLdb. Use the following command to install mysqlclient:
pip install mysqlclient
The following is the installation process:
Use pip to install PyMySQL
PyMySQL is a MySQL client library based on Python. Install PyMySQL:
pip install PyMySQL
The above three methods are all correct MySQL database client libraries. Which solution to choose depends on your needs. After the installation is complete, use the following command to test the installation of the MySQL library:
import pymysql conn = pymysql.connect(host='localhost', port=3306, user='root', password='password', db='test', charset='utf8') cur = conn.cursor() cur.execute('SELECT * FROM table_name;') res = cur.fetchall() print(res) cur.close() conn.close()
Use the above code. If the queried data is output normally, it means that the MySQL client library is successfully installed.
The above is the detailed content of mysql for python installation. For more information, please follow other related articles on the PHP Chinese website!