2017-12-18 258 views
0

我正在學習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) 

回答

0

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