SELECT语句返回列名而不是VALUE(对于所述列)

时间:2019-07-01 21:24:50

标签: python-3.x sqlite

我正在尝试使用两个列名'id'和'easy_high_score'将信息解析为SELECT语句,以便可以在程序中操纵两个列的值,但是尝试获取列的值时“ easy_high_score”,应为46或20这样的整数,它返回的字符串为('easy_high_score',)。

即使表中没有提及[['easy_high_score',)],它仍然会打印出来。在表中,id 1具有我尝试获取的正确值和信息,但无济于事。我对SQLite3相当陌生。

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_high_score',)]

1 个答案:

答案 0 :(得分:0)

不能使用参数指定列名,它应在查询中逐字出现。修改执行查询的行,如下所示:

c.execute("SELECT %s FROM players WHERE id=?" % mode, (incrementor,))