Skip to content

Commit 5ace622

Browse files
committed
docs(paramstyle): expanded descriptions for setting paramstyle on module and cursor level. added more examples of how to set paramstyle
1 parent 4247f99 commit 5ace622

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

README.rst

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,61 @@ Enabling autocommit
109109
conn.autocommit = False
110110
111111
112-
Configuring cursor paramstyle
112+
Configuring paramstyle
113113
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114-
The paramstyle for a cursor can be modified via ``cursor.paramstyle``. The default paramstyle used is ``format``. Valid values for ``paramstyle`` include ``qmark, numeric, named, format, pyformat``.
114+
Paramstyle can be set on both a module and cursor level. When paramstyle is set on a module level e.g. ``redshift_connector.paramstyle = 'qmark'``, the user specified paramstyle is used for all subsequent cursors unless set on the cursor.
115+
When paramstyle is set on the cursor e.g. ```cursor.paramstyle = 'qmark'`` the user specified paramstyle is only used for that cursor object.
115116

116117
.. code-block:: python
117118
118-
# qmark
119+
# setting paramstyle to qmark on a module level
119120
redshift_connector.paramstyle = 'qmark'
121+
122+
123+
with redshift_connector.connect() as conn1:
124+
with conn1.cursor() as cursor1: # this cursor will use qmark paramstyle as it's been set on the module level
125+
pass
126+
127+
with conn1.cursor() as cursor2:
128+
# setting paramstyle to numeric on the cursor level only this cursor will use numeric paramstyle
129+
cursor.paramstyle = 'numeric'
130+
131+
with conn1.cursor() as cursor3: # this cursor will use qmark paramstyle as it's been set on the module level
132+
pass
133+
134+
with redshift_connector.connect() as conn2:
135+
with conn2.cursor() as cursor1: # this cursor will use qmark paramstyle as it's been set on the module level
136+
pass
137+
138+
139+
The module level default paramstyle used is ``format``. Valid values for ``paramstyle`` include ``qmark, numeric, named, format, pyformat``. The below example shows how to use various paramstyles after the paramstyle is set on the cursor.
140+
141+
When paramstyle is set to ``named`` or ``pyformat``, parameters must be passed as a Python dictionary to the ``execute()`` method. Other paramstyles require parameters to be passed as a Python tuple or list.
142+
143+
.. code-block:: python
144+
145+
# qmark
146+
cursor.paramstyle = 'qmark'
120147
sql = 'insert into foo(bar, jar) VALUES(?, ?)'
121148
cursor.execute(sql, (1, "hello world"))
122149
123150
# numeric
124-
redshift_connector.paramstyle = 'numeric'
151+
cursor.paramstyle = 'numeric'
125152
sql = 'insert into foo(bar, jar) VALUES(:1, :2)'
126153
cursor.execute(sql, (1, "hello world"))
127154
128155
# named
129-
redshift_connector.paramstyle = 'named'
156+
cursor.paramstyle = 'named'
130157
sql = 'insert into foo(bar, jar) VALUES(:p1, :p2)'
131158
cursor.execute(sql, {"p1":1, "p2":"hello world"})
132159
133160
# format
134-
redshift_connector.paramstyle = 'format'
161+
cursor.paramstyle = 'format'
135162
sql = 'insert into foo(bar, jar) VALUES(%s, %s)'
136163
cursor.execute(sql, (1, "hello world"))
137164
138165
# pyformat
139-
redshift_connector.paramstyle = 'pyformat'
166+
cursor.paramstyle = 'pyformat'
140167
sql = 'insert into foo(bar, jar) VALUES(%(bar)s, %(jar)s)'
141168
cursor.execute(sql, {"bar": 1, "jar": "hello world"})
142169

0 commit comments

Comments
 (0)