Commit 15f0f421 authored by Berker Peksag's avatar Berker Peksag

Issue #27188: Fix various sqlite3 documentation errors

* Connection.execute* methods don't create intermediate cursor objects
* Fix description of seq_of_parameters parameter
* Clarify that Warning is sqlite3.Warning
* sql_script parameter of Cursor.executescript() doesn't accept bytes
* Add missing tests
* Fix various markup errors

Initial patch by Dave Sawyer.
parent e4d5529b
...@@ -309,25 +309,26 @@ Connection Objects ...@@ -309,25 +309,26 @@ Connection Objects
call :meth:`commit`. If you just close your database connection without call :meth:`commit`. If you just close your database connection without
calling :meth:`commit` first, your changes will be lost! calling :meth:`commit` first, your changes will be lost!
.. method:: execute(sql, [parameters]) .. method:: execute(sql[, parameters])
This is a nonstandard shortcut that creates an intermediate cursor object by This is a nonstandard shortcut that creates a cursor object by calling
calling the cursor method, then calls the cursor's :meth:`execute the :meth:`~Connection.cursor` method, calls the cursor's
<Cursor.execute>` method with the parameters given. :meth:`~Cursor.execute` method with the *parameters* given, and returns
the cursor.
.. method:: executemany(sql[, parameters])
.. method:: executemany(sql, [parameters]) This is a nonstandard shortcut that creates a cursor object by
calling the :meth:`~Connection.cursor` method, calls the cursor's
This is a nonstandard shortcut that creates an intermediate cursor object by :meth:`~Cursor.executemany` method with the *parameters* given, and
calling the cursor method, then calls the cursor's :meth:`executemany returns the cursor.
<Cursor.executemany>` method with the parameters given.
.. method:: executescript(sql_script) .. method:: executescript(sql_script)
This is a nonstandard shortcut that creates an intermediate cursor object by This is a nonstandard shortcut that creates a cursor object by
calling the cursor method, then calls the cursor's :meth:`executescript calling the :meth:`~Connection.cursor` method, calls the cursor's
<Cursor.executescript>` method with the parameters given. :meth:`~Cursor.executescript` method with the given *sql_script*, and
returns the cursor.
.. method:: create_function(name, num_params, func) .. method:: create_function(name, num_params, func)
...@@ -533,7 +534,7 @@ Cursor Objects ...@@ -533,7 +534,7 @@ Cursor Objects
A :class:`Cursor` instance has the following attributes and methods. A :class:`Cursor` instance has the following attributes and methods.
.. method:: execute(sql, [parameters]) .. method:: execute(sql[, parameters])
Executes an SQL statement. The SQL statement may be parameterized (i. e. Executes an SQL statement. The SQL statement may be parameterized (i. e.
placeholders instead of SQL literals). The :mod:`sqlite3` module supports two placeholders instead of SQL literals). The :mod:`sqlite3` module supports two
...@@ -545,7 +546,7 @@ Cursor Objects ...@@ -545,7 +546,7 @@ Cursor Objects
.. literalinclude:: ../includes/sqlite3/execute_1.py .. literalinclude:: ../includes/sqlite3/execute_1.py
:meth:`execute` will only execute a single SQL statement. If you try to execute :meth:`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 more than one statement with it, it will raise an ``sqlite3.Warning``. Use
:meth:`executescript` if you want to execute multiple SQL statements with one :meth:`executescript` if you want to execute multiple SQL statements with one
call. call.
...@@ -553,8 +554,8 @@ Cursor Objects ...@@ -553,8 +554,8 @@ Cursor Objects
.. method:: executemany(sql, seq_of_parameters) .. method:: executemany(sql, seq_of_parameters)
Executes an SQL command against all parameter sequences or mappings found in Executes an SQL command against all parameter sequences or mappings found in
the sequence *sql*. The :mod:`sqlite3` module also allows using an the sequence *seq_of_parameters*. The :mod:`sqlite3` module also allows
:term:`iterator` yielding parameters instead of a sequence. using an :term:`iterator` yielding parameters instead of a sequence.
.. literalinclude:: ../includes/sqlite3/executemany_1.py .. literalinclude:: ../includes/sqlite3/executemany_1.py
...@@ -569,7 +570,7 @@ Cursor Objects ...@@ -569,7 +570,7 @@ Cursor Objects
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
gets as a parameter. gets as a parameter.
*sql_script* can be an instance of :class:`str` or :class:`bytes`. *sql_script* can be an instance of :class:`str`.
Example: Example:
......
...@@ -250,6 +250,11 @@ class CursorTests(unittest.TestCase): ...@@ -250,6 +250,11 @@ class CursorTests(unittest.TestCase):
row = self.cu.fetchone() row = self.cu.fetchone()
self.assertEqual(row[0], "Hu\x00go") self.assertEqual(row[0], "Hu\x00go")
def CheckExecuteNonIterable(self):
with self.assertRaises(ValueError) as cm:
self.cu.execute("insert into test(id) values (?)", 42)
self.assertEqual(str(cm.exception), 'parameters are of unsupported type')
def CheckExecuteWrongNoOfArgs1(self): def CheckExecuteWrongNoOfArgs1(self):
# too many parameters # too many parameters
try: try:
...@@ -725,6 +730,13 @@ class ExtensionTests(unittest.TestCase): ...@@ -725,6 +730,13 @@ class ExtensionTests(unittest.TestCase):
raised = True raised = True
self.assertEqual(raised, True, "should have raised an exception") self.assertEqual(raised, True, "should have raised an exception")
def CheckCursorExecutescriptAsBytes(self):
con = sqlite.connect(":memory:")
cur = con.cursor()
with self.assertRaises(ValueError) as cm:
cur.executescript(b"create table test(foo); insert into test(foo) values (5);")
self.assertEqual(str(cm.exception), 'script argument must be unicode.')
def CheckConnectionExecute(self): def CheckConnectionExecute(self):
con = sqlite.connect(":memory:") con = sqlite.connect(":memory:")
result = con.execute("select 5").fetchone()[0] result = con.execute("select 5").fetchone()[0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment