我正在写一个Python应用程序在 flask 中,这个应用程序使用SQLite作为SQL服务器。这个应用程序需要从数据库中获取变量'喜欢',但该文件被作为sqlite3.Row打开,而不是我正在寻找的整数。
@bp.route('/<int:id>/like', methods=('POST', 'GET'))
def like(id):
db = get_db()
db.commit()
likes = get_db().execute(
'SELECT likes FROM post WHERE id = 1'
).fetchone()
print(int(likes))
return redirect(url_for('blog.index'))
当python到达print(int(likes))
时,它会显示错误TypeError: int() argument must be a string, a bytes-like object or a real number, not 'sqlite3.Row'
我没有太多的经验与SQLite的Python模块,我不知道很多关于SQL。并正在努力学习这些,但似乎没有什么帮助我。
1条答案
按热度按时间7cjasjjr1#
原来,为了解决这个问题,我只需要添加
[0]
到fetchone的末尾,使其成为.fetchone()[0]
,谢谢Michael。