@@ -25,7 +25,7 @@ represents the database. Here the data will be stored in the
2525You can also supply the special name ``:memory: `` to create a database in RAM.
2626
2727Once you have a :class: `Connection `, you can create a :class: `Cursor ` object
28- and call its :meth: `execute ` method to perform SQL commands::
28+ and call its :meth: `~Cursor. execute ` method to perform SQL commands::
2929
3030 c = conn.cursor()
3131
@@ -50,7 +50,7 @@ is insecure; it makes your program vulnerable to an SQL injection attack.
5050
5151Instead, use the DB-API's parameter substitution. Put ``? `` as a placeholder
5252wherever you want to use a value, and then provide a tuple of values as the
53- second argument to the cursor's :meth: `execute ` method. (Other database modules
53+ second argument to the cursor's :meth: `~Cursor. execute ` method. (Other database modules
5454may use a different placeholder, such as ``%s `` or ``:1 ``.) For example::
5555
5656 # Never do this -- insecure!
@@ -69,8 +69,8 @@ may use a different placeholder, such as ``%s`` or ``:1``.) For example::
6969 c.execute('insert into stocks values (?,?,?,?,?)', t)
7070
7171To retrieve data after executing a SELECT statement, you can either treat the
72- cursor as an :term: `iterator `, call the cursor's :meth: `fetchone ` method to
73- retrieve a single matching row, or call :meth: `fetchall ` to get a list of the
72+ cursor as an :term: `iterator `, call the cursor's :meth: `~Cursor. fetchone ` method to
73+ retrieve a single matching row, or call :meth: `~Cursor. fetchall ` to get a list of the
7474matching rows.
7575
7676This example uses the iterator form::
@@ -128,7 +128,7 @@ Module functions and constants
128128 returns. It will look for a string formed [mytype] in there, and then decide
129129 that 'mytype' is the type of the column. It will try to find an entry of
130130 'mytype' in the converters dictionary and then use the converter function found
131- there to return the value. The column name found in :attr: `cursor .description `
131+ there to return the value. The column name found in :attr: `Cursor .description `
132132 is only the first word of the column name, i. e. if you use something like
133133 ``'as "x [datetime]"' `` in your SQL, then we will parse out everything until the
134134 first blank for the column name: the column name would simply be "x".
@@ -215,11 +215,13 @@ Module functions and constants
215215Connection Objects
216216------------------
217217
218- A :class: `Connection ` instance has the following attributes and methods:
218+ .. class :: Connection
219+
220+ A SQLite database connection has the following attributes and methods:
219221
220222.. attribute :: Connection.isolation_level
221223
222- Get or set the current isolation level. None for autocommit mode or one of
224+ Get or set the current isolation level. :const: ` None ` for autocommit mode or one of
223225 "DEFERRED", "IMMEDIATE" or "EXLUSIVE". See section
224226 :ref: `sqlite3-controlling-transactions ` for a more detailed explanation.
225227
@@ -234,7 +236,7 @@ A :class:`Connection` instance has the following attributes and methods:
234236.. method :: Connection.commit()
235237
236238 This method commits the current transaction. If you don't call this method,
237- anything you did since the last call to commit() is not visible from from
239+ anything you did since the last call to `` commit() `` is not visible from from
238240 other database connections. If you wonder why you don't see the data you've
239241 written to the database, please check you didn't forget to call this method.
240242
@@ -383,9 +385,9 @@ A :class:`Connection` instance has the following attributes and methods:
383385
384386 .. attribute :: Connection.text_factory
385387
386- Using this attribute you can control what objects are returned for the TEXT data
387- type. By default, this attribute is set to :class: `str ` and the
388- :mod: `sqlite3 ` module will return strings for TEXT. If you want to
388+ Using this attribute you can control what objects are returned for the `` TEXT ``
389+ data type. By default, this attribute is set to :class: `str ` and the
390+ :mod: `sqlite3 ` module will return Unicode objects for `` TEXT `` . If you want to
389391 return bytestrings instead, you can set it to :class: `bytes `.
390392
391393 For efficiency reasons, there's also a way to return :class: `str ` objects
@@ -430,8 +432,9 @@ A :class:`Connection` instance has the following attributes and methods:
430432Cursor Objects
431433--------------
432434
433- A : class:` Cursor ` instance has the following attributes and methods:
435+ .. class :: Cursor
434436
437+ A SQLite database cursor has the following attributes and methods:
435438
436439.. method :: Cursor.execute(sql, [parameters])
437440
@@ -470,7 +473,7 @@ A :class:`Cursor` instance has the following attributes and methods:
470473.. method :: Cursor.executescript(sql_script)
471474
472475 This is a nonstandard convenience method for executing multiple SQL statements
473- at once. It issues a COMMIT statement first, then executes the SQL script it
476+ at once. It issues a `` COMMIT `` statement first, then executes the SQL script it
474477 gets as a parameter.
475478
476479 *sql_script * can be an instance of :class: `str ` or :class: `bytes `.
@@ -483,7 +486,7 @@ A :class:`Cursor` instance has the following attributes and methods:
483486.. method :: Cursor.fetchone()
484487
485488 Fetches the next row of a query result set, returning a single sequence,
486- or `` None ` ` when no more data is available.
489+ or :const: ` None ` when no more data is available.
487490
488491
489492.. method :: Cursor.fetchmany([size=cursor.arraysize])
@@ -522,8 +525,8 @@ A :class:`Cursor` instance has the following attributes and methods:
522525 into :attr: `rowcount `.
523526
524527 As required by the Python DB API Spec, the :attr: `rowcount ` attribute "is -1 in
525- case no executeXX() has been performed on the cursor or the rowcount of the last
526- operation is not determinable by the interface".
528+ case no `` executeXX() `` has been performed on the cursor or the rowcount of the
529+ last operation is not determinable by the interface".
527530
528531 This includes ``SELECT `` statements because we cannot determine the number of
529532 rows a query produced until all rows were fetched.
@@ -535,6 +538,81 @@ A :class:`Cursor` instance has the following attributes and methods:
535538 method. For operations other than ``INSERT `` or when :meth: `executemany ` is
536539 called, :attr: `lastrowid ` is set to :const: `None `.
537540
541+ .. attribute :: Cursor.description
542+
543+ This read-only attribute provides the column names of the last query. To
544+ remain compatible with the Python DB API, it returns a 7-tuple for each
545+ column where the last six items of each tuple are :const: `None `.
546+
547+ It is set for ``SELECT `` statements without any matching rows as well.
548+
549+ .. _sqlite3-row-objects :
550+
551+ Row Objects
552+ -----------
553+
554+ .. class :: Row
555+
556+ A :class: `Row ` instance serves as a highly optimized
557+ :attr: `~Connection.row_factory ` for :class: `Connection ` objects.
558+ It tries to mimic a tuple in most of its features.
559+
560+ It supports mapping access by column name and index, iteration,
561+ representation, equality testing and :func: `len `.
562+
563+ If two :class: `Row ` objects have exactly the same columns and their
564+ members are equal, they compare equal.
565+
566+ .. versionchanged :: 2.6
567+ Added iteration and equality (hashability).
568+
569+ .. method :: keys
570+
571+ This method returns a tuple of column names. Immediately after a query,
572+ it is the first member of each tuple in :attr: `Cursor.description `.
573+
574+ .. versionadded :: 2.6
575+
576+ Let's assume we initialize a table as in the example given above::
577+
578+ conn = sqlite3.connect(":memory:")
579+ c = conn.cursor()
580+ c.execute('''create table stocks
581+ (date text, trans text, symbol text,
582+ qty real, price real)''')
583+ c.execute("""insert into stocks
584+ values ('2006-01-05','BUY','RHAT',100,35.14)""")
585+ conn.commit()
586+ c.close()
587+
588+ Now we plug :class: `Row ` in::
589+
590+ >>> conn.row_factory = sqlite3.Row
591+ >>> c = conn.cursor()
592+ >>> c.execute('select * from stocks')
593+ <sqlite3.Cursor object at 0x7f4e7dd8fa80>
594+ >>> r = c.fetchone()
595+ >>> type(r)
596+ <type 'sqlite3.Row'>
597+ >>> r
598+ (u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.140000000000001)
599+ >>> len(r)
600+ 5
601+ >>> r[2]
602+ u'RHAT'
603+ >>> r.keys()
604+ ['date', 'trans', 'symbol', 'qty', 'price']
605+ >>> r['qty']
606+ 100.0
607+ >>> for member in r: print member
608+ ...
609+ 2006-01-05
610+ BUY
611+ RHAT
612+ 100.0
613+ 35.14
614+
615+
538616.. _sqlite3-types :
539617
540618SQLite and Python types
@@ -544,36 +622,38 @@ SQLite and Python types
544622Introduction
545623^^^^^^^^^^^^
546624
547- SQLite natively supports the following types: NULL, INTEGER, REAL, TEXT, BLOB.
625+ SQLite natively supports the following types: ``NULL ``, ``INTEGER ``,
626+ ``REAL ``, ``TEXT ``, ``BLOB ``.
548627
549628The following Python types can thus be sent to SQLite without any problem:
550629
551630+-------------------------------+-------------+
552631| Python type | SQLite type |
553632+===============================+=============+
554- | `` None `` | NULL |
633+ | :const: ` None ` | `` NULL `` |
555634+-------------------------------+-------------+
556- | :class: `int ` | INTEGER |
635+ | :class: `int ` | `` INTEGER `` |
557636+-------------------------------+-------------+
558- | :class: `float ` | REAL |
637+ | :class: `float ` | `` REAL `` |
559638+-------------------------------+-------------+
560- | :class: `bytes ` (UTF8-encoded) | TEXT |
639+ | :class: `bytes ` (UTF8-encoded) | `` TEXT `` |
561640+-------------------------------+-------------+
562- | :class: `str ` | TEXT |
641+ | :class: `str ` | `` TEXT `` |
563642+-------------------------------+-------------+
564- | :class: `buffer ` | BLOB |
643+ | :class: `buffer ` | `` BLOB `` |
565644+-------------------------------+-------------+
566645
646+
567647This is how SQLite types are converted to Python types by default:
568648
569649+-------------+---------------------------------------------+
570650| SQLite type | Python type |
571651+=============+=============================================+
572- | ``NULL `` | None |
652+ | ``NULL `` | :const: ` None ` |
573653+-------------+---------------------------------------------+
574- | ``INTEGER `` | int |
654+ | ``INTEGER `` | :class` int` |
575655+-------------+---------------------------------------------+
576- | ``REAL `` | float |
656+ | ``REAL `` | :class: ` float ` |
577657+-------------+---------------------------------------------+
578658| ``TEXT `` | depends on text_factory, str by default |
579659+-------------+---------------------------------------------+
@@ -701,9 +781,10 @@ Controlling Transactions
701781------------------------
702782
703783By default, the :mod: `sqlite3 ` module opens transactions implicitly before a
704- Data Modification Language (DML) statement (i.e. INSERT/UPDATE/DELETE/REPLACE),
705- and commits transactions implicitly before a non-DML, non-query statement (i. e.
706- anything other than SELECT/INSERT/UPDATE/DELETE/REPLACE).
784+ Data Modification Language (DML) statement (i.e.
785+ ``INSERT ``/``UPDATE ``/``DELETE ``/``REPLACE ``), and commits transactions
786+ implicitly before a non-DML, non-query statement (i. e.
787+ anything other than ``SELECT `` or the aforementioned).
707788
708789So if you are within a transaction and issue a command like ``CREATE TABLE
709790... ``, ``VACUUM ``, ``PRAGMA ``, the :mod: `sqlite3 ` module will commit implicitly
@@ -712,7 +793,7 @@ is that some of these commands don't work within transactions. The other reason
712793is that pysqlite needs to keep track of the transaction state (if a transaction
713794is active or not).
714795
715- You can control which kind of " BEGIN" statements pysqlite implicitly executes
796+ You can control which kind of `` BEGIN `` statements pysqlite implicitly executes
716797(or none at all) via the *isolation_level * parameter to the :func: `connect `
717798call, or via the :attr: `isolation_level ` property of connections.
718799
@@ -736,7 +817,7 @@ Using the nonstandard :meth:`execute`, :meth:`executemany` and
736817be written more concisely because you don't have to create the (often
737818superfluous) :class: `Cursor ` objects explicitly. Instead, the :class: `Cursor `
738819objects are created implicitly and these shortcut methods return the cursor
739- objects. This way, you can execute a SELECT statement and iterate over it
820+ objects. This way, you can execute a `` SELECT `` statement and iterate over it
740821directly using only a single call on the :class: `Connection ` object.
741822
742823.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
0 commit comments