在 Python 中传递 SQLite 变量 [英] Passing SQLite variables in Python
问题描述
我正在用 python 编写一个应用程序并使用 sqlite.我有一个字符串列表,我也想将其添加到数据库中,其中每个元素代表一些与将要放置的列重合的数据.
目前我有这样的东西
cursor.execute("""insert into creditvalues ('Citi','5567','visa',6000,9.99,'23',9000)""")
我可以轻松添加字符串,但不知道如何添加列表中的变量.
使用参数.execute()
:
query = """插入信用(银行、号码、卡、int1、值、类型、int2)价值观(?, ?, ?, ?, ?, ?, ?)"""数据 = ['花旗', '5567', '签证', 6000, 9.99, '23', 9000]游标.执行(查询,数据)
根据 PEP249:
<块引用>.execute(operation[,parameters])
:
准备并执行数据库操作(查询或命令).参数可以作为序列或映射提供,并将在操作中绑定到变量.变量以特定于数据库的表示法指定(有关详细信息,请参阅模块的 paramstyle
属性)
检查paramstyle
:
qmark
表示你使用 ?
作为参数.
I am writing a app in python and utilzing sqlite. I have a list of strings which I would like to add too the database, where each element represents some data which coincides with the column it will be put.
currently I have something like this
cursor.execute("""insert into credit
values ('Citi','5567','visa',6000,9.99,'23',9000)""")
I can add strings easily but dont know how to add the variables of my list.
Use parameters to .execute()
:
query = """
INSERT INTO credit
(bank, number, card, int1, value, type, int2)
VALUES
(?, ?, ?, ?, ?, ?, ?)
"""
data = ['Citi', '5567', 'visa', 6000, 9.99, '23', 9000]
cursor.execute(query, data)
According to PEP249:
.execute(operation[,parameters])
:Prepare and execute a database operation (query or command). Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified in a database-specific notation (see the module's
paramstyle
attribute for details)
Checking paramstyle
:
>>> import sqlite3
>>> print sqlite3.paramstyle
qmark
qmark
means you use ?
for parameters.
这篇关于在 Python 中传递 SQLite 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!