首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SELECT语句返回列名而不是值(用于所述列)

SELECT语句返回列名而不是值(用于所述列)
EN

Stack Overflow用户
提问于 2019-07-01 21:24:51
回答 1查看 25关注 0票数 0

我试图使用两个列名'id‘和'easy_high_score’将信息解析到SELECT语句中,这样我就可以在我的程序中操作这两个列的值,但是当尝试获取列‘easy_high_score’的值时,它返回一个字符串('easy_high_score',)。

即使表中没有提到('easy_high_score‘),它仍然会打印出来。在该表中,id 1具有我正在尝试获取的正确的值和信息,但无济于事。我对SQLite3还是个新手。

代码语言:javascript
运行
AI代码解释
复制
if mode == "Easy":
    mode = 'easy_high_score'

if mode == "Normal":
    mode = "normal_high_score"

if mode == 'Hard':
    mode == "hard_high_score"


incrementor = 1 ##This is used in a for loop but not necessary for this post
c.execute("SELECT ? FROM players WHERE id=?", (mode, incrementor))
allPlayers = c.fetchall()
print(allPlayers)  #This is printing [('easy_high_score',)], when it should be printing an integer.

预期结果: 20 (或一个整数,表示easy模式的高分)

实际结果:('easy_high_score',)

EN

回答 1

Stack Overflow用户

发布于 2019-07-01 21:32:55

不能使用参数指定列名,它应该在查询中按原样显示。修改执行查询的行,如下所示:

代码语言:javascript
运行
AI代码解释
复制
c.execute("SELECT %s FROM players WHERE id=?" % mode, (incrementor,))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56842657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档