You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+34-7Lines changed: 34 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -109,34 +109,61 @@ Enabling autocommit
109
109
conn.autocommit = False
110
110
111
111
112
-
Configuring cursor paramstyle
112
+
Configuring paramstyle
113
113
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
115
116
116
117
.. code-block:: python
117
118
118
-
# qmark
119
+
#setting paramstyle to qmark on a module level
119
120
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'
120
147
sql ='insert into foo(bar, jar) VALUES(?, ?)'
121
148
cursor.execute(sql, (1, "hello world"))
122
149
123
150
# numeric
124
-
redshift_connector.paramstyle ='numeric'
151
+
cursor.paramstyle ='numeric'
125
152
sql ='insert into foo(bar, jar) VALUES(:1, :2)'
126
153
cursor.execute(sql, (1, "hello world"))
127
154
128
155
# named
129
-
redshift_connector.paramstyle ='named'
156
+
cursor.paramstyle ='named'
130
157
sql ='insert into foo(bar, jar) VALUES(:p1, :p2)'
131
158
cursor.execute(sql, {"p1":1, "p2":"hello world"})
132
159
133
160
# format
134
-
redshift_connector.paramstyle ='format'
161
+
cursor.paramstyle ='format'
135
162
sql ='insert into foo(bar, jar) VALUES(%s, %s)'
136
163
cursor.execute(sql, (1, "hello world"))
137
164
138
165
# pyformat
139
-
redshift_connector.paramstyle ='pyformat'
166
+
cursor.paramstyle ='pyformat'
140
167
sql ='insert into foo(bar, jar) VALUES(%(bar)s, %(jar)s)'
0 commit comments