csvファイルを読み取り、SQLでテーブルを作成し、そのテーブル内にレコードを挿入するPythonコードがあります。コードは完全に正常に機能します。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を指すOLEDB宛先タスクを持つデータフローを作成します。
ここで車輪の再発明をする理由は本当にありません。SSISには、このタスクをほとんど簡単にするためのネイティブツールがあります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加