TypeError: 'int' object is not iterable when executin SQL with SET in python3

TypeError: 'int' object is not iterable when executin SQL with SET in python3

我正在创建集,以防止在我根据 table 列中的状态删除服务器上的旧文件夹时出现重复。但是当我尝试 运行 这个脚本时

cursor = connection.cursor()
sql="SELECT id, name, state FROM domain WHERE state='2'"
cursor.execute(sql)
records = cursor.fetchall()
print("Total number of records for deleting is: ", cursor.rowcount)

deleted = set(cursor.execute("select distinct name from domain where state = 2"))
active = set(cursor.execute("select distinct name from domain where state != 2"))

to_delete = deleted - active

print('Printing each domain record', "\n")
for row in records:

    print("id = ", row["id"], )
    print("name = ", row["name"])
    print("state  = ", row["state"], "\n")

    id = row["id"]
    name = row["name"]
    state = row["state"]

    if to_delete == 2:
        try:
            if os.path.exists('/data/sa/' + name):
                print('found records for deleting: ' + name, "\n")
                print("Total number of records for deleting is: ", cursor.rowcount)
                input("Press Enter to continue...")
                shutil.rmtree('/data/sa/' + name)
                print('records deleted')            
            else:
                print('no Directory found')
        #    pass
        except Exception as error:
            print("Directory already deleted or never existed")
    else:
        print('no records for deleting found')
        print('domain', name)
        print('hast state', state, "\n")

quit()
connection.close()

我遇到了这个错误

Traceback (most recent call last):
  File "mazani_old_zaznamu2.py", line 21, in <module>
    deleted = set(cursor.execute("select distinct name from domain where state = 2"))
TypeError: 'int' object is not iterable

我假设那是因为有很多域行,所以我必须以某种方式对它们进行排序以使其可迭代,但我不知道这怎么可能。

execute 方法不 return 行本身,只是 the number of affected rows

要获得结果,请尝试:

cursor.execute("select distinct name from domain where state = 2")
deleted = set(d['name'] for d in cursor.fetchall())