SQLite执行错误

问题描述:

我正在学习SQLite,并试图创建一个字符串来存储值并使用下面我的程序中提取的代码行来检索行,但是我不断得到一个错误“execute()不带关键字参数“。我不明白,因为sqlite3的DB API表示,这是引用数据库中的正确方式:SQLite执行错误

import sqlite3 
dbb = sqlite3.connect('finance.db') 
db = dbb.cursor() 
username = request.form.get("username") 

#query database for username 
rows = db.execute("SELECT * FROM users WHERE username = :username", \ 
username=username) 

documentation说:

sqlite3模块支持两种占位符:问题标记(qmark样式)和命名占位符(命名样式)。

下面是两种风格的例子:

# This is the qmark style: 
cur.execute("insert into people values (?, ?)", (who, age)) 

# And this is the named style: 
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age}) 

关键字参数不被支持。然而,你可以明确地将它们转换成字典(或写一个execute这样做的包装):

db.execute("SELECT ... :username", dict(username = username)) 
+0

谢谢,我错过了那一点。 qmark风格完美适用于我尚未考虑的模板方法。 – tiff