尝试使用带有关键字参数的.execute()SQLite API查询时发生Python错误

2024-06-01 01:58:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习SQLite,并试图创建一个存储值和检索行的字符串,但我得到一个错误“execute()不带关键字参数”。这是我引用数据库的方式:

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)

更新

我曲解了我的参数。我使用下面接受的答案中提到的qmarks符号来引用表行


Tags: 字符串import数据库executedbsqlite参数connect
1条回答
网友
1楼 · 发布于 2024-06-01 01:58:25

documentation上写着:

The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style).

Here’s an example of both styles:

# 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))

相关问题 更多 >