找到你要的答案

Q:Python if statement skipping code even though condition is met

Q:Python语句跳过代码即使符合条件

I am trying to check if an entry is already in the database and add the entry if it isn't but the if statement never runs even though the condition is met.

        userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
        exists = userCurs.fetchone()
        if exists is None:
            addImage(userName,image,'','')
        else:
            print '--------> image skipped'
            print userCurs.fetchone()

I get this output:

--------> image skipped
None
--------> image skipped
None

and no entries are made to the database

我试图检查一个条目是否已经在数据库中,并添加条目,如果不是,但if语句从来没有运行,即使条件满足。

        userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
        exists = userCurs.fetchone()
        if exists is None:
            addImage(userName,image,'','')
        else:
            print '--------> image skipped'
            print userCurs.fetchone()

我得到这个输出:

--------> image skipped
None
--------> image skipped
None

对数据库没有条目

answer1: 回答1:

The SQL statement:

SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)

will always return a single row. The value of the column in that row will be the result of the EXISTS() function, which will be TRUE if any matches are found, or FALSE otherwise.

Since your original code only tested for the existence of a row in the result, and the full statement will always return exactly one row, you get the behavior you see.

What I think you should do is have your query return a rowset that has zero rows if no matches are found, and one (or more) rows if matches are found:

userCurs.execute("SELECT 1 FROM images WHERE imageLink=?", (image,))

Now your original test should work - if no imageLinks match the query, then there will be no rows in the result, so the first fetchone() will return a null object as you expect.

Of course, as several others have mentioned, you should ony call fetchone() once per row since it moves the cursor.

SQL语句:

SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)

总是返回一行。在这行的exists()功能结果的列的值,将所发现的任何比赛都是真实的,否则。

因为你的原始代码只在结果中测试一行的存在,而完整语句总是返回一行,你得到了你看到的行为。

我认为你应该做的是让你的查询返回的行集,零行如果未找到匹配,和一个(或多个)如果发现匹配的行:

userCurs.execute("SELECT 1 FROM images WHERE imageLink=?", (image,))

现在你的原始测试如果没有imagelinks查询匹配的工作,然后将结果没有行,所以第一fetchone()将会如你所期望的返回一个空对象。

当然,一些其他人提到的,你应该只叫fetchone()每行因为它移动光标。

answer2: 回答2:

Solved it by changing the code to

        userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
        exists = userCurs.fetchone()
        if exists[0] == 0:
            addImage(userName,image,'','')
        else:
            print '--------> image skipped'
            print exists

As people have said the calling fetchone() twice gives different results the actual value of exists was (0,)

通过改变代码来解决它

        userCurs.execute("SELECT EXISTS(SELECT 1 FROM images WHERE imageLink=?)", (image,))
        exists = userCurs.fetchone()
        if exists[0] == 0:
            addImage(userName,image,'','')
        else:
            print '--------> image skipped'
            print exists

人们说,叫fetchone()两次给出不同的结果的实际价值存在是(0,)

answer3: 回答3:

Don't call cursor.fetchone() again; the next row is always empty. Reuse the variable. You are fetching a (0,) or (1,) tuple; you could use tuple assignment to extract the flag value (note the comma on exits, =):

exists, = userCurs.fetchone()
if not exists:
    addImage(userName, image, '', '')
else:
    print '--------> image skipped'
    print exists

Now exists will be set to 0 or 1, rather than (0,) or (1,) and the if not exists: test will not pass for the 0 case.

不要叫光标。fetchone()一遍;下一行总是空的。重用变量。您正在获取一个(0)或(1)元组;您可以使用元组赋值来提取标志值(注意出口上的逗号):

exists, = userCurs.fetchone()
if not exists:
    addImage(userName, image, '', '')
else:
    print '--------> image skipped'
    print exists

现在存在将设置为0或1,而不是(0)或(1),如果不存在:测试不会通过为0案件。

python-2.7  if-statement