MySQL is a very popular and open source database server. Python is an old but lately discovered language. In this tutorial, we will look at how to use this popular tech in our applications in order to run SQL queries.
MySQL是一种非常流行的开源数据库服务器。 Python是一种古老但最近发现的语言。 在本教程中,我们将研究如何在应用程序中使用这项流行技术来运行SQL查询。
安装Python MySQL连接器 (Install Python MySQL Connector)
In order to connect the MySQL database from Python application, we need some libraries. There are different libraries that support running SQL queries on MySQL but we will stick with mysql-connector-python3
.
为了从Python应用程序连接MySQL数据库,我们需要一些库。 有不同的库支持在MySQL上运行SQL查询,但我们会坚持使用mysql-connector-python3
。
Fedora,CentOS,RHEL (Fedora, CentOS, RHEL)
We can install the mysql-connector-python3 library with the following command. This requires root privileges.
我们可以使用以下命令安装mysql-connector-python3库。 这需要root特权。
$ yum install mysql-connector-python3
Debian,Ubuntu,薄荷 (Debian, Ubuntu,Mint)
We can install the mysql-connector-python3 library with the following command. This requires root privileges.
我们可以使用以下命令安装mysql-connector-python3库。 这需要root特权。
$ apt install mysql-connector-python3
点子 (Pip)
Pip can be used to install mysql connector library.
Pip可用于安装mysql连接器库。
$ pip3 install mysql-connector-python
加载MySQL库 (Load MySQL Library)
In order to use the MySQL library, we need to import it into our application. We will use import
statement to import library which is named mysql.connector
.
为了使用MySQL库,我们需要将其导入到我们的应用程序中。 我们将使用import
语句导入名为mysql.connector
库。
#!/bin/python3
import mysql.connector