开发者

fetchone() is setting int variable to tuple

I have a project with Python 2.7 and SQLite3.

I am trying to store an integer variable in an SQLite3 database column, and then later be able to set a var开发者_如何学运维iable to the value of the SQLite column.

The connection is fine, the column exists, and it is type "int".

I am using this code to pull the data:

    Trailcrest.fish_score = c.execute("SELECT fish_score FROM roster WHERE agentnumber = ?;", (AN,) ).fetchone()

The "AN" variable I'm calling is provided as an argument when the def this code is contained in is called.

However, when I look at the Trailcrest.fish_score variable, while the data has been written to it, it has been written as a tuple, and not as an integer. The variable originally contained an integer.

What is going on? How can I fix it? (I'm assuming I need to do some casting, but I'm not sure.)


fetchone() still returns a Row-like object, which you'll need to parse the lone field out of it, so you'll need to get the data out of it

row = c.execute("SELECT fish_score FROM roster WHERE agentnumber = ?;", (AN,) ).fetchone()
if row is not None:  # or just "if row"
    Trailcrest.fish_score = row[0]
else:
    pass # didn't get back a row
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜