Python SQlite。 LIMIT 变量在循环中不改变值

标签 python sqlite loops for-loop select

我正在尝试找出每行中特殊教育需要学生人数的差异。 (代码、输出和数据库表在底部)

我做了什么:

Finding the number of rows in a table.

Initializing the LIMIT number to 0 BEFORE the loop starts.

I then add one to LIMIT and output it as a string.

I then select the number of SEN students at LIMIT currLimitStr 

I then add one to LIMIT and output it as a string.

I then select the number of SEN students at LIMIT currLimitStr 

I then fetch this data and print it out.

The loop starts again.

递增 currLimit 2 每个循环都有效,但由于某种原因,每个循环后都使用相同的 senRow1 和 senRow2 值。为什么会这样

countNumRows = cur.execute("SELECT count(*) FROM SEN_Table")
countNumFetch = countNumRows.fetchone()
countNumRowsTable = countNumFetch[0]
print("Number of rows in the table: " +  str(countNumRowsTable))
currLimit = 0
for x in range(0, countNumRowsTable):
    currLimit = currLimit + 1
    currLimit1Str = str(currLimit)
    senRow1 = cur.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber DESC limit " + currLimit1Str)
    currLimit = currLimit + 1
    currLimit2Str = str(currLimit)
    senRow2 = cur.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber DESC limit " + currLimit2Str)

    senRow1Num = senRow1.fetchone()[0]
    senRow2Num = senRow2.fetchone()[0]
    print(senRow1Num)
    print(senRow2Num)
    print("")
    senDiff = print(senRow1Num - senRow2Num)
    print("")

输出:

enter image description here

数据库表:

enter image description here

最佳答案

我不太明白你想要什么,所以我无法帮助你实现它。希望我可以帮助您理解为什么您的代码会这样做。

我在您的代码中添加了一些行,并用数字 #n 注释它们,以便在我的解释中引用它们。我的代码的输出带有星号前缀,这样您就可以区分它并且不会干扰您的代码。

Cursor.execute() 返回对同一游标的引用。

因此,senRow1senRow2 实际上是同一个对象,正如 #5 所证明的,其中断言了此身份。当您获取 senRow1 的第一行和 senrow2 的第一行时,您实际上是在影响同一个游标的前两行,称之为 cursenRow1senRow2,它们都是同一事物的不同名称。

实际上执行第一个查询是没有用的,因为在对其执行任何操作之前,您会在同一个游标中执行第二个查询,并且第一个查询的结果会丢失。

更改限制会更改行数,但不会更改前几行。

为了演示它,我在 #1#2 中创建了两个游标,在 #3 中执行查询#4 并打印 #6#7 中的所有行。正如您所看到的,返回的行数随限制而变化,但无论限制如何,前几行都保持不变

请注意,我打印第一个查询的结果是为了保证完整性,但正如之前所解释的,只有第二个查询与您的代码相关。

因此,在循环的每次迭代中,您都会查看第二个查询返回的前两行。这两行在每次迭代中都是相同的。

修改后的代码:

countNumRows = cur.execute("SELECT count(*) FROM SEN_Table")
countNumFetch = countNumRows.fetchone()
countNumRowsTable = countNumFetch[0]
print("Number of rows in the table: " +  str(countNumRowsTable))
currLimit = 0

cursor1 = conn.cursor()  #1
cursor2 = conn.cursor()  #2

for x in range(0, countNumRowsTable):
    currLimit = currLimit + 1
    currLimit1Str = str(currLimit)
    senRow1 = cur.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber DESC limit " + currLimit1Str)
    cursor1.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber "
                    "DESC limit " + currLimit1Str)  #3

    currLimit = currLimit + 1
    currLimit2Str = str(currLimit)
    senRow2 = cur.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber DESC limit " + currLimit2Str)
    cursor2.execute("SELECT SenNumber FROM SEN_Table ORDER BY SenNumber "
                    "DESC limit " + currLimit2Str)  #4

    assert(senRow1 is senRow2)  #5
    print('* cursor1: {}'.format(cursor1.fetchall()))  #6
    print('* cursor2: {}'.format(cursor2.fetchall()))  #7

    senRow1Num = senRow1.fetchone()[0]  # the 1st row of cur, senRow1
                                        # or senRow2 (they are the same)
    senRow2Num = senRow2.fetchone()[0]  # the 2nd row of cur, senRow1 or
                                        # senRow2 (they are the same)
    print(senRow1Num)
    print(senRow2Num)
    print("")
    senDiff = print(senRow1Num - senRow2Num)
    print("")

输出:

Number of rows in the table: 5
* cursor1: [(4,)]
* cursor2: [(4,), (3,)]
4
3

1

* cursor1: [(4,), (3,), (3,)]
* cursor2: [(4,), (3,), (3,), (2,)]
4
3

1

* cursor1: [(4,), (3,), (3,), (2,), (1,)]
* cursor2: [(4,), (3,), (3,), (2,), (1,)]
4
3

1

* cursor1: [(4,), (3,), (3,), (2,), (1,)]
* cursor2: [(4,), (3,), (3,), (2,), (1,)]
4
3

1

* cursor1: [(4,), (3,), (3,), (2,), (1,)]
* cursor2: [(4,), (3,), (3,), (2,), (1,)]
4
3

1

关于Python SQlite。 LIMIT 变量在循环中不改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50022851/

相关文章:

javascript - App Engine 和 Facebook : which libraries to use?

python - 使用 django orm 更新表而不删除关系

python - 计算列表中连续真值的长度

c++ - 使用数组 C++ 进行组合

c - 凯撒密码中最后一个字母后循环回到字母表开头的逻辑

c# - C# 中 for 循环的正确语法是什么?

python - 如何在Python中使用条件格式将数据框写入Excel?

typescript - 为每个测试创建一个单独的(在内存中)数据库

java - 如何在 Sqlite 中搜索字符串值?

android - 将 ViewModel 与 SQLite 数据库而非 Room 数据库一起使用