无法在python / sql / flask中连接'str'和'nonetype'对象

时间:2016-06-10 10:26:52

标签: python sqlite flask flask-sqlalchemy

我只需要将一个列名传递给SQL Query一次。这个代码在订购时工作正常,但是当在页面上按下按钮(将内容插入表中)时,会抛出此错误。

“无法连接'str'和'nonetype'对象”

    if request.method == "POST":
    ## DISPLAYING MOVIES ##

    general = db.execute(
        """
        SELECT Movies.Movie_ID, Movies.Name, Movies.Year, Movies.Image
        FROM Movie_Genre
        JOIN Movies ON Movie_Genre.Movie_ID = Movies.Movie_ID
        JOIN Genres ON Movie_Genre.Genre_ID = Genres.Genre_ID
        WHERE Genres.Genre = ? AND Rating_IMDB >= ?
        ORDER BY """+order+""";
        """, 
        (str(selectedGenre), rating)
        )
    basic = general.fetchall()

这是导致错误显示的代码。当用户按下“收藏夹”按钮时执行代码。

try:
    clicked = request.get_json(['data'])
    user = db.execute(
        """
        SELECT User_ID
        FROM Users
        WHERE Username = ?;
        """,
        (user_name,)
        )

    userID = user.fetchone()[0]

    user_favs = db.execute(
        """
        INSERT INTO User_Favs
        VALUES (?,?);
        """,
        (userID, clicked,)
        )
    db.commit()
    db.close()
    gc.collect()
except:
    pass

修改

这是订单的来源,几乎从来都不是'无'

order = request.form.get('order')
while order:
    if order == "Newest":
        order = "Year DESC"
        break 
    elif order == "Oldest":
        order = "Year ASC"
        break
    elif order == "Best":
        order = "Rating_IMDB DESC"
        break
    elif order == "Worst":
        order = "Rating_IMDB ASC"
        break
    else:
        order == ''
        break

如果问题出现在串联中,那么排序查询就无法在网站上运行,但确实如此。只有按下“收藏夹”按钮才会出现错误。将数据插入数据库。

1 个答案:

答案 0 :(得分:0)

在你写的else子句中:

write-host "add-dhcpserverv4Filter -computername pc-alibaba -list allow -macaddress $mac -description $desc"

而不是:

    else:
        order == ''
        break

此外, else: order = '' break 子句是多余的,因为该块中的代码只运行一次(因为每个条件中的while s)。如果break返回request.form.get('order'),您甚至无法获得该代码。