May-06-2019, 07:00 PM
Hope you will get some idea.
The following example assumes
CSV file contains column names in the first line
Connection is already built
File name is test.csv
Table name is MyTable
Python 3
...
https://stackoverflow.com/questions/2125...ing-python
The following example assumes
CSV file contains column names in the first line
Connection is already built
File name is test.csv
Table name is MyTable
Python 3
with open ('test.csv', 'r') as f: reader = csv.reader(f) columns = next(reader) query = 'insert into MyTable({0}) values ({1})' query = query.format(','.join(columns), ','.join('?' * len(columns))) cursor = connection.cursor() for data in reader: cursor.execute(query, data) cursor.commit()If column names are not included in the file:
...
with open ('test.csv', 'r') as f: reader = csv.reader(f) data = next(reader) query = 'insert into dbo.Test values ({0})' query = query.format(','.join('?' * len(data))) cursor = connection.cursor() cursor.execute(query, data) for data in reader: cursor.execute(query, data) cursor.commit()Source :
https://stackoverflow.com/questions/2125...ing-python