2016-05-06 76 views
0
import sqlite3 
import math 
import Dish_vol 

try: 
    conn = sqlite3.connect('C:\\Users\\TJgro\\Desktop\\NEW_ATG\\\ 
Databases\\\Systems.db') 
    print("Connection Successful") 
except: 
    print("Unsuccessful connection") 

c=conn.cursor() 

c.execute("DROP TABLE IF EXISTS Tanks") 

c.execute("CREATE TABLE Tanks \ 
(ID INT PRIMARY KEY NOT NULL,\ 
DIR_Tag VARCHAR(255) NOT NULL,\ 
PRO_Type INT NOT NULL,\ 
PRO_Shape INT NOT NULL,\ 
DIR_Radius INT NOT NULL,\ 
DIR_Dish1 INT NOT NULL,\ 
DIR_Dish2 INT NOT NULL,\ 
DIR_Length INT NOT NULL,\ 
DIR_Capacity INT NOT NULL,\ 
DIR_HighAlarm INT NOT NULL,\ 
DIR_LowAlarm INT NOT NULL,\ 
DIR_WaterAlarm INT NOT NULL,\ 
DIR_Hysteresis INT NOT NULL,\ 
DIR_Area VARCHAR(255) NOT NULL,\ 
DIR_LevelOffset INT NOT NULL,\ 
DIR_SpecificGravity INT NOT NULL,\ 
DIR_MoveCount INT NOT NULL,\ 
DIR_MinMovement INT NOT NULL,\ 
DIR_MinimumDelivery INT NOT NULL,\ 
DIR_MinimumTheft INT NOT NULL,\ 
DIR_DeliveryGap INT NOT NULL,\ 
DIR_ProbeRange INT NOT NULL,\ 
PRO_TCPVolumeEnable INT NOT NULL,\ 
PRO_TCPUllageEnable INT NOT NULL,\ 
PRO_TCPTemperatureEnable INT NOT NULL,\ 
VAR_Pressure INT NOT NULL,\ 
VAR_LevelReg INT NOT NULL,\ 
AUX_Ullage INT NOT NULL,\ 
VAR_HMI_1_Volume INT NOT NULL,\ 
VAR_HMI_1_Ullage INT NOT NULL,\ 
VAR_HMI_1_Temperature INT NOT NULL,\ 
VAR_HMI_2_Volume INT NOT NULL,\ 
VAR_HMI_2_Ullage INT NOT NULL,\ 
VAR_HMI_2_Temperature INT NOT NULL,\ 
VAR_HMI_3_Volume INT NOT NULL,\ 
VAR_HMI_3_Ullage INT NOT NULL,\ 
VAR_HMI_3_Temperature INT NOT NULL,\ 
VAR_AlarmRelayReg INT NOT NULL)") 

for t in [(0, 'xyz', 'abc', 5, 15, 1371, 1500, 1500, 9144, 0, 0, 0, \ 
     0, 0, 10, 1, 1, 0, 0, 0, 0, 0, 2500, 0, 0, 0,\ 
     1000, 1000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
     (1, 'xyz', 'abc', 5, 10, 1371, 1500, 1500, 9144, 0, 0, 0, \ 
     0, 0, 10, 1, 1, 0, 0, 0, 0, 0, 2500, 0, 0, 0,\ 
     1000, 1000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
     (2, 'xyz', 'abc', 5, 12, 1371, 1500, 1500, 9144, 0, 0, 0, \ 
     0, 0, 10, 1, 1, 0, 0, 0, 0, 0, 2500, 0, 0, 0,\ 
     1000, 1000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
     ]: 

c.execute("INSERT INTO Tanks VALUES\ 
(?,?,?,?,?,?,?,?,\ 
?,?,?,?,?,?,?,?,\ 
?,?,?,?,?,?,?,?,\ 
?,?,?,?,?,?,?,?,\ 
?,?,?,?,?,?,?)",t) 

沒有可迭代的創建表和填充之後,我有一些計算我需要執行,所以我選擇所需的字段形成數據庫:我不明白的錯誤信息INT在我的代碼

c.execute("SELECT DIR_Dish1 FROM Tanks") 
DIR_Dish1 = c.fetchall() 
print (DIR_Dish1) 
c.execute("SELECT DIR_Radius FROM Tanks") 
DIR_Radius = c.fetchall() 
print (DIR_Radius) 
c.execute("SELECT VAR_LevelReg FROM Tanks") 
VAR_LevelReg = c.fetchall() 
print (VAR_LevelReg) 
c.execute("SELECT DIR_Length FROM Tanks") 
DIR_Length = c.fetchall() 
print (DIR_Length) 

現在我有變數,我有一些計算:

dish1Volume = Dish_vol.Dish_Vol(DIR_Radius, DIR_Dish1, VAR_LevelReg) 

for row in c.execute("SELECT DIR_Radius, VAR_LevelReg, DIR_Length FROM Tanks"): 
for [DIR_Radius, VAR_LevelReg, DIR_Length] in row: 
    cylinderVolume = (((DIR_Radius * DIR_Radius) * math.acos((DIR_Radius - VAR_LevelReg)/DIR_Radius)) - \ 
       ((DIR_Radius - VAR_LevelReg) * math.sqrt((DIR_Radius * DIR_Radius) - \ 
       ((DIR_Radius - VAR_LevelReg) * (DIR_Radius - VAR_LevelReg))))) * (DIR_Length/1000.0) 

    volume = cylinderVolume/1000.0 

    Total = volume + dish1Volume + dish1Volume 
    print (Total) 

我得到的錯誤信息:

Traceback (most recent call last): 
File "C:\Users\TJgro\workspace\SQLite Practise\Dish_Vol\Cylinder_Vol \__init__.py", line 91, in <module> 
for [DIR_Radius, VAR_LevelReg, DIR_Length] in row: 
TypeError: 'int' object is not iterable 

我明白變量DIR_Dish1等都是整數,但在代碼中的另一部分相同的語法產生期望的結果:

for row in c.execute("SELECT DIR_Dish1 FROM Tanks"): 
for DIR_Dish1 in row: 
    if DIR_Dish1 > 0.001: 
     int1 = math.pi * DIR_Dish1 
     int3 = 4 * DIR_Dish1 * DIR_Dish1 

如何使我可以通過迭代的方式循環數據庫使用我需要的三個變量?

回答

2

每個row是一個具有列值的元組。您嘗試遍歷行和解壓每個列的值分爲3個不同的名字:

for [DIR_Radius, VAR_LevelReg, DIR_Length] in row: 

這對任何給定的行中的第一列的值如下效果:

>>> [DIR_Radius, VAR_LevelReg, DIR_Length] = 42 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'int' object is not iterable 

只需使用行,直接的分配新建分配FY,沒有for循環:

DIR_Radius, VAR_LevelReg, DIR_Length = row 

是不是真的需要這裏的方括號。

+0

謝謝,這解決了它! –