使用 Python 中的 MySQL 连接器(如 PyMySQL 或 mysql-connector-python)可以轻松连接 MySQL 数据库,并使用 SQL 查询语句检索数据集。

以下是使用 mysql-connector-python 连接 MySQL 并导入数据集的示例:

import mysql.connector

# 连接 MySQL 数据库
mydb = mysql.connector.connect(
  host='localhost',
  user='yourusername',
  password='yourpassword',
  database='yourdatabase'
)

# 查询数据集
mycursor = mydb.cursor()
mycursor.execute('SELECT * FROM yourtable')
myresult = mycursor.fetchall()

# 打印数据集
for x in myresult:
  print(x)

你可以根据需要修改查询语句,并使用 Python 中的其他库(如 Pandas)来进一步处理和分析数据集。

Python 导入 MySQL 数据集 - 简易指南

原文地址: https://www.cveoy.top/t/topic/n3T3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录