【发布时间】:2022-03-07 10:11:02
【问题描述】:
我正在尝试使用两个列名“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',)]
【问题讨论】:
-
您不能对表名或列名使用参数。它们必须在编译时直接出现在语句中。
-
@Shawn 抱歉,我不确定你的意思。你能详细说明一下吗?
标签: python-3.x sqlite