Пример загрузки в DB MSSQL c CVS

python > Пример загрузки в DB MSSQL c CVS
21.05.2020 14:00:16


Наиболее часто встречающиеся слова в статье:

[read_csv] ['QGPL_SQL2] [encoding] ["cp1251"] [header=None] [names=cols] [connect] ['DRIVER=] [SERVER=] [DATABASE=MDM_Import]


Статья:

import pandas as pd 
import numpy as np
import pyodbc
cols = ['col1','col2','col3']
data = pd.read_csv('QGPL_SQL2.csv',encoding = "cp1251", sep = ";", header=None, names=cols) 
 
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=.;DATABASE=MDM_Import;')
cursor = conn.cursor()
df = np.array(data [['col1', 'col2','col3']])
 
z=[]
for c in df:
   z.append((c[0], c[1], c[2]))
 
cursor.executemany("insert into [dbo].[QGPL_SQL2] ([COL1],[COL2],[COL3] ) values (?,?,?);", z)
conn.commit()
print('Готово')