In this tutorial, we will learn how to retrieve data from MySQL table in python, both, the complete table data, and data from some specific columns.
在本教程中,我们将学习如何从 python中的MySQL表中检索数据 ,包括完整的表数据和某些特定列中的数据。
Python MySQL-选择数据 (Python MySQL - SELECT Data)
In MySQL, to retrieve data from a table we will use the SELECT
statement. The syntax for the same is given below:
在MySQL中, 要从表中检索数据,我们将使用SELECT
语句。 下面给出了相同的语法:
SELECT column_names FROM table_name
从MySQL表中检索所有记录 (Retrieve All records from MySQL Table)
In order to get all the records from a table, *
is used instead of column names. Let us retrieve all the data from the students table which we inserted before:
为了从表中获取所有记录,使用*
代替列名。 让我们从之前插入的students表中检索所有数据:
import mysql.connector as mysql
db = mysql.connect(
host = "localhost",
user = "yourusername",
passwd = "yourpassword",
database = "studytonight"
)
cursor = db.cursor()
#