mysql - 在Python中将CSV数据加载到MySQL中

标签 mysql python-3.x csv

我是 Python 新手,但必须跳过一些类(class)才能了解这个主题。

我正在使用以下代码:

import csv
import MySQLdb

mydb = MySQLdb.connect(host = 'scmetrics-rds-rw.db.amazon.com',
port = 3306,
user = "s",
passwd = '',
db = 'mydb'
)
cursor = mydb.cursor()
csv_data = csv.reader('h:\Depreciation-Reconstructed\test.csv')#Read the csv
for row in csv_data:
cursor.execute('INSERT INTO TestTable(ID,Name,Age,Student(Yes/No) VALUES (%s,%s,%s,%s)',row)

cursor.commit()
cursor.close()

但是我遇到了一个错误:

'query = query % args TypeError : Not enough arguments for format string

最佳答案

您在代码中遗漏了一个括号(是/否): cursor.execute('INSERT INTO TestTable(ID,姓名,年龄,学生(是/否)值(%s,%s,%s,%s)',row)

关于mysql - 在Python中将CSV数据加载到MySQL中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40408470/

相关文章:

php - 选择框: how to populate days php

mysql - 更改双周数据MYSQL的日期

python - Python 中的尝试/异常(exception) : appropriate for parsing dictionaries?

Python pandas.core.frame.Dataframe 索引不正确/如何删除 csv 文件每行末尾的逗号?

c# - 在 DataGridView 中读取 CSV 文件

java - 二维字符串传递到 JTable

php - 打开动态文件(fopen)

mysql - 如何在 Laravel 5 中使用 Eloquent 更新数据透视表

python - 为什么标记数据时出现错误。 C错误: out of memory in my panda script

python - eventlet.monkey_patch() 抛出异常 : The SECRET_KEY setting must not be empty