创建MySQL表

mycursor.execute("CREATE TABLE IF NOT EXISTS my_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)")

将txt文件中的数据导入MySQL表

for row in df.itertuples(): name = row[1] age = row[2] sql = "INSERT INTO my_table (name, age) VALUES (%s, %s)" val = (name, age) mycursor.execute(sql, val)

mydb.commit() # 提交事务 print("数据导入成功")

import pymysqlimport pandas as pdmydb = pymysqlconnect host=localhost user=root password=123456 database=txmycursor = mydbcursor# 从txt文档取数数据df = pdread_csvD工程技术大学爬虫pythonProjectresultssues

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

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