import pymysql as pm
import pandas as pd
import csv
username='user'
password='pass'
host='host'
port=port
database='database'
connection = pm.connect(user=username,password=password,host=host,port=port,database=database)
with open(r"path\to\file.csv", 'r') as file:
csvreader = csv.reader(file)
rows = list(csvreader)
placeholders = ','.join(['%s'] * len(rows[0]))
cursor=connection.cursor()
cursor.execute(f"INSERT INTO DB_NAME.TABLE_NAME ({placeholders})", rows)
我尝试将CSV文件导入MariaDB,并收到错误:
TypeError: not all arguments converted during string formatting
在这一行:
cursor.execute(f"INSERT INTO DB_NAME.TABLE_NAME ({placeholders})", rows)
我确实计算了csv中的列数和数据库中的列数,它们匹配,所以我不确定问题是什么。
1条答案
按热度按时间blmhpbnm1#
您的脚本中有两个错误:
你可以简单地用print命令替换cursor.execute()来检查:
INSERT INTO yourtable VALUES ({placeholders})
。