如何从Python应用程序连接MySQL数据库并通过示例执行SQL查询?

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值