python-2.7 - python sqlite3 .executemany() 带有命名占位符?

标签 python-2.7 sqlite

这有效:

ss = 'insert into images (file_path) values(?);'
dddd = (('dd1',), ('dd2',))
conn.executemany(ss, dddd)

但是这不会:

s = 'insert into images (file_path) values (:v)'
ddddd = ({':v': 'dd11'}, {':v': 'dd22'})
conn.executemany(s, ddddd)
Traceback (most recent call last):
  File "/Users/Wes/.virtualenvs/ppyy/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-31-a999de59f73b>", line 1, in <module>
    conn.executemany(s, ddddd)
ProgrammingError: You did not supply a value for binding 1.

我想知道是否可以在executemany 中使用命名参数,如果可以,如何使用。

documentation第 11.13.3 节一般性地讨论了参数,但没有讨论为其他风格的 .executexxx() 描述的两种类型的参数。

我已查看Python sqlite3 execute with both named and qmark parameters这与executemany无关。

最佳答案

source表明 execute() 只是构造一个单元素列表并调用 executemany(),因此问题不在于 executemany() 本身;相同的调用失败并显示 execute():

>>> conn.execute('SELECT :v', {':v': 42})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.ProgrammingError: You did not supply a value for binding 1.

Python documentation所示,命名参数不包含冒号:

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

所以你必须使用ddddd = ({'v': 'dd11'}, {'v': 'dd22'})

关于python-2.7 - python sqlite3 .executemany() 带有命名占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123283/

相关文章:

android - SQLite 数据库被锁定?

python - 在 Python 中重置文本框

python-2.7 - 分类轴标签中的换行符、 Bokeh 图

java - 如何在没有服务器的情况下在多个设备中的同一个应用程序之间共享数据库

mysql - SQL 查询在 SQLite 中有效,但在 MySQL 中无效

Xamarin.Forms 中的 SQLite-Net-Pcl

python - 使用 get_absolute_url() 时出现 NoReverseMatch 错误

python - 覆盖 Odoo 中的创建函数

python-2.7 - python simpy错误返回生成器内部的值

android-如何使用CursorAdapter填充listView并回收