Dav*_*dJB 5 python mysql mysql-python
当我使用python从SQL数据库中获取结果时,我会在返回值的开头和结尾处获得额外的章程.例如,下面的代码返回((56L,),而不是56,是否有人知道如何获得值......以及((,),)实际意味着什么......?
hp= 56
id= 3
database = MySQLdb.connect (host="localhost", user = "root", passwd = "", db = "db")
cursor = database.cursor()
cursor.execute("UPDATE period_option SET points =%s WHERE period_option_id =%s", (hp, id))
cursor.execute("SELECT points FROM period_option WHERE period_option_id =%s", (po_id_home))
results = cursor.fetchall()
print results
Run Code Online (Sandbox Code Playgroud)
谢谢!
fetchall()返回一个元组列表(实际上是一个元组).可以将其视为一系列行,其中每一行都是列中的一系列项.如果您确定您的搜索只返回1行,请使用fetchone(),它返回一个元组,这更容易解压缩.以下是从fetchall()和fetchone()中提取所需内容的示例:
# Use fetchall():
((points,),) = cursor.fetchall() # points = 56L
# Or, if you use fetchone():
(points,) = cursor.fetchone() # points = 56L
Run Code Online (Sandbox Code Playgroud)