mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
Docs: improve sqlite3 placeholders example (GH-101092)
(cherry picked from commit b84be8d9c0
)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
This commit is contained in:
parent
3ef9f6b508
commit
db65a326a4
1 changed files with 11 additions and 12 deletions
|
@ -1841,19 +1841,18 @@ Here's an example of both styles:
|
||||||
con = sqlite3.connect(":memory:")
|
con = sqlite3.connect(":memory:")
|
||||||
cur = con.execute("CREATE TABLE lang(name, first_appeared)")
|
cur = con.execute("CREATE TABLE lang(name, first_appeared)")
|
||||||
|
|
||||||
# This is the qmark style:
|
# This is the named style used with executemany():
|
||||||
cur.execute("INSERT INTO lang VALUES(?, ?)", ("C", 1972))
|
data = (
|
||||||
|
{"name": "C", "year": 1972},
|
||||||
|
{"name": "Fortran", "year": 1957},
|
||||||
|
{"name": "Python", "year": 1991},
|
||||||
|
{"name": "Go", "year": 2009},
|
||||||
|
)
|
||||||
|
cur.executemany("INSERT INTO lang VALUES(:name, :year)", data)
|
||||||
|
|
||||||
# The qmark style used with executemany():
|
# This is the qmark style used in a SELECT query:
|
||||||
lang_list = [
|
params = (1972,)
|
||||||
("Fortran", 1957),
|
cur.execute("SELECT * FROM lang WHERE first_appeared = ?", params)
|
||||||
("Python", 1991),
|
|
||||||
("Go", 2009),
|
|
||||||
]
|
|
||||||
cur.executemany("INSERT INTO lang VALUES(?, ?)", lang_list)
|
|
||||||
|
|
||||||
# And this is the named style:
|
|
||||||
cur.execute("SELECT * FROM lang WHERE first_appeared = :year", {"year": 1972})
|
|
||||||
print(cur.fetchall())
|
print(cur.fetchall())
|
||||||
|
|
||||||
.. testoutput::
|
.. testoutput::
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue