下面我有一些连接到数据库并运行查询的代码。如果我将它打入DB Browser for SQLite,这个查询按预期运行,但是当我在python中运行它时,我收到错误:
sqlite3.OperationalError:没有这样的列:CHARACTER_INNATES.PC_ID
我已经阅读了一些关于参数的文档和多个StackOverflow注释,以及它们防止注入攻击等问题的目的。我尝试过这种方法,它散落在'?'在任何地方,使它变得难以理解和不可维护,并且它不起作用。它返回了一个空元组。这让我相信应该有一种更简单的方法来执行这些类型的SQL查询。那有吗?或者我错过了一些明显的东西?
db = sqlite3.connect('TheGame.db')
db.text_factory = str
conn = db.cursor()
query = 'SELECT CHARACTERS.Full_Name, CHARACTER_INNATES.PC_ID, SKILLS_INNATES.Skill, Sum([CHARACTER_INNATES].[Current]*[Weight]) AS [Current Innate], Sum([CHARACTER_INNATES].[Maximum]*[Weight]) AS [Max Innate]\
FROM CHARACTERS INNER JOIN (SKILLS_INNATES INNER JOIN CHARACTER_INNATES ON SKILLS_INNATES.Innate = CHARACTER_INNATES.Innate) ON CHARACTERS.ID = CHARACTER_INNATES.PC_ID\
GROUP BY CHARACTERS.Full_Name, CHARACTER_INNATES.PC_ID, SKILLS_INNATES.Skill\
ORDER BY CHARACTER_INNATES.PC_ID, Sum([CHARACTER_INNATES].[Current]*[Weight]) DESC;'
conn.execute(query)
print conn.fetchall()
我的修复尝试看起来像
params = ('CHARACTERS.Full_Name', 'CHARACTER_INNATES.PC_ID', ...) #this continued for awhile
query = 'SELECT ?, ?, ?, ...'
conn.execute(query, params)
print conn.fetchall() # prints empty tuple
答案 0 :(得分:0)
此查询已无法读取。
无论如何,一些SQLite版本存在隐藏在括号内的表名的问题。当您使用Access查询构建器时,通常会发生这种情况;只需正确编写连接:
SELECT CHARACTERS.Full_Name,
CHARACTER_INNATES.PC_ID,
SKILLS_INNATES.Skill,
Sum(CHARACTER_INNATES.Current * Weight) AS "Current Innate",
Sum(CHARACTER_INNATES.Maximum * Weight) AS "Max Innate"
FROM CHARACTERS
INNER JOIN CHARACTER_INNATES ON CHARACTERS.ID = CHARACTER_INNATES.PC_ID
INNER JOIN SKILLS_INNATES USING (Innate)
GROUP BY CHARACTERS.Full_Name,
CHARACTER_INNATES.PC_ID,
SKILLS_INNATES.Skill
ORDER BY CHARACTER_INNATES.PC_ID,
Sum(CHARACTER_INNATES.Current * Weight) DESC;