Python Forum
sqlite3 question - execute method with :parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite3 question - execute method with :parameter
#1
I see code using this syntax for the .execute and parameters passed with a dictionary. Where is this :parameter syntax defined?
  connection.execute("select * from :tab where :tab.fname = :firstname ",  {"tab": peopletable, "firstname": onename} )
 
I don't find this :parameter in any sqlite3 documentation? Is this dictionary string editing syntax a standard Python feature?
Reply
#2
https://docs.python.org/3/library/sqlite...or.execute Wrote:class sqlite3.Cursor
A Cursor instance has the following attributes and methods.

execute(sql[, parameters])
Executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders instead of SQL literals). The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style).

Here’s an example of both styles:
import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table people (name_last, age)")

who = "Yeltsin"
age = 72

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

print(cur.fetchone())

con.close()
execute() will only execute a single SQL statement. If you try to execute more than one statement with it, it will raise a Warning. Use executescript() if you want to execute multiple SQL statements with one call.
Reply
#3
This is called named place holders http://zetcode.com/db/sqlitepythontutorial/ You can send a dictionary to a general lookup or add or delete function using named placeholders.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,973 Dec-26-2022, 08:48 AM
Last Post: ibreeden
Question Sqlite3 how to know when cursor.execute didn't return anything ? SpongeB0B 2 860 Dec-18-2022, 06:13 PM
Last Post: deanhystad
  Zfill Method Parameter Confusion new_coder_231013 3 1,048 Dec-05-2022, 05:52 PM
Last Post: new_coder_231013
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,306 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Question about List.reverse() method tomliuwhite 1 1,340 Dec-07-2021, 08:20 AM
Last Post: ndc85430
  Sqlite3 DELETE question DT2000 7 2,988 May-01-2020, 08:55 AM
Last Post: DT2000
  Input as not default method parameter dan789 4 2,922 Mar-08-2019, 09:04 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020