我不明白错误消息int在我的代码中不可迭代

时间:2016-05-06 13:15:16

标签: python sqlite

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)

创建表并填充后,我需要执行一些计算,因此我从数据库中选择所需的字段:

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

如何使用我需要的三个变量以可以迭代数据库的方式创建循环?

1 个答案:

答案 0 :(得分: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

直接使用行的分配,没有for循环:

DIR_Radius, VAR_LevelReg, DIR_Length = row

这里不需要方括号。