Skip to content

Commit f6e99ba

Browse files
committed
docs(cursor, paramstyle): add explanation and examples
1 parent fc77787 commit f6e99ba

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ Enabling autocommit
8686
con.run("VACUUM")
8787
con.autocommit = False
8888
89+
90+
Configuring cursor paramstyle
91+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92+
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``.
93+
94+
.. code-block:: python
95+
96+
# qmark
97+
redshift_connector.paramstyle = 'qmark'
98+
sql = 'insert into foo(bar, jar) VALUES(?, ?)'
99+
cursor.execute(sql, (1, "hello world"))
100+
101+
# numeric
102+
redshift_connector.paramstyle = 'numeric'
103+
sql = 'insert into foo(bar, jar) VALUES(:1, :2)'
104+
cursor.execute(sql, (1, "hello world"))
105+
106+
# named
107+
redshift_connector.paramstyle = 'numeric'
108+
sql = 'insert into foo(bar, jar) VALUES(:p1, :p2)'
109+
cursor.execute(sql, p1=1, p2="hello world")
110+
111+
# format
112+
redshift_connector.paramstyle = 'format'
113+
sql = 'insert into foo(bar, jar) VALUES(%s, %s)'
114+
cursor.execute(sql, (1, "hello world"))
115+
116+
# pyformat
117+
redshift_connector.paramstyle = 'pyformat'
118+
sql = 'insert into foo(bar, jar) VALUES(%(bar)s, %(jar)s)'
119+
cursor.execute(sql, {"bar": 1, "jar": "hello world"})
120+
121+
122+
89123
Example using IAM Credentials
90124
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91125
IAM Credentials can be supplied directly to ``connect(...)`` using an AWS profile as shown below:

0 commit comments

Comments
 (0)