SQL循环与python和pyodbc

我想创build一个查询循环,从一个实例到下一个实例进行交互。 获取正确的数据后,我会做一些计算,并将具有特定关系的新信息写回数据库。

Example Set up Firt loop = [Aggregation Level](1, 2, 3) Second loop = Product(A, B, C) Time = (2001,2002,2003) 1. First loop Aggregation level "1", Product "A", Fetch all years (2001, 2002, 2003) Output = [ (2001,'10 pc','20 €') (2002,'8 pc','18 €') (2003,'82 pc','5000 €')] 2. Second loop Aggregation level "1", Product "B", Fetch all years (2001, 2002, 2003) Output = [ (2001,'15 pc','35 €') (2002,'20 pc','100 €') (2003,'25 pc','5000 €')] 

我将使用数组(行)并做一些计算。 因此,我将在这个特定的结构中转换数据,我会得到新的数字。

最后,我提出了我的代码,我想用select语句中的varible [X,Y]来代替导入的excel文件的索引作为循环参数,还是在数据库中search更好? 这些信息已经存储在数据库中。

variables[X1,Y1]是我的新值,必须用特定的关系写回数据库中,我该怎么做才能find正确的关系? 主键还是更好的方法?

祝愿123GuteLaune

 import pyodbc as py import openpyxl as ol #Connection string to access cnxn = py.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};UID=admin;UserCommitSync=Yes#;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:\xxx\Desktop;DBQ=C:\xxx\Desktop\DatabaseMA_Test.mdb") #Connection string to excel wb = ol.load_workbook('C:\xxx\Desktop\Beisspieldatensatz.xlsx') X = wb.get_sheet_by_name("Hierarchie") tuple(X['A2':'A72']).value wb = ol.load_workbook('C:\xxx\Desktop\Beisspieldatensatz.xlsx') Y = wb.get_sheet_by_name("Mat_GR") tuple(Y['A2':'A51']).value #Output of the loaded Excel data print (X,Y) #Load Data out of MS Access cursor.execute( """ SELECT Mat_ID, Monat, Jahr, Umsatzmenge, Umsatz FROM [TW-DS] Order BY Jahr ASC 'this is at the moment not working' WHERE Hier_ID = ? and Mat_ID= ? """, [X, Y]) rows = cursor.fetchall() print(rows) #Mathematical model unfilled #Write data back in MS Access cursor.execute( """insert into [TW-DS] (Outlier, [Outlier Value]) Values (X1, Y2) """) cnxn.commit()