我试图检索存储在hs_audit.sqlite的表T3中的最新版本的审计问题。我已经尝试了cur.exectute行的多个版本,但仍然得到一个结果=无。我是sqlite的新手,所以任何人都可以帮助我正确的语法。 如果我按如下方式执行cur.execute行
cur.execute("SELECT hvq1 FROM T3 WHERE audit_ver = 1")
并删除
con.text_factory = str
它有效,所以我认为问题必须是在sqlite命令中使用变量。
max_audit_ver = 1
audit_questions = ["hvq1", "hvq2", "hvq3", "hvq4", "hvq5"]
count = 0
for quest in audit_questions:
con = sqlite3.connect("hs_audit.sqlite")
con.text_factory = str
cur = con.cursor()
cur.execute("SELECT '%s' FROM T3 WHERE audit_ver = '%s'" % (quest, max_audit_version))
result = cur.fetchone()
con.close()
print "Question =%s" % (quest)
print "Audit Version =%s" % (max_audit_version)
print "The Count Is =%s" % (count)
print "The return from the DB is =%s" % (result)
count += 1
答案 0 :(得分:0)
记住synthax ...... max_audit_version
和max_audit_ver
另一方面,你应该阅读this page,你会发现一些重要的注释,例如:
# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)