Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c415440

Browse files
committed
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.
1 parent 00eaa8a commit c415440

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

Doc/library/sqlite3.rst

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,26 @@ Connection Objects
309309
call :meth:`commit`. If you just close your database connection without
310310
calling :meth:`commit` first, your changes will be lost!
311311

312-
.. method:: execute(sql, [parameters])
312+
.. method:: execute(sql[, parameters])
313313

314-
This is a nonstandard shortcut that creates an intermediate cursor object by
315-
calling the cursor method, then calls the cursor's :meth:`execute
316-
<Cursor.execute>` method with the parameters given.
314+
This is a nonstandard shortcut that creates a cursor object by calling
315+
the :meth:`~Connection.cursor` method, calls the cursor's
316+
:meth:`~Cursor.execute` method with the *parameters* given, and returns
317+
the cursor.
317318

319+
.. method:: executemany(sql[, parameters])
318320

319-
.. method:: executemany(sql, [parameters])
320-
321-
This is a nonstandard shortcut that creates an intermediate cursor object by
322-
calling the cursor method, then calls the cursor's :meth:`executemany
323-
<Cursor.executemany>` method with the parameters given.
321+
This is a nonstandard shortcut that creates a cursor object by
322+
calling the :meth:`~Connection.cursor` method, calls the cursor's
323+
:meth:`~Cursor.executemany` method with the *parameters* given, and
324+
returns the cursor.
324325

325326
.. method:: executescript(sql_script)
326327

327-
This is a nonstandard shortcut that creates an intermediate cursor object by
328-
calling the cursor method, then calls the cursor's :meth:`executescript
329-
<Cursor.executescript>` method with the parameters given.
330-
328+
This is a nonstandard shortcut that creates a cursor object by
329+
calling the :meth:`~Connection.cursor` method, calls the cursor's
330+
:meth:`~Cursor.executescript` method with the given *sql_script*, and
331+
returns the cursor.
331332

332333
.. method:: create_function(name, num_params, func)
333334

@@ -533,7 +534,7 @@ Cursor Objects
533534

534535
A :class:`Cursor` instance has the following attributes and methods.
535536

536-
.. method:: execute(sql, [parameters])
537+
.. method:: execute(sql[, parameters])
537538

538539
Executes an SQL statement. The SQL statement may be parameterized (i. e.
539540
placeholders instead of SQL literals). The :mod:`sqlite3` module supports two
@@ -545,16 +546,16 @@ Cursor Objects
545546
.. literalinclude:: ../includes/sqlite3/execute_1.py
546547

547548
:meth:`execute` will only execute a single SQL statement. If you try to execute
548-
more than one statement with it, it will raise a Warning. Use
549+
more than one statement with it, it will raise an ``sqlite3.Warning``. Use
549550
:meth:`executescript` if you want to execute multiple SQL statements with one
550551
call.
551552

552553

553554
.. method:: executemany(sql, seq_of_parameters)
554555

555556
Executes an SQL command against all parameter sequences or mappings found in
556-
the sequence *sql*. The :mod:`sqlite3` module also allows using an
557-
:term:`iterator` yielding parameters instead of a sequence.
557+
the sequence *seq_of_parameters*. The :mod:`sqlite3` module also allows
558+
using an :term:`iterator` yielding parameters instead of a sequence.
558559

559560
.. literalinclude:: ../includes/sqlite3/executemany_1.py
560561

@@ -569,7 +570,7 @@ Cursor Objects
569570
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
570571
gets as a parameter.
571572

572-
*sql_script* can be an instance of :class:`str` or :class:`bytes`.
573+
*sql_script* can be an instance of :class:`str`.
573574

574575
Example:
575576

Lib/sqlite3/test/dbapi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ def CheckExecuteArgStringWithZeroByte(self):
250250
row = self.cu.fetchone()
251251
self.assertEqual(row[0], "Hu\x00go")
252252

253+
def CheckExecuteNonIterable(self):
254+
with self.assertRaises(ValueError) as cm:
255+
self.cu.execute("insert into test(id) values (?)", 42)
256+
self.assertEqual(str(cm.exception), 'parameters are of unsupported type')
257+
253258
def CheckExecuteWrongNoOfArgs1(self):
254259
# too many parameters
255260
try:
@@ -725,6 +730,13 @@ def CheckScriptErrorNormal(self):
725730
raised = True
726731
self.assertEqual(raised, True, "should have raised an exception")
727732

733+
def CheckCursorExecutescriptAsBytes(self):
734+
con = sqlite.connect(":memory:")
735+
cur = con.cursor()
736+
with self.assertRaises(ValueError) as cm:
737+
cur.executescript(b"create table test(foo); insert into test(foo) values (5);")
738+
self.assertEqual(str(cm.exception), 'script argument must be unicode.')
739+
728740
def CheckConnectionExecute(self):
729741
con = sqlite.connect(":memory:")
730742
result = con.execute("select 5").fetchone()[0]

0 commit comments

Comments
 (0)