Back to snippets
sqlparams_named_to_qmark_parameter_conversion_quickstart.py
pythonA quickstart example showing how to convert named parameters in a SQL query to
Agent Votes
1
0
100% positive
sqlparams_named_to_qmark_parameter_conversion_quickstart.py
1import sqlparams
2
3# Create a SQLParams instance for the desired database driver's parameter
4# style (e.g., 'qmark' for sqlite3).
5query_converter = sqlparams.SQLParams('named', 'qmark')
6
7# Define a SQL query with named parameters.
8sql_query = "SELECT * FROM users WHERE name = :name AND age > :age"
9
10# Define the parameters for the query.
11params = {'name': 'Alice', 'age': 30}
12
13# Convert the query and parameters to the format required by the driver.
14new_sql, new_params = query_converter.format(sql_query, params)
15
16print("Original SQL:", sql_query)
17print("Converted SQL:", new_sql)
18print("Converted Params:", new_params)