Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`sqlite3` --- DB-API 2.0 interface for SQLite databases |
| 2 | ============================================================ |
| 3 | |
| 4 | .. module:: sqlite3 |
| 5 | :synopsis: A DB-API 2.0 implementation using SQLite 3.x. |
| 6 | .. sectionauthor:: Gerhard Häring <gh@ghaering.de> |
| 7 | |
| 8 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | SQLite is a C library that provides a lightweight disk-based database that |
| 10 | doesn't require a separate server process and allows accessing the database |
| 11 | using a nonstandard variant of the SQL query language. Some applications can use |
| 12 | SQLite for internal data storage. It's also possible to prototype an |
| 13 | application using SQLite and then port the code to a larger database such as |
| 14 | PostgreSQL or Oracle. |
| 15 | |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 16 | sqlite3 was written by Gerhard Häring and provides a SQL interface compliant |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | with the DB-API 2.0 specification described by :pep:`249`. |
| 18 | |
| 19 | To use the module, you must first create a :class:`Connection` object that |
| 20 | represents the database. Here the data will be stored in the |
| 21 | :file:`/tmp/example` file:: |
| 22 | |
| 23 | conn = sqlite3.connect('/tmp/example') |
| 24 | |
| 25 | You can also supply the special name ``:memory:`` to create a database in RAM. |
| 26 | |
| 27 | Once you have a :class:`Connection`, you can create a :class:`Cursor` object |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 28 | and call its :meth:`~Cursor.execute` method to perform SQL commands:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
| 30 | c = conn.cursor() |
| 31 | |
| 32 | # Create table |
| 33 | c.execute('''create table stocks |
| 34 | (date text, trans text, symbol text, |
| 35 | qty real, price real)''') |
| 36 | |
| 37 | # Insert a row of data |
| 38 | c.execute("""insert into stocks |
| 39 | values ('2006-01-05','BUY','RHAT',100,35.14)""") |
| 40 | |
| 41 | # Save (commit) the changes |
| 42 | conn.commit() |
| 43 | |
| 44 | # We can also close the cursor if we are done with it |
| 45 | c.close() |
| 46 | |
| 47 | Usually your SQL operations will need to use values from Python variables. You |
| 48 | shouldn't assemble your query using Python's string operations because doing so |
| 49 | is insecure; it makes your program vulnerable to an SQL injection attack. |
| 50 | |
| 51 | Instead, use the DB-API's parameter substitution. Put ``?`` as a placeholder |
| 52 | wherever you want to use a value, and then provide a tuple of values as the |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 53 | second argument to the cursor's :meth:`~Cursor.execute` method. (Other database |
| 54 | modules may use a different placeholder, such as ``%s`` or ``:1``.) For |
| 55 | example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 57 | # Never do this -- insecure! |
| 58 | symbol = 'IBM' |
| 59 | c.execute("... where symbol = '%s'" % symbol) |
| 60 | |
| 61 | # Do this instead |
| 62 | t = (symbol,) |
| 63 | c.execute('select * from stocks where symbol=?', t) |
| 64 | |
| 65 | # Larger example |
Georg Brandl | a971c65 | 2008-11-07 09:39:56 +0000 | [diff] [blame] | 66 | for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00), |
| 68 | ('2006-04-06', 'SELL', 'IBM', 500, 53.00), |
Georg Brandl | a971c65 | 2008-11-07 09:39:56 +0000 | [diff] [blame] | 69 | ]: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | c.execute('insert into stocks values (?,?,?,?,?)', t) |
| 71 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 72 | To retrieve data after executing a SELECT statement, you can either treat the |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 73 | cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to |
| 74 | retrieve a single matching row, or call :meth:`~Cursor.fetchall` to get a list of the |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 75 | matching rows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | This example uses the iterator form:: |
| 78 | |
| 79 | >>> c = conn.cursor() |
| 80 | >>> c.execute('select * from stocks order by price') |
| 81 | >>> for row in c: |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 82 | ... print(row) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | ... |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 84 | ('2006-01-05', 'BUY', 'RHAT', 100, 35.14) |
| 85 | ('2006-03-28', 'BUY', 'IBM', 1000, 45.0) |
| 86 | ('2006-04-06', 'SELL', 'IBM', 500, 53.0) |
| 87 | ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | >>> |
| 89 | |
| 90 | |
| 91 | .. seealso:: |
| 92 | |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 93 | http://code.google.com/p/pysqlite/ |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 94 | The pysqlite web page -- sqlite3 is developed externally under the name |
| 95 | "pysqlite". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
| 97 | http://www.sqlite.org |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 98 | The SQLite web page; the documentation describes the syntax and the |
| 99 | available data types for the supported SQL dialect. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | :pep:`249` - Database API Specification 2.0 |
| 102 | PEP written by Marc-André Lemburg. |
| 103 | |
| 104 | |
| 105 | .. _sqlite3-module-contents: |
| 106 | |
| 107 | Module functions and constants |
| 108 | ------------------------------ |
| 109 | |
| 110 | |
| 111 | .. data:: PARSE_DECLTYPES |
| 112 | |
| 113 | This constant is meant to be used with the *detect_types* parameter of the |
| 114 | :func:`connect` function. |
| 115 | |
| 116 | Setting it makes the :mod:`sqlite3` module parse the declared type for each |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 117 | column it returns. It will parse out the first word of the declared type, |
| 118 | i. e. for "integer primary key", it will parse out "integer", or for |
| 119 | "number(10)" it will parse out "number". Then for that column, it will look |
| 120 | into the converters dictionary and use the converter function registered for |
| 121 | that type there. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | .. data:: PARSE_COLNAMES |
| 125 | |
| 126 | This constant is meant to be used with the *detect_types* parameter of the |
| 127 | :func:`connect` function. |
| 128 | |
| 129 | Setting this makes the SQLite interface parse the column name for each column it |
| 130 | returns. It will look for a string formed [mytype] in there, and then decide |
| 131 | that 'mytype' is the type of the column. It will try to find an entry of |
| 132 | 'mytype' in the converters dictionary and then use the converter function found |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 133 | there to return the value. The column name found in :attr:`Cursor.description` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | is only the first word of the column name, i. e. if you use something like |
| 135 | ``'as "x [datetime]"'`` in your SQL, then we will parse out everything until the |
| 136 | first blank for the column name: the column name would simply be "x". |
| 137 | |
| 138 | |
| 139 | .. function:: connect(database[, timeout, isolation_level, detect_types, factory]) |
| 140 | |
| 141 | Opens a connection to the SQLite database file *database*. You can use |
| 142 | ``":memory:"`` to open a database connection to a database that resides in RAM |
| 143 | instead of on disk. |
| 144 | |
| 145 | When a database is accessed by multiple connections, and one of the processes |
| 146 | modifies the database, the SQLite database is locked until that transaction is |
| 147 | committed. The *timeout* parameter specifies how long the connection should wait |
| 148 | for the lock to go away until raising an exception. The default for the timeout |
| 149 | parameter is 5.0 (five seconds). |
| 150 | |
| 151 | For the *isolation_level* parameter, please see the |
| 152 | :attr:`Connection.isolation_level` property of :class:`Connection` objects. |
| 153 | |
| 154 | SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. If |
| 155 | you want to use other types you must add support for them yourself. The |
| 156 | *detect_types* parameter and the using custom **converters** registered with the |
| 157 | module-level :func:`register_converter` function allow you to easily do that. |
| 158 | |
| 159 | *detect_types* defaults to 0 (i. e. off, no type detection), you can set it to |
| 160 | any combination of :const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES` to turn |
| 161 | type detection on. |
| 162 | |
| 163 | By default, the :mod:`sqlite3` module uses its :class:`Connection` class for the |
| 164 | connect call. You can, however, subclass the :class:`Connection` class and make |
| 165 | :func:`connect` use your class instead by providing your class for the *factory* |
| 166 | parameter. |
| 167 | |
| 168 | Consult the section :ref:`sqlite3-types` of this manual for details. |
| 169 | |
| 170 | The :mod:`sqlite3` module internally uses a statement cache to avoid SQL parsing |
| 171 | overhead. If you want to explicitly set the number of statements that are cached |
| 172 | for the connection, you can set the *cached_statements* parameter. The currently |
| 173 | implemented default is to cache 100 statements. |
| 174 | |
| 175 | |
| 176 | .. function:: register_converter(typename, callable) |
| 177 | |
| 178 | Registers a callable to convert a bytestring from the database into a custom |
| 179 | Python type. The callable will be invoked for all database values that are of |
| 180 | the type *typename*. Confer the parameter *detect_types* of the :func:`connect` |
| 181 | function for how the type detection works. Note that the case of *typename* and |
| 182 | the name of the type in your query must match! |
| 183 | |
| 184 | |
| 185 | .. function:: register_adapter(type, callable) |
| 186 | |
| 187 | Registers a callable to convert the custom Python type *type* into one of |
| 188 | SQLite's supported types. The callable *callable* accepts as single parameter |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 189 | the Python value, and must return a value of the following types: int, |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 190 | float, str or bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | .. function:: complete_statement(sql) |
| 194 | |
| 195 | Returns :const:`True` if the string *sql* contains one or more complete SQL |
| 196 | statements terminated by semicolons. It does not verify that the SQL is |
| 197 | syntactically correct, only that there are no unclosed string literals and the |
| 198 | statement is terminated by a semicolon. |
| 199 | |
| 200 | This can be used to build a shell for SQLite, as in the following example: |
| 201 | |
| 202 | |
| 203 | .. literalinclude:: ../includes/sqlite3/complete_statement.py |
| 204 | |
| 205 | |
| 206 | .. function:: enable_callback_tracebacks(flag) |
| 207 | |
| 208 | By default you will not get any tracebacks in user-defined functions, |
| 209 | aggregates, converters, authorizer callbacks etc. If you want to debug them, you |
| 210 | can call this function with *flag* as True. Afterwards, you will get tracebacks |
| 211 | from callbacks on ``sys.stderr``. Use :const:`False` to disable the feature |
| 212 | again. |
| 213 | |
| 214 | |
| 215 | .. _sqlite3-connection-objects: |
| 216 | |
| 217 | Connection Objects |
| 218 | ------------------ |
| 219 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 220 | .. class:: Connection |
| 221 | |
| 222 | A SQLite database connection has the following attributes and methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
| 224 | .. attribute:: Connection.isolation_level |
| 225 | |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 226 | Get or set the current isolation level. :const:`None` for autocommit mode or |
| 227 | one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 | :ref:`sqlite3-controlling-transactions` for a more detailed explanation. |
| 229 | |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 230 | .. attribute:: Connection.in_transaction |
| 231 | |
| 232 | .. versionadded:: 3.2 |
| 233 | |
Benjamin Peterson | 5c5eb36 | 2010-06-06 02:40:38 +0000 | [diff] [blame^] | 234 | :const:`True` if a transaction is active (there are uncommitted changes), |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 235 | :const:`False` otherwise. Read-only attribute. |
| 236 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
| 238 | .. method:: Connection.cursor([cursorClass]) |
| 239 | |
| 240 | The cursor method accepts a single optional parameter *cursorClass*. If |
| 241 | supplied, this must be a custom cursor class that extends |
| 242 | :class:`sqlite3.Cursor`. |
| 243 | |
| 244 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 245 | .. method:: Connection.commit() |
| 246 | |
| 247 | This method commits the current transaction. If you don't call this method, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 248 | anything you did since the last call to ``commit()`` is not visible from from |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 249 | other database connections. If you wonder why you don't see the data you've |
| 250 | written to the database, please check you didn't forget to call this method. |
| 251 | |
| 252 | .. method:: Connection.rollback() |
| 253 | |
| 254 | This method rolls back any changes to the database since the last call to |
| 255 | :meth:`commit`. |
| 256 | |
| 257 | .. method:: Connection.close() |
| 258 | |
| 259 | This closes the database connection. Note that this does not automatically |
| 260 | call :meth:`commit`. If you just close your database connection without |
| 261 | calling :meth:`commit` first, your changes will be lost! |
| 262 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | .. method:: Connection.execute(sql, [parameters]) |
| 264 | |
| 265 | This is a nonstandard shortcut that creates an intermediate cursor object by |
Ezio Melotti | a10a2e8 | 2010-02-14 02:54:22 +0000 | [diff] [blame] | 266 | calling the cursor method, then calls the cursor's |
| 267 | :meth:`execute<Cursor.execute>` method with the parameters given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | .. method:: Connection.executemany(sql, [parameters]) |
| 271 | |
| 272 | This is a nonstandard shortcut that creates an intermediate cursor object by |
Ezio Melotti | a10a2e8 | 2010-02-14 02:54:22 +0000 | [diff] [blame] | 273 | calling the cursor method, then calls the cursor's |
| 274 | :meth:`executemany<Cursor.executemany>` method with the parameters given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 275 | |
| 276 | |
| 277 | .. method:: Connection.executescript(sql_script) |
| 278 | |
| 279 | This is a nonstandard shortcut that creates an intermediate cursor object by |
Ezio Melotti | a10a2e8 | 2010-02-14 02:54:22 +0000 | [diff] [blame] | 280 | calling the cursor method, then calls the cursor's |
| 281 | :meth:`executescript<Cursor.executescript>` method with the parameters |
| 282 | given. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 283 | |
| 284 | |
| 285 | .. method:: Connection.create_function(name, num_params, func) |
| 286 | |
| 287 | Creates a user-defined function that you can later use from within SQL |
| 288 | statements under the function name *name*. *num_params* is the number of |
| 289 | parameters the function accepts, and *func* is a Python callable that is called |
| 290 | as the SQL function. |
| 291 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 292 | The function can return any of the types supported by SQLite: bytes, str, int, |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 293 | float and None. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | |
| 295 | Example: |
| 296 | |
| 297 | .. literalinclude:: ../includes/sqlite3/md5func.py |
| 298 | |
| 299 | |
| 300 | .. method:: Connection.create_aggregate(name, num_params, aggregate_class) |
| 301 | |
| 302 | Creates a user-defined aggregate function. |
| 303 | |
| 304 | The aggregate class must implement a ``step`` method, which accepts the number |
| 305 | of parameters *num_params*, and a ``finalize`` method which will return the |
| 306 | final result of the aggregate. |
| 307 | |
| 308 | The ``finalize`` method can return any of the types supported by SQLite: |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 309 | bytes, str, int, float and None. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
| 311 | Example: |
| 312 | |
| 313 | .. literalinclude:: ../includes/sqlite3/mysumaggr.py |
| 314 | |
| 315 | |
| 316 | .. method:: Connection.create_collation(name, callable) |
| 317 | |
| 318 | Creates a collation with the specified *name* and *callable*. The callable will |
| 319 | be passed two string arguments. It should return -1 if the first is ordered |
| 320 | lower than the second, 0 if they are ordered equal and 1 if the first is ordered |
| 321 | higher than the second. Note that this controls sorting (ORDER BY in SQL) so |
| 322 | your comparisons don't affect other SQL operations. |
| 323 | |
| 324 | Note that the callable will get its parameters as Python bytestrings, which will |
| 325 | normally be encoded in UTF-8. |
| 326 | |
| 327 | The following example shows a custom collation that sorts "the wrong way": |
| 328 | |
| 329 | .. literalinclude:: ../includes/sqlite3/collation_reverse.py |
| 330 | |
| 331 | To remove a collation, call ``create_collation`` with None as callable:: |
| 332 | |
| 333 | con.create_collation("reverse", None) |
| 334 | |
| 335 | |
| 336 | .. method:: Connection.interrupt() |
| 337 | |
| 338 | You can call this method from a different thread to abort any queries that might |
| 339 | be executing on the connection. The query will then abort and the caller will |
| 340 | get an exception. |
| 341 | |
| 342 | |
| 343 | .. method:: Connection.set_authorizer(authorizer_callback) |
| 344 | |
| 345 | This routine registers a callback. The callback is invoked for each attempt to |
| 346 | access a column of a table in the database. The callback should return |
| 347 | :const:`SQLITE_OK` if access is allowed, :const:`SQLITE_DENY` if the entire SQL |
| 348 | statement should be aborted with an error and :const:`SQLITE_IGNORE` if the |
| 349 | column should be treated as a NULL value. These constants are available in the |
| 350 | :mod:`sqlite3` module. |
| 351 | |
| 352 | The first argument to the callback signifies what kind of operation is to be |
| 353 | authorized. The second and third argument will be arguments or :const:`None` |
| 354 | depending on the first argument. The 4th argument is the name of the database |
| 355 | ("main", "temp", etc.) if applicable. The 5th argument is the name of the |
| 356 | inner-most trigger or view that is responsible for the access attempt or |
| 357 | :const:`None` if this access attempt is directly from input SQL code. |
| 358 | |
| 359 | Please consult the SQLite documentation about the possible values for the first |
| 360 | argument and the meaning of the second and third argument depending on the first |
| 361 | one. All necessary constants are available in the :mod:`sqlite3` module. |
| 362 | |
| 363 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 364 | .. method:: Connection.set_progress_handler(handler, n) |
| 365 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 366 | This routine registers a callback. The callback is invoked for every *n* |
| 367 | instructions of the SQLite virtual machine. This is useful if you want to |
| 368 | get called from SQLite during long-running operations, for example to update |
| 369 | a GUI. |
| 370 | |
| 371 | If you want to clear any previously installed progress handler, call the |
| 372 | method with :const:`None` for *handler*. |
| 373 | |
| 374 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 375 | .. method:: Connection.enable_load_extension(enabled) |
| 376 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 377 | .. versionadded:: 3.2 |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 378 | |
| 379 | This routine allows/disallows the SQLite engine to load SQLite extensions |
| 380 | from shared libraries. SQLite extensions can define new functions, |
| 381 | aggregates or whole new virtual table implementations. One well-known |
| 382 | extension is the fulltext-search extension distributed with SQLite. |
| 383 | |
| 384 | .. literalinclude:: ../includes/sqlite3/load_extension.py |
| 385 | |
| 386 | .. method:: Connection.load_extension(path) |
| 387 | |
Ezio Melotti | f8754a6 | 2010-03-21 07:16:43 +0000 | [diff] [blame] | 388 | .. versionadded:: 3.2 |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 389 | |
| 390 | This routine loads a SQLite extension from a shared library. You have to |
| 391 | enable extension loading with ``enable_load_extension`` before you can use |
| 392 | this routine. |
| 393 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 394 | .. attribute:: Connection.row_factory |
| 395 | |
| 396 | You can change this attribute to a callable that accepts the cursor and the |
| 397 | original row as a tuple and will return the real result row. This way, you can |
| 398 | implement more advanced ways of returning results, such as returning an object |
| 399 | that can also access columns by name. |
| 400 | |
| 401 | Example: |
| 402 | |
| 403 | .. literalinclude:: ../includes/sqlite3/row_factory.py |
| 404 | |
| 405 | If returning a tuple doesn't suffice and you want name-based access to |
| 406 | columns, you should consider setting :attr:`row_factory` to the |
| 407 | highly-optimized :class:`sqlite3.Row` type. :class:`Row` provides both |
| 408 | index-based and case-insensitive name-based access to columns with almost no |
| 409 | memory overhead. It will probably be better than your own custom |
| 410 | dictionary-based approach or even a db_row based solution. |
| 411 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 412 | .. XXX what's a db_row-based solution? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
| 414 | |
| 415 | .. attribute:: Connection.text_factory |
| 416 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 417 | Using this attribute you can control what objects are returned for the ``TEXT`` |
| 418 | data type. By default, this attribute is set to :class:`str` and the |
| 419 | :mod:`sqlite3` module will return Unicode objects for ``TEXT``. If you want to |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 420 | return bytestrings instead, you can set it to :class:`bytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 422 | For efficiency reasons, there's also a way to return :class:`str` objects |
| 423 | only for non-ASCII data, and :class:`bytes` otherwise. To activate it, set |
| 424 | this attribute to :const:`sqlite3.OptimizedUnicode`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | |
| 426 | You can also set it to any other callable that accepts a single bytestring |
| 427 | parameter and returns the resulting object. |
| 428 | |
| 429 | See the following example code for illustration: |
| 430 | |
| 431 | .. literalinclude:: ../includes/sqlite3/text_factory.py |
| 432 | |
| 433 | |
| 434 | .. attribute:: Connection.total_changes |
| 435 | |
| 436 | Returns the total number of database rows that have been modified, inserted, or |
| 437 | deleted since the database connection was opened. |
| 438 | |
| 439 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 440 | .. attribute:: Connection.iterdump |
| 441 | |
| 442 | Returns an iterator to dump the database in an SQL text format. Useful when |
| 443 | saving an in-memory database for later restoration. This function provides |
| 444 | the same capabilities as the :kbd:`.dump` command in the :program:`sqlite3` |
| 445 | shell. |
| 446 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 447 | Example:: |
| 448 | |
| 449 | # Convert file existing_db.db to SQL dump file dump.sql |
| 450 | import sqlite3, os |
| 451 | |
| 452 | con = sqlite3.connect('existing_db.db') |
Georg Brandl | a971c65 | 2008-11-07 09:39:56 +0000 | [diff] [blame] | 453 | with open('dump.sql', 'w') as f: |
| 454 | for line in con.iterdump(): |
| 455 | f.write('%s\n' % line) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 456 | |
| 457 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | .. _sqlite3-cursor-objects: |
| 459 | |
| 460 | Cursor Objects |
| 461 | -------------- |
| 462 | |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 463 | A :class:`Cursor` instance has the following attributes and methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 464 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 465 | A SQLite database cursor has the following attributes and methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | |
| 467 | .. method:: Cursor.execute(sql, [parameters]) |
| 468 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 469 | Executes an SQL statement. The SQL statement may be parametrized (i. e. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | placeholders instead of SQL literals). The :mod:`sqlite3` module supports two |
| 471 | kinds of placeholders: question marks (qmark style) and named placeholders |
| 472 | (named style). |
| 473 | |
| 474 | This example shows how to use parameters with qmark style: |
| 475 | |
| 476 | .. literalinclude:: ../includes/sqlite3/execute_1.py |
| 477 | |
| 478 | This example shows how to use the named style: |
| 479 | |
| 480 | .. literalinclude:: ../includes/sqlite3/execute_2.py |
| 481 | |
| 482 | :meth:`execute` will only execute a single SQL statement. If you try to execute |
| 483 | more than one statement with it, it will raise a Warning. Use |
| 484 | :meth:`executescript` if you want to execute multiple SQL statements with one |
| 485 | call. |
| 486 | |
| 487 | |
| 488 | .. method:: Cursor.executemany(sql, seq_of_parameters) |
| 489 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 490 | Executes an SQL command against all parameter sequences or mappings found in |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 491 | the sequence *sql*. The :mod:`sqlite3` module also allows using an |
| 492 | :term:`iterator` yielding parameters instead of a sequence. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 493 | |
| 494 | .. literalinclude:: ../includes/sqlite3/executemany_1.py |
| 495 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 496 | Here's a shorter example using a :term:`generator`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 497 | |
| 498 | .. literalinclude:: ../includes/sqlite3/executemany_2.py |
| 499 | |
| 500 | |
| 501 | .. method:: Cursor.executescript(sql_script) |
| 502 | |
| 503 | This is a nonstandard convenience method for executing multiple SQL statements |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 504 | at once. It issues a ``COMMIT`` statement first, then executes the SQL script it |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 505 | gets as a parameter. |
| 506 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 507 | *sql_script* can be an instance of :class:`str` or :class:`bytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | |
| 509 | Example: |
| 510 | |
| 511 | .. literalinclude:: ../includes/sqlite3/executescript.py |
| 512 | |
| 513 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 514 | .. method:: Cursor.fetchone() |
| 515 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 516 | Fetches the next row of a query result set, returning a single sequence, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 517 | or :const:`None` when no more data is available. |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 518 | |
| 519 | |
| 520 | .. method:: Cursor.fetchmany([size=cursor.arraysize]) |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 521 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 522 | Fetches the next set of rows of a query result, returning a list. An empty |
| 523 | list is returned when no more rows are available. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 524 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 525 | The number of rows to fetch per call is specified by the *size* parameter. |
| 526 | If it is not given, the cursor's arraysize determines the number of rows |
| 527 | to be fetched. The method should try to fetch as many rows as indicated by |
| 528 | the size parameter. If this is not possible due to the specified number of |
| 529 | rows not being available, fewer rows may be returned. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 530 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 531 | Note there are performance considerations involved with the *size* parameter. |
| 532 | For optimal performance, it is usually best to use the arraysize attribute. |
| 533 | If the *size* parameter is used, then it is best for it to retain the same |
| 534 | value from one :meth:`fetchmany` call to the next. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 535 | |
| 536 | .. method:: Cursor.fetchall() |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 537 | |
| 538 | Fetches all (remaining) rows of a query result, returning a list. Note that |
| 539 | the cursor's arraysize attribute can affect the performance of this operation. |
| 540 | An empty list is returned when no rows are available. |
| 541 | |
| 542 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 543 | .. attribute:: Cursor.rowcount |
| 544 | |
| 545 | Although the :class:`Cursor` class of the :mod:`sqlite3` module implements this |
| 546 | attribute, the database engine's own support for the determination of "rows |
| 547 | affected"/"rows selected" is quirky. |
| 548 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 549 | For ``DELETE`` statements, SQLite reports :attr:`rowcount` as 0 if you make a |
| 550 | ``DELETE FROM table`` without any condition. |
| 551 | |
| 552 | For :meth:`executemany` statements, the number of modifications are summed up |
| 553 | into :attr:`rowcount`. |
| 554 | |
| 555 | As required by the Python DB API Spec, the :attr:`rowcount` attribute "is -1 in |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 556 | case no ``executeXX()`` has been performed on the cursor or the rowcount of the |
| 557 | last operation is not determinable by the interface". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 559 | This includes ``SELECT`` statements because we cannot determine the number of |
| 560 | rows a query produced until all rows were fetched. |
| 561 | |
Gerhard Häring | d337279 | 2008-03-29 19:13:55 +0000 | [diff] [blame] | 562 | .. attribute:: Cursor.lastrowid |
| 563 | |
| 564 | This read-only attribute provides the rowid of the last modified row. It is |
| 565 | only set if you issued a ``INSERT`` statement using the :meth:`execute` |
| 566 | method. For operations other than ``INSERT`` or when :meth:`executemany` is |
| 567 | called, :attr:`lastrowid` is set to :const:`None`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 568 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 569 | .. attribute:: Cursor.description |
| 570 | |
| 571 | This read-only attribute provides the column names of the last query. To |
| 572 | remain compatible with the Python DB API, it returns a 7-tuple for each |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 573 | column where the last six items of each tuple are :const:`None`. |
| 574 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 575 | It is set for ``SELECT`` statements without any matching rows as well. |
| 576 | |
| 577 | .. _sqlite3-row-objects: |
| 578 | |
| 579 | Row Objects |
| 580 | ----------- |
| 581 | |
| 582 | .. class:: Row |
| 583 | |
| 584 | A :class:`Row` instance serves as a highly optimized |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 585 | :attr:`~Connection.row_factory` for :class:`Connection` objects. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 586 | It tries to mimic a tuple in most of its features. |
| 587 | |
| 588 | It supports mapping access by column name and index, iteration, |
| 589 | representation, equality testing and :func:`len`. |
| 590 | |
| 591 | If two :class:`Row` objects have exactly the same columns and their |
| 592 | members are equal, they compare equal. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 593 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 594 | .. method:: keys |
| 595 | |
| 596 | This method returns a tuple of column names. Immediately after a query, |
| 597 | it is the first member of each tuple in :attr:`Cursor.description`. |
| 598 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 599 | Let's assume we initialize a table as in the example given above:: |
| 600 | |
| 601 | conn = sqlite3.connect(":memory:") |
| 602 | c = conn.cursor() |
| 603 | c.execute('''create table stocks |
| 604 | (date text, trans text, symbol text, |
| 605 | qty real, price real)''') |
| 606 | c.execute("""insert into stocks |
| 607 | values ('2006-01-05','BUY','RHAT',100,35.14)""") |
| 608 | conn.commit() |
| 609 | c.close() |
| 610 | |
| 611 | Now we plug :class:`Row` in:: |
| 612 | |
| 613 | >>> conn.row_factory = sqlite3.Row |
| 614 | >>> c = conn.cursor() |
| 615 | >>> c.execute('select * from stocks') |
| 616 | <sqlite3.Cursor object at 0x7f4e7dd8fa80> |
| 617 | >>> r = c.fetchone() |
| 618 | >>> type(r) |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 619 | <class 'sqlite3.Row'> |
| 620 | >>> tuple(r) |
| 621 | ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 622 | >>> len(r) |
| 623 | 5 |
| 624 | >>> r[2] |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 625 | 'RHAT' |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 626 | >>> r.keys() |
| 627 | ['date', 'trans', 'symbol', 'qty', 'price'] |
| 628 | >>> r['qty'] |
| 629 | 100.0 |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 630 | >>> for member in r: |
| 631 | ... print(member) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 632 | ... |
| 633 | 2006-01-05 |
| 634 | BUY |
| 635 | RHAT |
| 636 | 100.0 |
| 637 | 35.14 |
| 638 | |
| 639 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 640 | .. _sqlite3-types: |
| 641 | |
| 642 | SQLite and Python types |
| 643 | ----------------------- |
| 644 | |
| 645 | |
| 646 | Introduction |
| 647 | ^^^^^^^^^^^^ |
| 648 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 649 | SQLite natively supports the following types: ``NULL``, ``INTEGER``, |
| 650 | ``REAL``, ``TEXT``, ``BLOB``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 651 | |
| 652 | The following Python types can thus be sent to SQLite without any problem: |
| 653 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 654 | +-------------------------------+-------------+ |
| 655 | | Python type | SQLite type | |
| 656 | +===============================+=============+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 657 | | :const:`None` | ``NULL`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 658 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 659 | | :class:`int` | ``INTEGER`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 660 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 661 | | :class:`float` | ``REAL`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 662 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 663 | | :class:`str` | ``TEXT`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 664 | +-------------------------------+-------------+ |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 665 | | :class:`bytes` | ``BLOB`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 666 | +-------------------------------+-------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 667 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 668 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 669 | This is how SQLite types are converted to Python types by default: |
| 670 | |
| 671 | +-------------+---------------------------------------------+ |
| 672 | | SQLite type | Python type | |
| 673 | +=============+=============================================+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 674 | | ``NULL`` | :const:`None` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | +-------------+---------------------------------------------+ |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 676 | | ``INTEGER`` | :class:`int` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 677 | +-------------+---------------------------------------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 678 | | ``REAL`` | :class:`float` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 679 | +-------------+---------------------------------------------+ |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 680 | | ``TEXT`` | depends on text_factory, str by default | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 681 | +-------------+---------------------------------------------+ |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 682 | | ``BLOB`` | :class:`bytes` | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 | +-------------+---------------------------------------------+ |
| 684 | |
| 685 | The type system of the :mod:`sqlite3` module is extensible in two ways: you can |
| 686 | store additional Python types in a SQLite database via object adaptation, and |
| 687 | you can let the :mod:`sqlite3` module convert SQLite types to different Python |
| 688 | types via converters. |
| 689 | |
| 690 | |
| 691 | Using adapters to store additional Python types in SQLite databases |
| 692 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 693 | |
| 694 | As described before, SQLite supports only a limited set of types natively. To |
| 695 | use other Python types with SQLite, you must **adapt** them to one of the |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 696 | sqlite3 module's supported types for SQLite: one of NoneType, int, float, |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 697 | str, bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 698 | |
| 699 | The :mod:`sqlite3` module uses Python object adaptation, as described in |
| 700 | :pep:`246` for this. The protocol to use is :class:`PrepareProtocol`. |
| 701 | |
| 702 | There are two ways to enable the :mod:`sqlite3` module to adapt a custom Python |
| 703 | type to one of the supported ones. |
| 704 | |
| 705 | |
| 706 | Letting your object adapt itself |
| 707 | """""""""""""""""""""""""""""""" |
| 708 | |
| 709 | This is a good approach if you write the class yourself. Let's suppose you have |
| 710 | a class like this:: |
| 711 | |
| 712 | class Point(object): |
| 713 | def __init__(self, x, y): |
| 714 | self.x, self.y = x, y |
| 715 | |
| 716 | Now you want to store the point in a single SQLite column. First you'll have to |
| 717 | choose one of the supported types first to be used for representing the point. |
| 718 | Let's just use str and separate the coordinates using a semicolon. Then you need |
| 719 | to give your class a method ``__conform__(self, protocol)`` which must return |
| 720 | the converted value. The parameter *protocol* will be :class:`PrepareProtocol`. |
| 721 | |
| 722 | .. literalinclude:: ../includes/sqlite3/adapter_point_1.py |
| 723 | |
| 724 | |
| 725 | Registering an adapter callable |
| 726 | """"""""""""""""""""""""""""""" |
| 727 | |
| 728 | The other possibility is to create a function that converts the type to the |
| 729 | string representation and register the function with :meth:`register_adapter`. |
| 730 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 731 | .. literalinclude:: ../includes/sqlite3/adapter_point_2.py |
| 732 | |
| 733 | The :mod:`sqlite3` module has two default adapters for Python's built-in |
| 734 | :class:`datetime.date` and :class:`datetime.datetime` types. Now let's suppose |
| 735 | we want to store :class:`datetime.datetime` objects not in ISO representation, |
| 736 | but as a Unix timestamp. |
| 737 | |
| 738 | .. literalinclude:: ../includes/sqlite3/adapter_datetime.py |
| 739 | |
| 740 | |
| 741 | Converting SQLite values to custom Python types |
| 742 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 743 | |
| 744 | Writing an adapter lets you send custom Python types to SQLite. But to make it |
| 745 | really useful we need to make the Python to SQLite to Python roundtrip work. |
| 746 | |
| 747 | Enter converters. |
| 748 | |
| 749 | Let's go back to the :class:`Point` class. We stored the x and y coordinates |
| 750 | separated via semicolons as strings in SQLite. |
| 751 | |
| 752 | First, we'll define a converter function that accepts the string as a parameter |
| 753 | and constructs a :class:`Point` object from it. |
| 754 | |
| 755 | .. note:: |
| 756 | |
| 757 | Converter functions **always** get called with a string, no matter under which |
| 758 | data type you sent the value to SQLite. |
| 759 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 760 | :: |
| 761 | |
| 762 | def convert_point(s): |
| 763 | x, y = map(float, s.split(";")) |
| 764 | return Point(x, y) |
| 765 | |
| 766 | Now you need to make the :mod:`sqlite3` module know that what you select from |
| 767 | the database is actually a point. There are two ways of doing this: |
| 768 | |
| 769 | * Implicitly via the declared type |
| 770 | |
| 771 | * Explicitly via the column name |
| 772 | |
| 773 | Both ways are described in section :ref:`sqlite3-module-contents`, in the entries |
| 774 | for the constants :const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES`. |
| 775 | |
| 776 | The following example illustrates both approaches. |
| 777 | |
| 778 | .. literalinclude:: ../includes/sqlite3/converter_point.py |
| 779 | |
| 780 | |
| 781 | Default adapters and converters |
| 782 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 783 | |
| 784 | There are default adapters for the date and datetime types in the datetime |
| 785 | module. They will be sent as ISO dates/ISO timestamps to SQLite. |
| 786 | |
| 787 | The default converters are registered under the name "date" for |
| 788 | :class:`datetime.date` and under the name "timestamp" for |
| 789 | :class:`datetime.datetime`. |
| 790 | |
| 791 | This way, you can use date/timestamps from Python without any additional |
| 792 | fiddling in most cases. The format of the adapters is also compatible with the |
| 793 | experimental SQLite date/time functions. |
| 794 | |
| 795 | The following example demonstrates this. |
| 796 | |
| 797 | .. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py |
| 798 | |
| 799 | |
| 800 | .. _sqlite3-controlling-transactions: |
| 801 | |
| 802 | Controlling Transactions |
| 803 | ------------------------ |
| 804 | |
| 805 | By default, the :mod:`sqlite3` module opens transactions implicitly before a |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 806 | Data Modification Language (DML) statement (i.e. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 807 | ``INSERT``/``UPDATE``/``DELETE``/``REPLACE``), and commits transactions |
| 808 | implicitly before a non-DML, non-query statement (i. e. |
| 809 | anything other than ``SELECT`` or the aforementioned). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 810 | |
| 811 | So if you are within a transaction and issue a command like ``CREATE TABLE |
| 812 | ...``, ``VACUUM``, ``PRAGMA``, the :mod:`sqlite3` module will commit implicitly |
| 813 | before executing that command. There are two reasons for doing that. The first |
| 814 | is that some of these commands don't work within transactions. The other reason |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 815 | is that sqlite3 needs to keep track of the transaction state (if a transaction |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 816 | is active or not). The current transaction state is exposed through the |
| 817 | :attr:`Connection.in_transaction` attribute of the connection object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 818 | |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 819 | You can control which kind of ``BEGIN`` statements sqlite3 implicitly executes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 820 | (or none at all) via the *isolation_level* parameter to the :func:`connect` |
| 821 | call, or via the :attr:`isolation_level` property of connections. |
| 822 | |
| 823 | If you want **autocommit mode**, then set :attr:`isolation_level` to None. |
| 824 | |
| 825 | Otherwise leave it at its default, which will result in a plain "BEGIN" |
Georg Brandl | a971c65 | 2008-11-07 09:39:56 +0000 | [diff] [blame] | 826 | statement, or set it to one of SQLite's supported isolation levels: "DEFERRED", |
| 827 | "IMMEDIATE" or "EXCLUSIVE". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 828 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 829 | |
| 830 | |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 831 | Using :mod:`sqlite3` efficiently |
| 832 | -------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 833 | |
| 834 | |
| 835 | Using shortcut methods |
| 836 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 837 | |
| 838 | Using the nonstandard :meth:`execute`, :meth:`executemany` and |
| 839 | :meth:`executescript` methods of the :class:`Connection` object, your code can |
| 840 | be written more concisely because you don't have to create the (often |
| 841 | superfluous) :class:`Cursor` objects explicitly. Instead, the :class:`Cursor` |
| 842 | objects are created implicitly and these shortcut methods return the cursor |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 843 | objects. This way, you can execute a ``SELECT`` statement and iterate over it |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 844 | directly using only a single call on the :class:`Connection` object. |
| 845 | |
| 846 | .. literalinclude:: ../includes/sqlite3/shortcut_methods.py |
| 847 | |
| 848 | |
| 849 | Accessing columns by name instead of by index |
| 850 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 851 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 852 | One useful feature of the :mod:`sqlite3` module is the built-in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 853 | :class:`sqlite3.Row` class designed to be used as a row factory. |
| 854 | |
| 855 | Rows wrapped with this class can be accessed both by index (like tuples) and |
| 856 | case-insensitively by name: |
| 857 | |
| 858 | .. literalinclude:: ../includes/sqlite3/rowclass.py |
| 859 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 860 | |
| 861 | Using the connection as a context manager |
| 862 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 863 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 864 | Connection objects can be used as context managers |
| 865 | that automatically commit or rollback transactions. In the event of an |
| 866 | exception, the transaction is rolled back; otherwise, the transaction is |
| 867 | committed: |
| 868 | |
| 869 | .. literalinclude:: ../includes/sqlite3/ctx_manager.py |