简体   繁体   中英

How to fix an error inserting csv data in Snowflake

I am trying to load data from csv file to a database using python with following code

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")

I get the following error

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 ']'.

When I print(row) My data look as follows.

['pivotValues', 'pivotValues_stripped']
['urn:li:sponsoredCampaign:134971656', '134971656']
['urn:li:sponsoredCampaign:135043976', '135043976']
['urn:li:sponsoredCampaign:135249746', '135249746']

How can I insert data without these brackets ?

How about this:

for row in csv_data:
    cursor.execute('INSERT INTO jeremy_table_test(pivot, pivot_values)' ' VALUES({1},{2})'.format(row[0],row[1]))

Edit: without .format()

for row in csv_data:
    cursor.execute('INSERT INTO jeremy_table_test(pivot, pivot_values)' ' VALUES('+str(row[0])+','+str(row[1])+')')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM