我正在尝试使用带有以下代码的python将数据从csv文件加载到数据库中
file=open('C:\\Users\\Tom\\Documents\\Test\\csv_data_20181115.csv')
csv_data = csv.reader(file)
mydb = snowflake.connector.connect(
user=user,
password=password,
account=account,
database=database,
)
for row in csv_data:
cursor.execute('INSERT INTO jeremy_table_test(pivot, pivot_values)' ' VALUES({})'.format(row))
# close the connection to the database.
mydb.commit()
cursor.close()
print("Done")
我收到以下错误
Traceback (most recent call last):
File "read_csv.py", line 26, in <module>
cursor.execute('INSERT INTO test_table_test(pivot, pivot_values)' ' VALUES({})'.format(row))
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\snowflake\connector\cursor.py", line 550, in execute
errvalue)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\snowflake\connector\errors.py", line 97, in errorhandler_wrapper
cursor.errorhandler(connection, cursor, errorclass, errorvalue)
File "C:\Users\Tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\snowflake\connector\errors.py", line 73, in default_errorhandler
done_format_msg=errorvalue.get(u'done_format_msg'))
snowflake.connector.errors.ProgrammingError: 001003 (42000): SQL compilation error:
syntax error line 1 at position 58 unexpected '['.
syntax error line 1 at position 96 unexpected ']'.
当我打印(行)时 我的数据如下。
['pivotValues', 'pivotValues_stripped']
['urn:li:sponsoredCampaign:134971656', '134971656']
['urn:li:sponsoredCampaign:135043976', '135043976']
['urn:li:sponsoredCampaign:135249746', '135249746']
如何在没有这些括号的情况下插入数据?
答案 0 :(得分:1)
如何?
for row in csv_data:
cursor.execute('INSERT INTO jeremy_table_test(pivot, pivot_values)' ' VALUES({1},{2})'.format(row[0],row[1]))
编辑:
没有.format()
for row in csv_data:
cursor.execute('INSERT INTO jeremy_table_test(pivot, pivot_values)' ' VALUES('+str(row[0])+','+str(row[1])+')')