我有一个 python 代码,它读取一个 csv 文件,在 sql 中创建一个表并在该表中插入记录。该代码工作得很好。SSIS 任务大约需要 2-3 分钟才能运行,它没问题,因为代码需要那么多时间,但它不会在数据库上创建任何内容。
这是我的代码
#Import the required libraries
import pandas
import pyodbc
import os
#Set the Current Directory
path="C:/Users/MOLAP/Desktop/Data Warehouse Project/1. Datasets/Structured Data"
os.chdir(path)
#Establish connection with the server and the database
conn_str = (
r'DRIVER={ODBC Driver 13 for SQL Server};'
r'SERVER=MOLAP;'
r'DATABASE=EnergyUsageEffects_Database;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
#If a table is present - Truncate it else create the table
if cursor.tables(table='EnergyConsumption_Electrical', tableType='TABLE').fetchone():
cursor.execute("Truncate table EnergyConsumption_Electrical")
cnxn.commit()
else:
cursor.execute("Create table EnergyConsumption_Electrical (CountryName nvarchar(255), Year nvarchar(255), Unit nvarchar(255), Indicator nvarchar(255), Product nvarchar(255), ConsumptionValue float) ")
cnxn.commit()
#Read the CSV Inside the dataframe
df = pandas.read_csv("Electrical Energy Types.csv")
#Convert the dataframe into a list
ReqList = df.values.tolist()
#Loop through the list and Insert record after record inside the table
for i in range(len(ReqList)):
Val1 = ReqList[i][0]
Val2= ReqList[i][1]
Val3= ReqList[i][2]
Val4= ReqList[i][3]
Val5= ReqList[i][4]
Val6= ReqList[i][5]
cursor.execute("insert into EnergyConsumption_Electrical (CountryName, Year, Unit, Indicator, Product, ConsumptionValue) values (?,?,?,?,?,?)", Val1, Val2, Val3, Val4, Val5, Val6)
cnxn.commit()
这是我在SSIS上的配置,
我哪里出错了?
步骤 1) 从您的包中删除此任务。
步骤 2) 创建一个数据流,该数据流具有指向您的 .csv 的平面文件源和指向您的 database.table 的 OLE DB 目标任务。
真的没有理由在这里重新发明轮子。SSIS 有本地工具可以使这项任务变得微不足道。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句