我正在尝试使用以下代码将数据从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:UsersTomAppDataLocalProgramsPythonPython37-32libsite-packagessnowflakeconnectorcursor.py", line 550, in execute
errvalue)
File "C:UsersTomAppDataLocalProgramsPythonPython37-32libsite-packagessnowflakeconnectorerrors.py", line 97, in errorhandler_wrapper
cursor.errorhandler(connection, cursor, errorclass, errorvalue)
File "C:UsersTomAppDataLocalProgramsPythonPython37-32libsite-packagessnowflakeconnectorerrors.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']
如何在没有这些括号的情况下插入数据?
这个怎么样:
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])+')')