简体   繁体   中英

Inserting data from a csv file into a mysql table

So i'm trying to extract columns from a csv file, and input these into a mysql table.

However, I'm getting the following error which is targetting a 'title' (column 3,line1)

 'Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation, ' at line 1")

csv data

1038819,Discoverable,Using Adversarial Autoencoders for Multi-Modal Automatic Playlist Continuation,Conference Proceeding,"Vagliano, Iacopo; Galke, Lukas; Mai, Florian; Scherp, Ansgar",10.1145/3267471.3267476,
1037162,Discoverable,Performance Comparison of Ad-hoc Retrieval Models over Full-text vs. Titles of Documents (Forthcoming),Conference Proceeding,"Saleh, Ahmed; Beck, Tilman; Galke, Lukas; Scherp, Ansgar",,1893/28014

I think it has something to do with the fact the author column is using ',', and there is a whole mix of different spacing and symbols in others.

Python

import pymysql
import csv

csv_data= csv.reader(open('Book1.csv'))

conn=pymysql.connect("localhost","root", "root", "test")
cursor=conn.cursor()
print ("Done")
for row in csv_data:

    cursor.execute('INSERT INTO output (Output_ID, Status, Title, Type, Authors, DOI, Handle ) VALUES({}, {}, {}, {}, {}, {}, {})'.format(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
    conn.commit()

cursor.close()

Notice that when you compose the SQL using string formatting, the result lacks quotation marks around the values to be inserted:

In [215]: 'INSERT INTO output (Output_ID, Status, Title, Type, Authors, DOI, Handle ) 
           VALUES({}, {}, {}, {}, {}, {}, {})'.format('manchego', 'brie', 'gruyere', 'stilton', 'camembert', 'bleu', 'asiago')

Out[215]: 'INSERT INTO output (Output_ID, Status, Title, Type, Authors, DOI, Handle ) 
           VALUES(manchego, brie, gruyere, stilton, camembert, bleu, asiago)'

Proper SQL would require quotation marks

INSERT INTO output (Output_ID, Status, Title, Type, Authors, DOI, Handle )
VALUES("manchego", "brie", "gruyere", "stilton", "camembert", "bleu", "asiago")

Rather than adding the proper quotation yourself, the best practice is to use parametrized sql :

sql = '''INSERT INTO output (Output_ID, Status, Title, Type, Authors, DOI, Handle)
         VALUES(%s, %s, %s, %s, %s, %s, %s)'''
cursor.execute(sql, row)

Notice that cursor.execute(sql, row) can accept 2 arguments , sql and row . They are passed as separate arguments instead of as one string, sql % row . The cursor.execute function will handle the proper quoting for you. Not only is this simpler for you, it protects against SQL injection .

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