我有一个python脚本,该脚本从CSV文件读取并将内容插入MySQL数据库。
运行脚本时,此行显示SQL错误:
insert_sql = "INSERT INTO billing_info
(InvoiceId, PayerAccountId, LinkedAccountId,
RecordType, RecordId, ProductName, RateId,
SubscriptionId, PricingPlanId, UsageType, Operation,
AvailabilityZone, ReservedInstance, ItemDescription,
UsageStartDate, UsageEndDate, UsageQuantity,
BlendedRate, BlendedCost, UnBlendedRate, UnBlendedCost,
ResourceId, Engagement, Name, Owner, Parent)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
运行时出现此SQL错误:
['198715713', '188087670762', '200925899732', 'LineItem', '200176724883754717735329322', 'AWS CloudTrail', '300551402', '214457068', '34915176', 'EUC1-FreeEventsRecorded', 'None', '', 'N', '0.0 per free event recorded in EU (Frankfurt) region', '2019-03-01 00:00:00', '2019-03-01 01:00:00', '4.0000000000', '0.0000000000', '0.0000000000', '0.0000000000', '0.0000000000', '', '', '', '', '']
Exception: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, ' at line 1
但是当我采用该错误所抱怨的确切SQL语句并将其自己插入数据库时,它就可以工作!
mysql> INSERT INTO billing_info (InvoiceId, PayerAccountId, LinkedAccountId, RecordType, RecordId, ProductName, RateId, SubscriptionId, PricingPlanId, UsageType, Operation, AvailabilityZone, ReservedInstance, ItemDescription, UsageStartDate, UsageEndDate, UsageQuantity, BlendedRate, BlendedCost, UnBlendedRate, UnBlendedCost, ResourceId, Engagement, Name, Owner, Parent) VALUES ('198715713', '188087670762', '200925899732', 'LineItem', '200176724883754717735329322', 'AWS CloudTrail', '300551402', '214457068', '34915176', 'EUC1-FreeEventsRecorded', 'None', '', 'N', '0.0 per free event recorded in EU (Frankfurt) region', '2019-03-01 00:00:00', '2019-03-01 01:00:00', '4.0000000000', '0.0000000000', '0.0000000000', '0.0000000000', '0.0000000000', '', '', '', '', '');
Query OK, 1 row affected (0.01 sec)
到底会发生什么?
这是我所有的代码(非常小):
#!/usr/bin/env python3
import os
import mysql.connector
import csv
source = os.path.join('source_files', 'aws_bills', 'march-bill-original-2019.csv')
destination = os.path.join('output_files', 'aws_bills', 'test_out.csv')
mydb = mysql.connector.connect(user='xxx', password='xxx',
host='xxx',
database='aws_bill')
cursor = mydb.cursor()
def read_csv_to_sql():
try:
with open(source) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
insert_sql = "INSERT INTO billing_info (InvoiceId, PayerAccountId, LinkedAccountId, RecordType, RecordId, ProductName, RateId, SubscriptionId, PricingPlanId, UsageType, Operation, AvailabilityZone, ReservedInstance, ItemDescription, UsageStartDate, UsageEndDate, UsageQuantity, BlendedRate, BlendedCost, UnBlendedRate, UnBlendedCost, ResourceId, Engagement, Name, Owner, Parent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
for row in csv_reader:
print(row)
cursor.execute(insert_sql.format(row))
mydb.commit()
print("Done importing data.")
except Exception as e:
print("Exception:", e)
mydb.rollback()
finally:
mydb.close()
def main():
read_csv_to_sql()
if __name__ == "__main__":
main()
我在做什么错?