【问题标题】:Cursor returns <sqlite3.Cursor object at 0x033A21E0> instead of returning the object itself光标返回 <sqlite3.Cursor object at 0x033A21E0> 而不是返回对象本身
【发布时间】:2019-11-18 02:39:51
【问题描述】:
当我在 db 浏览器上运行此代码时,它会显示价格,但当我在 python 中运行它时,它会返回控制台上的位置。
price = c.execute("SELECT Selling_Price FROM stock_records
WHERE Product_Name ='popcorn'")
print(price)
【问题讨论】:
标签:
python
sqlite
cursor
execute
【解决方案1】:
您必须检索已退回的商品(或商品列表)。举个例子:
price = c.execute("SELECT Selling_Price FROM stock_records
WHERE Product_Name ='popcorn'")
price = c.fetchone()
print(price)
更多信息可以在Python documentation找到。
【解决方案2】:
正如@Matthias 所评论的,execute() 不会直接返回查询结果;您必须从游标对象中获取结果。
试试这个:
c.execute("SELECT Selling_Price FROM stock_records WHERE Product_Name ='popcorn'")
print (c.fetchone())
如果您希望查询返回多个结果行,请使用fetchall() 而不是fetchone()。