我对使用SQL查询很新。任何建议,以改善这一点的代码: (顺便说一下,我真的不在乎这里的sql安全;这是一个代码,将在一个pyexe文件连接到本地sqlite文件 - 所以它不会感觉担心查询的安全性)。任何改善此功能的建议?
def InitBars(QA = "GDP1POP1_20091224_gdp", QB = "1 pork", reset = False):
global heights, values
D, heights, values, max, = [], {}, {}, 0.0001
if reset: GHolder.remove()
Q = "SELECT wbcode, Year, "+QA+" FROM DB WHERE commodity='"+QB+"' and "+QA+" IS NOT 'NULL'"
for i in cursor.execute(Q):
D.append((str(i[0]) + str(i[1]), float(i[2])))
if float(i[2]) > max: max = float(i[2])
for (i, n) in D: heights[i] = 5.0/max * n; values[i] = n
Gui["YRBox_Slider"].set(0.0)
Gui["YRBox_Speed"].set(0.0)
以下的建议后,这是我的了:
def InitBars(QA = "GDP1POP1_20091224_gdp", QB = "1 pork", reset = False):
global heights, values; D, heights, values, max, = [], {}, {}, 0.0001
if reset: GHolder.remove()
Q = "SELECT wbcode||Year, %s FROM DB WHERE commodity='%s' and %s IS NOT 'NULL'" % (QA, QB, QA)
for a, b in cursor.execute(Q):
if float(b) > max: max = float(b)
values[a] = float(b)
for i in values: heights[i] = 5.0/max * values[i]
Gui["YRBox_Slider"].set(0.0); Gui["YRBox_Speed"].set(0.0)
不要习惯坏习惯。始终关心安全。 – 2010-10-30 16:18:23
考虑到sqlite3是Python标准库的一部分,您将不得不为更好的理由不使用它的参数化查询。 – 2010-10-30 17:52:26
建议是*不*要在同一行放置多个语句。这包括'如果'及其分支。 – Amnon 2010-10-30 19:52:46