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. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Petri Lehtinen | 4d2bfb5 | 2012-03-01 21:18:34 +0200 | [diff] [blame] | 7 | .. sectionauthor:: Gerhard Häring <gh@ghaering.de> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 9 | **Source code:** :source:`Lib/sqlite3/` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | SQLite is a C library that provides a lightweight disk-based database that |
| 14 | doesn't require a separate server process and allows accessing the database |
| 15 | using a nonstandard variant of the SQL query language. Some applications can use |
| 16 | SQLite for internal data storage. It's also possible to prototype an |
| 17 | application using SQLite and then port the code to a larger database such as |
| 18 | PostgreSQL or Oracle. |
| 19 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 20 | The sqlite3 module was written by Gerhard Häring. It provides a SQL interface |
| 21 | compliant with the DB-API 2.0 specification described by :pep:`249`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
| 23 | To use the module, you must first create a :class:`Connection` object that |
| 24 | represents the database. Here the data will be stored in the |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 25 | :file:`example.db` file:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Petri Lehtinen | 4d2bfb5 | 2012-03-01 21:18:34 +0200 | [diff] [blame] | 27 | import sqlite3 |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 28 | conn = sqlite3.connect('example.db') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
| 30 | You can also supply the special name ``:memory:`` to create a database in RAM. |
| 31 | |
| 32 | 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] | 33 | and call its :meth:`~Cursor.execute` method to perform SQL commands:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 35 | c = conn.cursor() |
| 36 | |
| 37 | # Create table |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 38 | c.execute('''CREATE TABLE stocks |
| 39 | (date text, trans text, symbol text, qty real, price real)''') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
| 41 | # Insert a row of data |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 42 | c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 44 | # Save (commit) the changes |
| 45 | conn.commit() |
| 46 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 47 | # We can also close the connection if we are done with it. |
| 48 | # Just be sure any changes have been committed or they will be lost. |
| 49 | conn.close() |
| 50 | |
| 51 | The data you've saved is persistent and is available in subsequent sessions:: |
| 52 | |
| 53 | import sqlite3 |
| 54 | conn = sqlite3.connect('example.db') |
| 55 | c = conn.cursor() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 57 | Usually your SQL operations will need to use values from Python variables. You |
| 58 | shouldn't assemble your query using Python's string operations because doing so |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 59 | is insecure; it makes your program vulnerable to an SQL injection attack |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 60 | (see https://xkcd.com/327/ for humorous example of what can go wrong). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
| 62 | Instead, use the DB-API's parameter substitution. Put ``?`` as a placeholder |
| 63 | 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] | 64 | second argument to the cursor's :meth:`~Cursor.execute` method. (Other database |
| 65 | modules may use a different placeholder, such as ``%s`` or ``:1``.) For |
| 66 | example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | # Never do this -- insecure! |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 69 | symbol = 'RHAT' |
| 70 | c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | # Do this instead |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 73 | t = ('RHAT',) |
| 74 | c.execute('SELECT * FROM stocks WHERE symbol=?', t) |
| 75 | print(c.fetchone()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 77 | # Larger example that inserts many records at a time |
| 78 | purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), |
| 79 | ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), |
| 80 | ('2006-04-06', 'SELL', 'IBM', 500, 53.00), |
| 81 | ] |
| 82 | c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 84 | 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] | 85 | cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to |
| 86 | 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] | 87 | matching rows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
| 89 | This example uses the iterator form:: |
| 90 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 91 | >>> for row in c.execute('SELECT * FROM stocks ORDER BY price'): |
| 92 | print(row) |
| 93 | |
Ezio Melotti | b584505 | 2009-09-13 05:49:25 +0000 | [diff] [blame] | 94 | ('2006-01-05', 'BUY', 'RHAT', 100, 35.14) |
| 95 | ('2006-03-28', 'BUY', 'IBM', 1000, 45.0) |
| 96 | ('2006-04-06', 'SELL', 'IBM', 500, 53.0) |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 97 | ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | .. seealso:: |
| 101 | |
Benjamin Peterson | 216e47d | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 102 | https://github.com/ghaering/pysqlite |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 103 | The pysqlite web page -- sqlite3 is developed externally under the name |
| 104 | "pysqlite". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 106 | https://www.sqlite.org |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 107 | The SQLite web page; the documentation describes the syntax and the |
| 108 | available data types for the supported SQL dialect. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
Sanyam Khurana | 1b4587a | 2017-12-06 22:09:33 +0530 | [diff] [blame] | 110 | https://www.w3schools.com/sql/ |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 111 | Tutorial, reference and examples for learning SQL syntax. |
| 112 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | :pep:`249` - Database API Specification 2.0 |
| 114 | PEP written by Marc-André Lemburg. |
| 115 | |
| 116 | |
| 117 | .. _sqlite3-module-contents: |
| 118 | |
| 119 | Module functions and constants |
| 120 | ------------------------------ |
| 121 | |
| 122 | |
R David Murray | 3f7beb9 | 2013-01-10 20:18:21 -0500 | [diff] [blame] | 123 | .. data:: version |
| 124 | |
| 125 | The version number of this module, as a string. This is not the version of |
| 126 | the SQLite library. |
| 127 | |
| 128 | |
| 129 | .. data:: version_info |
| 130 | |
| 131 | The version number of this module, as a tuple of integers. This is not the |
| 132 | version of the SQLite library. |
| 133 | |
| 134 | |
| 135 | .. data:: sqlite_version |
| 136 | |
| 137 | The version number of the run-time SQLite library, as a string. |
| 138 | |
| 139 | |
| 140 | .. data:: sqlite_version_info |
| 141 | |
| 142 | The version number of the run-time SQLite library, as a tuple of integers. |
| 143 | |
| 144 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | .. data:: PARSE_DECLTYPES |
| 146 | |
| 147 | This constant is meant to be used with the *detect_types* parameter of the |
| 148 | :func:`connect` function. |
| 149 | |
| 150 | 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] | 151 | column it returns. It will parse out the first word of the declared type, |
| 152 | i. e. for "integer primary key", it will parse out "integer", or for |
| 153 | "number(10)" it will parse out "number". Then for that column, it will look |
| 154 | into the converters dictionary and use the converter function registered for |
| 155 | that type there. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | .. data:: PARSE_COLNAMES |
| 159 | |
| 160 | This constant is meant to be used with the *detect_types* parameter of the |
| 161 | :func:`connect` function. |
| 162 | |
| 163 | Setting this makes the SQLite interface parse the column name for each column it |
| 164 | returns. It will look for a string formed [mytype] in there, and then decide |
| 165 | that 'mytype' is the type of the column. It will try to find an entry of |
| 166 | 'mytype' in the converters dictionary and then use the converter function found |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 167 | 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] | 168 | is only the first word of the column name, i. e. if you use something like |
| 169 | ``'as "x [datetime]"'`` in your SQL, then we will parse out everything until the |
| 170 | first blank for the column name: the column name would simply be "x". |
| 171 | |
| 172 | |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 173 | .. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 175 | Opens a connection to the SQLite database file *database*. By default returns a |
| 176 | :class:`Connection` object, unless a custom *factory* is given. |
| 177 | |
| 178 | *database* is a :term:`path-like object` giving the pathname (absolute or |
| 179 | relative to the current working directory) of the database file to be opened. |
| 180 | You can use ``":memory:"`` to open a database connection to a database that |
| 181 | resides in RAM instead of on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | When a database is accessed by multiple connections, and one of the processes |
| 184 | modifies the database, the SQLite database is locked until that transaction is |
| 185 | committed. The *timeout* parameter specifies how long the connection should wait |
| 186 | for the lock to go away until raising an exception. The default for the timeout |
| 187 | parameter is 5.0 (five seconds). |
| 188 | |
| 189 | For the *isolation_level* parameter, please see the |
Berker Peksag | a1bc246 | 2016-09-07 04:02:41 +0300 | [diff] [blame] | 190 | :attr:`~Connection.isolation_level` property of :class:`Connection` objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | |
Georg Brandl | 3c12711 | 2013-10-06 12:38:44 +0200 | [diff] [blame] | 192 | SQLite natively supports only the types TEXT, INTEGER, REAL, BLOB and NULL. If |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | you want to use other types you must add support for them yourself. The |
| 194 | *detect_types* parameter and the using custom **converters** registered with the |
| 195 | module-level :func:`register_converter` function allow you to easily do that. |
| 196 | |
| 197 | *detect_types* defaults to 0 (i. e. off, no type detection), you can set it to |
| 198 | any combination of :const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES` to turn |
| 199 | type detection on. |
| 200 | |
Senthil Kumaran | 7ee9194 | 2016-06-03 00:03:48 -0700 | [diff] [blame] | 201 | By default, *check_same_thread* is :const:`True` and only the creating thread may |
| 202 | use the connection. If set :const:`False`, the returned connection may be shared |
| 203 | across multiple threads. When using multiple threads with the same connection |
| 204 | writing operations should be serialized by the user to avoid data corruption. |
| 205 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | By default, the :mod:`sqlite3` module uses its :class:`Connection` class for the |
| 207 | connect call. You can, however, subclass the :class:`Connection` class and make |
| 208 | :func:`connect` use your class instead by providing your class for the *factory* |
| 209 | parameter. |
| 210 | |
| 211 | Consult the section :ref:`sqlite3-types` of this manual for details. |
| 212 | |
| 213 | The :mod:`sqlite3` module internally uses a statement cache to avoid SQL parsing |
| 214 | overhead. If you want to explicitly set the number of statements that are cached |
| 215 | for the connection, you can set the *cached_statements* parameter. The currently |
| 216 | implemented default is to cache 100 statements. |
| 217 | |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 218 | If *uri* is true, *database* is interpreted as a URI. This allows you |
| 219 | to specify options. For example, to open a database in read-only mode |
| 220 | you can use:: |
| 221 | |
| 222 | db = sqlite3.connect('file:path/to/database?mode=ro', uri=True) |
| 223 | |
| 224 | More information about this feature, including a list of recognized options, can |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 225 | be found in the `SQLite URI documentation <https://www.sqlite.org/uri.html>`_. |
Antoine Pitrou | 902fc8b | 2013-02-10 00:02:44 +0100 | [diff] [blame] | 226 | |
| 227 | .. versionchanged:: 3.4 |
| 228 | Added the *uri* parameter. |
| 229 | |
Anders Lorentsen | a22a127 | 2017-11-07 01:47:43 +0100 | [diff] [blame] | 230 | .. versionchanged:: 3.7 |
| 231 | *database* can now also be a :term:`path-like object`, not only a string. |
| 232 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | .. function:: register_converter(typename, callable) |
| 235 | |
| 236 | Registers a callable to convert a bytestring from the database into a custom |
| 237 | Python type. The callable will be invoked for all database values that are of |
| 238 | the type *typename*. Confer the parameter *detect_types* of the :func:`connect` |
| 239 | function for how the type detection works. Note that the case of *typename* and |
| 240 | the name of the type in your query must match! |
| 241 | |
| 242 | |
| 243 | .. function:: register_adapter(type, callable) |
| 244 | |
| 245 | Registers a callable to convert the custom Python type *type* into one of |
| 246 | SQLite's supported types. The callable *callable* accepts as single parameter |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 247 | 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] | 248 | float, str or bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | .. function:: complete_statement(sql) |
| 252 | |
| 253 | Returns :const:`True` if the string *sql* contains one or more complete SQL |
| 254 | statements terminated by semicolons. It does not verify that the SQL is |
| 255 | syntactically correct, only that there are no unclosed string literals and the |
| 256 | statement is terminated by a semicolon. |
| 257 | |
| 258 | This can be used to build a shell for SQLite, as in the following example: |
| 259 | |
| 260 | |
| 261 | .. literalinclude:: ../includes/sqlite3/complete_statement.py |
| 262 | |
| 263 | |
| 264 | .. function:: enable_callback_tracebacks(flag) |
| 265 | |
| 266 | By default you will not get any tracebacks in user-defined functions, |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 267 | aggregates, converters, authorizer callbacks etc. If you want to debug them, |
| 268 | you can call this function with *flag* set to ``True``. Afterwards, you will |
| 269 | get tracebacks from callbacks on ``sys.stderr``. Use :const:`False` to |
| 270 | disable the feature again. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | .. _sqlite3-connection-objects: |
| 274 | |
| 275 | Connection Objects |
| 276 | ------------------ |
| 277 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 278 | .. class:: Connection |
| 279 | |
| 280 | A SQLite database connection has the following attributes and methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 281 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 282 | .. attribute:: isolation_level |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 283 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 284 | Get or set the current isolation level. :const:`None` for autocommit mode or |
| 285 | one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section |
| 286 | :ref:`sqlite3-controlling-transactions` for a more detailed explanation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 287 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 288 | .. attribute:: in_transaction |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 289 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 290 | :const:`True` if a transaction is active (there are uncommitted changes), |
| 291 | :const:`False` otherwise. Read-only attribute. |
R. David Murray | d35251d | 2010-06-01 01:32:12 +0000 | [diff] [blame] | 292 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 293 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 294 | |
Serhiy Storchaka | ef113cd | 2016-08-29 14:29:55 +0300 | [diff] [blame] | 295 | .. method:: cursor(factory=Cursor) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
Serhiy Storchaka | ef113cd | 2016-08-29 14:29:55 +0300 | [diff] [blame] | 297 | The cursor method accepts a single optional parameter *factory*. If |
| 298 | supplied, this must be a callable returning an instance of :class:`Cursor` |
| 299 | or its subclasses. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 301 | .. method:: commit() |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 302 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 303 | This method commits the current transaction. If you don't call this method, |
| 304 | anything you did since the last call to ``commit()`` is not visible from |
| 305 | other database connections. If you wonder why you don't see the data you've |
| 306 | written to the database, please check you didn't forget to call this method. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 307 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 308 | .. method:: rollback() |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 309 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 310 | This method rolls back any changes to the database since the last call to |
| 311 | :meth:`commit`. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 312 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 313 | .. method:: close() |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 314 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 315 | This closes the database connection. Note that this does not automatically |
| 316 | call :meth:`commit`. If you just close your database connection without |
| 317 | calling :meth:`commit` first, your changes will be lost! |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 318 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 319 | .. method:: execute(sql[, parameters]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 321 | This is a nonstandard shortcut that creates a cursor object by calling |
| 322 | the :meth:`~Connection.cursor` method, calls the cursor's |
| 323 | :meth:`~Cursor.execute` method with the *parameters* given, and returns |
| 324 | the cursor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 326 | .. method:: executemany(sql[, parameters]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 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.executemany` method with the *parameters* given, and |
| 331 | returns the cursor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 333 | .. method:: executescript(sql_script) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 335 | This is a nonstandard shortcut that creates a cursor object by |
| 336 | calling the :meth:`~Connection.cursor` method, calls the cursor's |
| 337 | :meth:`~Cursor.executescript` method with the given *sql_script*, and |
| 338 | returns the cursor. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 340 | .. method:: create_function(name, num_params, func) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 341 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 342 | Creates a user-defined function that you can later use from within SQL |
| 343 | statements under the function name *name*. *num_params* is the number of |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 344 | parameters the function accepts (if *num_params* is -1, the function may |
| 345 | take any number of arguments), and *func* is a Python callable that is |
| 346 | called as the SQL function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 347 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 348 | The function can return any of the types supported by SQLite: bytes, str, int, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 349 | float and ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 350 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 351 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 353 | .. literalinclude:: ../includes/sqlite3/md5func.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 354 | |
| 355 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 356 | .. method:: create_aggregate(name, num_params, aggregate_class) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 358 | Creates a user-defined aggregate function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 360 | The aggregate class must implement a ``step`` method, which accepts the number |
Berker Peksag | fa0f62d | 2016-03-27 22:39:14 +0300 | [diff] [blame] | 361 | of parameters *num_params* (if *num_params* is -1, the function may take |
| 362 | any number of arguments), and a ``finalize`` method which will return the |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 363 | final result of the aggregate. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 365 | The ``finalize`` method can return any of the types supported by SQLite: |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 366 | bytes, str, int, float and ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 368 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 370 | .. literalinclude:: ../includes/sqlite3/mysumaggr.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 371 | |
| 372 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 373 | .. method:: create_collation(name, callable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 374 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 375 | Creates a collation with the specified *name* and *callable*. The callable will |
| 376 | be passed two string arguments. It should return -1 if the first is ordered |
| 377 | lower than the second, 0 if they are ordered equal and 1 if the first is ordered |
| 378 | higher than the second. Note that this controls sorting (ORDER BY in SQL) so |
| 379 | your comparisons don't affect other SQL operations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 380 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 381 | Note that the callable will get its parameters as Python bytestrings, which will |
| 382 | normally be encoded in UTF-8. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 383 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 384 | The following example shows a custom collation that sorts "the wrong way": |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 386 | .. literalinclude:: ../includes/sqlite3/collation_reverse.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 388 | To remove a collation, call ``create_collation`` with ``None`` as callable:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 390 | con.create_collation("reverse", None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | |
| 392 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 393 | .. method:: interrupt() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 394 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 395 | You can call this method from a different thread to abort any queries that might |
| 396 | be executing on the connection. The query will then abort and the caller will |
| 397 | get an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
| 399 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 400 | .. method:: set_authorizer(authorizer_callback) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 402 | This routine registers a callback. The callback is invoked for each attempt to |
| 403 | access a column of a table in the database. The callback should return |
| 404 | :const:`SQLITE_OK` if access is allowed, :const:`SQLITE_DENY` if the entire SQL |
| 405 | statement should be aborted with an error and :const:`SQLITE_IGNORE` if the |
| 406 | column should be treated as a NULL value. These constants are available in the |
| 407 | :mod:`sqlite3` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 409 | The first argument to the callback signifies what kind of operation is to be |
| 410 | authorized. The second and third argument will be arguments or :const:`None` |
| 411 | depending on the first argument. The 4th argument is the name of the database |
| 412 | ("main", "temp", etc.) if applicable. The 5th argument is the name of the |
| 413 | inner-most trigger or view that is responsible for the access attempt or |
| 414 | :const:`None` if this access attempt is directly from input SQL code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 415 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 416 | Please consult the SQLite documentation about the possible values for the first |
| 417 | argument and the meaning of the second and third argument depending on the first |
| 418 | one. All necessary constants are available in the :mod:`sqlite3` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 420 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 421 | .. method:: set_progress_handler(handler, n) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 423 | This routine registers a callback. The callback is invoked for every *n* |
| 424 | instructions of the SQLite virtual machine. This is useful if you want to |
| 425 | get called from SQLite during long-running operations, for example to update |
| 426 | a GUI. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 427 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 428 | If you want to clear any previously installed progress handler, call the |
| 429 | method with :const:`None` for *handler*. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 430 | |
Simon Willison | ac03c03 | 2017-11-02 07:34:12 -0700 | [diff] [blame] | 431 | Returning a non-zero value from the handler function will terminate the |
| 432 | currently executing query and cause it to raise an :exc:`OperationalError` |
| 433 | exception. |
| 434 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 435 | |
R David Murray | 842ca5f | 2012-09-30 20:49:19 -0400 | [diff] [blame] | 436 | .. method:: set_trace_callback(trace_callback) |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 437 | |
R David Murray | 842ca5f | 2012-09-30 20:49:19 -0400 | [diff] [blame] | 438 | Registers *trace_callback* to be called for each SQL statement that is |
| 439 | actually executed by the SQLite backend. |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 440 | |
R David Murray | 842ca5f | 2012-09-30 20:49:19 -0400 | [diff] [blame] | 441 | The only argument passed to the callback is the statement (as string) that |
| 442 | is being executed. The return value of the callback is ignored. Note that |
| 443 | the backend does not only run statements passed to the :meth:`Cursor.execute` |
| 444 | methods. Other sources include the transaction management of the Python |
| 445 | module and the execution of triggers defined in the current database. |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 446 | |
R David Murray | 842ca5f | 2012-09-30 20:49:19 -0400 | [diff] [blame] | 447 | Passing :const:`None` as *trace_callback* will disable the trace callback. |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 448 | |
R David Murray | 842ca5f | 2012-09-30 20:49:19 -0400 | [diff] [blame] | 449 | .. versionadded:: 3.3 |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 450 | |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 451 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 452 | .. method:: enable_load_extension(enabled) |
Antoine Pitrou | 5bfa062 | 2011-04-04 00:12:04 +0200 | [diff] [blame] | 453 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 454 | This routine allows/disallows the SQLite engine to load SQLite extensions |
| 455 | from shared libraries. SQLite extensions can define new functions, |
| 456 | aggregates or whole new virtual table implementations. One well-known |
| 457 | extension is the fulltext-search extension distributed with SQLite. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 458 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 459 | Loadable extensions are disabled by default. See [#f1]_. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 460 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 461 | .. versionadded:: 3.2 |
Petri Lehtinen | 4d2bfb5 | 2012-03-01 21:18:34 +0200 | [diff] [blame] | 462 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 463 | .. literalinclude:: ../includes/sqlite3/load_extension.py |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 464 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 465 | .. method:: load_extension(path) |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 466 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 467 | This routine loads a SQLite extension from a shared library. You have to |
| 468 | enable extension loading with :meth:`enable_load_extension` before you can |
| 469 | use this routine. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 470 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 471 | Loadable extensions are disabled by default. See [#f1]_. |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 472 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 473 | .. versionadded:: 3.2 |
Gerhard Häring | e0941c5 | 2010-10-03 21:47:06 +0000 | [diff] [blame] | 474 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 475 | .. attribute:: row_factory |
Petri Lehtinen | 4d2bfb5 | 2012-03-01 21:18:34 +0200 | [diff] [blame] | 476 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 477 | You can change this attribute to a callable that accepts the cursor and the |
| 478 | original row as a tuple and will return the real result row. This way, you can |
| 479 | implement more advanced ways of returning results, such as returning an object |
| 480 | that can also access columns by name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 482 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 483 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 484 | .. literalinclude:: ../includes/sqlite3/row_factory.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 486 | If returning a tuple doesn't suffice and you want name-based access to |
| 487 | columns, you should consider setting :attr:`row_factory` to the |
| 488 | highly-optimized :class:`sqlite3.Row` type. :class:`Row` provides both |
| 489 | index-based and case-insensitive name-based access to columns with almost no |
| 490 | memory overhead. It will probably be better than your own custom |
| 491 | dictionary-based approach or even a db_row based solution. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 493 | .. XXX what's a db_row-based solution? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 496 | .. attribute:: text_factory |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 497 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 498 | Using this attribute you can control what objects are returned for the ``TEXT`` |
| 499 | data type. By default, this attribute is set to :class:`str` and the |
| 500 | :mod:`sqlite3` module will return Unicode objects for ``TEXT``. If you want to |
| 501 | return bytestrings instead, you can set it to :class:`bytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 502 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 503 | You can also set it to any other callable that accepts a single bytestring |
| 504 | parameter and returns the resulting object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 505 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 506 | See the following example code for illustration: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 508 | .. literalinclude:: ../includes/sqlite3/text_factory.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | |
| 510 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 511 | .. attribute:: total_changes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 512 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 513 | Returns the total number of database rows that have been modified, inserted, or |
| 514 | deleted since the database connection was opened. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 515 | |
| 516 | |
Berker Peksag | 557a063 | 2016-03-27 18:46:18 +0300 | [diff] [blame] | 517 | .. method:: iterdump |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 518 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 519 | Returns an iterator to dump the database in an SQL text format. Useful when |
| 520 | saving an in-memory database for later restoration. This function provides |
| 521 | the same capabilities as the :kbd:`.dump` command in the :program:`sqlite3` |
| 522 | shell. |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 523 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 524 | Example:: |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 525 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 526 | # Convert file existing_db.db to SQL dump file dump.sql |
Berker Peksag | 557a063 | 2016-03-27 18:46:18 +0300 | [diff] [blame] | 527 | import sqlite3 |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 528 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 529 | con = sqlite3.connect('existing_db.db') |
| 530 | with open('dump.sql', 'w') as f: |
| 531 | for line in con.iterdump(): |
| 532 | f.write('%s\n' % line) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 533 | |
| 534 | |
Emanuele Gaifas | d7aed41 | 2018-03-10 23:08:31 +0100 | [diff] [blame] | 535 | .. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250) |
| 536 | |
| 537 | This method makes a backup of a SQLite database even while it's being accessed |
| 538 | by other clients, or concurrently by the same connection. The copy will be |
| 539 | written into the mandatory argument *target*, that must be another |
| 540 | :class:`Connection` instance. |
| 541 | |
| 542 | By default, or when *pages* is either ``0`` or a negative integer, the entire |
| 543 | database is copied in a single step; otherwise the method performs a loop |
| 544 | copying up to *pages* pages at a time. |
| 545 | |
| 546 | If *progress* is specified, it must either be ``None`` or a callable object that |
| 547 | will be executed at each iteration with three integer arguments, respectively |
| 548 | the *status* of the last iteration, the *remaining* number of pages still to be |
| 549 | copied and the *total* number of pages. |
| 550 | |
| 551 | The *name* argument specifies the database name that will be copied: it must be |
| 552 | a string containing either ``"main"``, the default, to indicate the main |
| 553 | database, ``"temp"`` to indicate the temporary database or the name specified |
| 554 | after the ``AS`` keyword in an ``ATTACH DATABASE`` statement for an attached |
| 555 | database. |
| 556 | |
| 557 | The *sleep* argument specifies the number of seconds to sleep by between |
| 558 | successive attempts to backup remaining pages, can be specified either as an |
| 559 | integer or a floating point value. |
| 560 | |
| 561 | Example 1, copy an existing database into another:: |
| 562 | |
| 563 | import sqlite3 |
| 564 | |
| 565 | def progress(status, remaining, total): |
| 566 | print(f'Copied {total-remaining} of {total} pages...') |
| 567 | |
| 568 | con = sqlite3.connect('existing_db.db') |
| 569 | with sqlite3.connect('backup.db') as bck: |
| 570 | con.backup(bck, pages=1, progress=progress) |
| 571 | |
| 572 | Example 2, copy an existing database into a transient copy:: |
| 573 | |
| 574 | import sqlite3 |
| 575 | |
| 576 | source = sqlite3.connect('existing_db.db') |
| 577 | dest = sqlite3.connect(':memory:') |
| 578 | source.backup(dest) |
| 579 | |
| 580 | Availability: SQLite 3.6.11 or higher |
| 581 | |
| 582 | .. versionadded:: 3.7 |
| 583 | |
| 584 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 585 | .. _sqlite3-cursor-objects: |
| 586 | |
| 587 | Cursor Objects |
| 588 | -------------- |
| 589 | |
Georg Brandl | 96115fb2 | 2010-10-17 09:33:24 +0000 | [diff] [blame] | 590 | .. class:: Cursor |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 | |
Georg Brandl | 96115fb2 | 2010-10-17 09:33:24 +0000 | [diff] [blame] | 592 | A :class:`Cursor` instance has the following attributes and methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 593 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 594 | .. method:: execute(sql[, parameters]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 595 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 596 | Executes an SQL statement. The SQL statement may be parameterized (i. e. |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 597 | placeholders instead of SQL literals). The :mod:`sqlite3` module supports two |
| 598 | kinds of placeholders: question marks (qmark style) and named placeholders |
| 599 | (named style). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 600 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 601 | Here's an example of both styles: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 602 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 603 | .. literalinclude:: ../includes/sqlite3/execute_1.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 604 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 605 | :meth:`execute` will only execute a single SQL statement. If you try to execute |
Berker Peksag | 7d92f89 | 2016-08-25 00:50:24 +0300 | [diff] [blame] | 606 | more than one statement with it, it will raise a :exc:`.Warning`. Use |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 607 | :meth:`executescript` if you want to execute multiple SQL statements with one |
| 608 | call. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 609 | |
| 610 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 611 | .. method:: executemany(sql, seq_of_parameters) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 612 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 613 | Executes an SQL command against all parameter sequences or mappings found in |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 614 | the sequence *seq_of_parameters*. The :mod:`sqlite3` module also allows |
| 615 | using an :term:`iterator` yielding parameters instead of a sequence. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 616 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 617 | .. literalinclude:: ../includes/sqlite3/executemany_1.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 618 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 619 | Here's a shorter example using a :term:`generator`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 620 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 621 | .. literalinclude:: ../includes/sqlite3/executemany_2.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 622 | |
| 623 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 624 | .. method:: executescript(sql_script) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 625 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 626 | This is a nonstandard convenience method for executing multiple SQL statements |
| 627 | at once. It issues a ``COMMIT`` statement first, then executes the SQL script it |
| 628 | gets as a parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 629 | |
Berker Peksag | c415440 | 2016-06-12 13:41:47 +0300 | [diff] [blame] | 630 | *sql_script* can be an instance of :class:`str`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 631 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 632 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 633 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 634 | .. literalinclude:: ../includes/sqlite3/executescript.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 635 | |
| 636 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 637 | .. method:: fetchone() |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 638 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 639 | Fetches the next row of a query result set, returning a single sequence, |
| 640 | or :const:`None` when no more data is available. |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 641 | |
| 642 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 643 | .. method:: fetchmany(size=cursor.arraysize) |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 644 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 645 | Fetches the next set of rows of a query result, returning a list. An empty |
| 646 | list is returned when no more rows are available. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 647 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 648 | The number of rows to fetch per call is specified by the *size* parameter. |
| 649 | If it is not given, the cursor's arraysize determines the number of rows |
| 650 | to be fetched. The method should try to fetch as many rows as indicated by |
| 651 | the size parameter. If this is not possible due to the specified number of |
| 652 | rows not being available, fewer rows may be returned. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 653 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 654 | Note there are performance considerations involved with the *size* parameter. |
| 655 | For optimal performance, it is usually best to use the arraysize attribute. |
| 656 | If the *size* parameter is used, then it is best for it to retain the same |
| 657 | value from one :meth:`fetchmany` call to the next. |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 658 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 659 | .. method:: fetchall() |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 660 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 661 | Fetches all (remaining) rows of a query result, returning a list. Note that |
| 662 | the cursor's arraysize attribute can affect the performance of this operation. |
| 663 | An empty list is returned when no rows are available. |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 664 | |
Berker Peksag | f70fe6f | 2016-03-27 21:51:02 +0300 | [diff] [blame] | 665 | .. method:: close() |
| 666 | |
| 667 | Close the cursor now (rather than whenever ``__del__`` is called). |
| 668 | |
Berker Peksag | ed789f9 | 2016-08-25 00:45:07 +0300 | [diff] [blame] | 669 | The cursor will be unusable from this point forward; a :exc:`ProgrammingError` |
Berker Peksag | f70fe6f | 2016-03-27 21:51:02 +0300 | [diff] [blame] | 670 | exception will be raised if any operation is attempted with the cursor. |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 671 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 672 | .. attribute:: rowcount |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 673 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 674 | Although the :class:`Cursor` class of the :mod:`sqlite3` module implements this |
| 675 | attribute, the database engine's own support for the determination of "rows |
| 676 | affected"/"rows selected" is quirky. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 677 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 678 | For :meth:`executemany` statements, the number of modifications are summed up |
| 679 | into :attr:`rowcount`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 680 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 681 | As required by the Python DB API Spec, the :attr:`rowcount` attribute "is -1 in |
| 682 | case no ``executeXX()`` has been performed on the cursor or the rowcount of the |
| 683 | last operation is not determinable by the interface". This includes ``SELECT`` |
| 684 | statements because we cannot determine the number of rows a query produced |
| 685 | until all rows were fetched. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 686 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 687 | With SQLite versions before 3.6.5, :attr:`rowcount` is set to 0 if |
| 688 | you make a ``DELETE FROM table`` without any condition. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 689 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 690 | .. attribute:: lastrowid |
Gerhard Häring | d337279 | 2008-03-29 19:13:55 +0000 | [diff] [blame] | 691 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 692 | This read-only attribute provides the rowid of the last modified row. It is |
Berker Peksag | e0b70cd | 2016-06-14 15:25:36 +0300 | [diff] [blame] | 693 | only set if you issued an ``INSERT`` or a ``REPLACE`` statement using the |
| 694 | :meth:`execute` method. For operations other than ``INSERT`` or |
| 695 | ``REPLACE`` or when :meth:`executemany` is called, :attr:`lastrowid` is |
| 696 | set to :const:`None`. |
| 697 | |
| 698 | If the ``INSERT`` or ``REPLACE`` statement failed to insert the previous |
| 699 | successful rowid is returned. |
| 700 | |
| 701 | .. versionchanged:: 3.6 |
| 702 | Added support for the ``REPLACE`` statement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 703 | |
csabella | 02e1213 | 2017-04-04 01:16:14 -0400 | [diff] [blame] | 704 | .. attribute:: arraysize |
| 705 | |
| 706 | Read/write attribute that controls the number of rows returned by :meth:`fetchmany`. |
| 707 | The default value is 1 which means a single row would be fetched per call. |
| 708 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 709 | .. attribute:: description |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 710 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 711 | This read-only attribute provides the column names of the last query. To |
| 712 | remain compatible with the Python DB API, it returns a 7-tuple for each |
| 713 | column where the last six items of each tuple are :const:`None`. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 714 | |
R David Murray | 6db2335 | 2012-09-30 20:44:43 -0400 | [diff] [blame] | 715 | It is set for ``SELECT`` statements without any matching rows as well. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 716 | |
Ezio Melotti | 62564db | 2016-03-18 20:10:36 +0200 | [diff] [blame] | 717 | .. attribute:: connection |
| 718 | |
| 719 | This read-only attribute provides the SQLite database :class:`Connection` |
| 720 | used by the :class:`Cursor` object. A :class:`Cursor` object created by |
| 721 | calling :meth:`con.cursor() <Connection.cursor>` will have a |
| 722 | :attr:`connection` attribute that refers to *con*:: |
| 723 | |
| 724 | >>> con = sqlite3.connect(":memory:") |
| 725 | >>> cur = con.cursor() |
| 726 | >>> cur.connection == con |
| 727 | True |
| 728 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 729 | .. _sqlite3-row-objects: |
| 730 | |
| 731 | Row Objects |
| 732 | ----------- |
| 733 | |
| 734 | .. class:: Row |
| 735 | |
| 736 | A :class:`Row` instance serves as a highly optimized |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 737 | :attr:`~Connection.row_factory` for :class:`Connection` objects. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 738 | It tries to mimic a tuple in most of its features. |
| 739 | |
| 740 | It supports mapping access by column name and index, iteration, |
| 741 | representation, equality testing and :func:`len`. |
| 742 | |
| 743 | If two :class:`Row` objects have exactly the same columns and their |
| 744 | members are equal, they compare equal. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 745 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 746 | .. method:: keys |
| 747 | |
R David Murray | 092135e | 2014-06-05 15:16:38 -0400 | [diff] [blame] | 748 | This method returns a list of column names. Immediately after a query, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 749 | it is the first member of each tuple in :attr:`Cursor.description`. |
| 750 | |
Serhiy Storchaka | 72e731c | 2015-03-31 13:33:11 +0300 | [diff] [blame] | 751 | .. versionchanged:: 3.5 |
| 752 | Added support of slicing. |
| 753 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 754 | Let's assume we initialize a table as in the example given above:: |
| 755 | |
Senthil Kumaran | 946eb86 | 2011-07-03 10:17:22 -0700 | [diff] [blame] | 756 | conn = sqlite3.connect(":memory:") |
| 757 | c = conn.cursor() |
| 758 | c.execute('''create table stocks |
| 759 | (date text, trans text, symbol text, |
| 760 | qty real, price real)''') |
| 761 | c.execute("""insert into stocks |
| 762 | values ('2006-01-05','BUY','RHAT',100,35.14)""") |
| 763 | conn.commit() |
| 764 | c.close() |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 765 | |
| 766 | Now we plug :class:`Row` in:: |
| 767 | |
Senthil Kumaran | 946eb86 | 2011-07-03 10:17:22 -0700 | [diff] [blame] | 768 | >>> conn.row_factory = sqlite3.Row |
| 769 | >>> c = conn.cursor() |
| 770 | >>> c.execute('select * from stocks') |
| 771 | <sqlite3.Cursor object at 0x7f4e7dd8fa80> |
| 772 | >>> r = c.fetchone() |
| 773 | >>> type(r) |
| 774 | <class 'sqlite3.Row'> |
| 775 | >>> tuple(r) |
| 776 | ('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14) |
| 777 | >>> len(r) |
| 778 | 5 |
| 779 | >>> r[2] |
| 780 | 'RHAT' |
| 781 | >>> r.keys() |
| 782 | ['date', 'trans', 'symbol', 'qty', 'price'] |
| 783 | >>> r['qty'] |
| 784 | 100.0 |
| 785 | >>> for member in r: |
| 786 | ... print(member) |
| 787 | ... |
| 788 | 2006-01-05 |
| 789 | BUY |
| 790 | RHAT |
| 791 | 100.0 |
| 792 | 35.14 |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 793 | |
| 794 | |
Berker Peksag | ed789f9 | 2016-08-25 00:45:07 +0300 | [diff] [blame] | 795 | .. _sqlite3-exceptions: |
| 796 | |
| 797 | Exceptions |
| 798 | ---------- |
| 799 | |
| 800 | .. exception:: Warning |
| 801 | |
| 802 | A subclass of :exc:`Exception`. |
| 803 | |
| 804 | .. exception:: Error |
| 805 | |
| 806 | The base class of the other exceptions in this module. It is a subclass |
| 807 | of :exc:`Exception`. |
| 808 | |
| 809 | .. exception:: DatabaseError |
| 810 | |
| 811 | Exception raised for errors that are related to the database. |
| 812 | |
| 813 | .. exception:: IntegrityError |
| 814 | |
| 815 | Exception raised when the relational integrity of the database is affected, |
| 816 | e.g. a foreign key check fails. It is a subclass of :exc:`DatabaseError`. |
| 817 | |
| 818 | .. exception:: ProgrammingError |
| 819 | |
| 820 | Exception raised for programming errors, e.g. table not found or already |
| 821 | exists, syntax error in the SQL statement, wrong number of parameters |
| 822 | specified, etc. It is a subclass of :exc:`DatabaseError`. |
| 823 | |
| 824 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 825 | .. _sqlite3-types: |
| 826 | |
| 827 | SQLite and Python types |
| 828 | ----------------------- |
| 829 | |
| 830 | |
| 831 | Introduction |
| 832 | ^^^^^^^^^^^^ |
| 833 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 834 | SQLite natively supports the following types: ``NULL``, ``INTEGER``, |
| 835 | ``REAL``, ``TEXT``, ``BLOB``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 836 | |
| 837 | The following Python types can thus be sent to SQLite without any problem: |
| 838 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 839 | +-------------------------------+-------------+ |
| 840 | | Python type | SQLite type | |
| 841 | +===============================+=============+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 842 | | :const:`None` | ``NULL`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 843 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 844 | | :class:`int` | ``INTEGER`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 845 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 846 | | :class:`float` | ``REAL`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 847 | +-------------------------------+-------------+ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 848 | | :class:`str` | ``TEXT`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 849 | +-------------------------------+-------------+ |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 850 | | :class:`bytes` | ``BLOB`` | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 851 | +-------------------------------+-------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 852 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 853 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 854 | This is how SQLite types are converted to Python types by default: |
| 855 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 856 | +-------------+----------------------------------------------+ |
| 857 | | SQLite type | Python type | |
| 858 | +=============+==============================================+ |
| 859 | | ``NULL`` | :const:`None` | |
| 860 | +-------------+----------------------------------------------+ |
| 861 | | ``INTEGER`` | :class:`int` | |
| 862 | +-------------+----------------------------------------------+ |
| 863 | | ``REAL`` | :class:`float` | |
| 864 | +-------------+----------------------------------------------+ |
| 865 | | ``TEXT`` | depends on :attr:`~Connection.text_factory`, | |
| 866 | | | :class:`str` by default | |
| 867 | +-------------+----------------------------------------------+ |
| 868 | | ``BLOB`` | :class:`bytes` | |
| 869 | +-------------+----------------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 870 | |
| 871 | The type system of the :mod:`sqlite3` module is extensible in two ways: you can |
| 872 | store additional Python types in a SQLite database via object adaptation, and |
| 873 | you can let the :mod:`sqlite3` module convert SQLite types to different Python |
| 874 | types via converters. |
| 875 | |
| 876 | |
| 877 | Using adapters to store additional Python types in SQLite databases |
| 878 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 879 | |
| 880 | As described before, SQLite supports only a limited set of types natively. To |
| 881 | 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] | 882 | sqlite3 module's supported types for SQLite: one of NoneType, int, float, |
Antoine Pitrou | f06917e | 2010-02-02 23:00:29 +0000 | [diff] [blame] | 883 | str, bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 884 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 885 | There are two ways to enable the :mod:`sqlite3` module to adapt a custom Python |
| 886 | type to one of the supported ones. |
| 887 | |
| 888 | |
| 889 | Letting your object adapt itself |
| 890 | """""""""""""""""""""""""""""""" |
| 891 | |
| 892 | This is a good approach if you write the class yourself. Let's suppose you have |
| 893 | a class like this:: |
| 894 | |
Éric Araujo | 28053fb | 2010-11-22 03:09:19 +0000 | [diff] [blame] | 895 | class Point: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 896 | def __init__(self, x, y): |
| 897 | self.x, self.y = x, y |
| 898 | |
| 899 | Now you want to store the point in a single SQLite column. First you'll have to |
| 900 | choose one of the supported types first to be used for representing the point. |
| 901 | Let's just use str and separate the coordinates using a semicolon. Then you need |
| 902 | to give your class a method ``__conform__(self, protocol)`` which must return |
| 903 | the converted value. The parameter *protocol* will be :class:`PrepareProtocol`. |
| 904 | |
| 905 | .. literalinclude:: ../includes/sqlite3/adapter_point_1.py |
| 906 | |
| 907 | |
| 908 | Registering an adapter callable |
| 909 | """"""""""""""""""""""""""""""" |
| 910 | |
| 911 | The other possibility is to create a function that converts the type to the |
| 912 | string representation and register the function with :meth:`register_adapter`. |
| 913 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 914 | .. literalinclude:: ../includes/sqlite3/adapter_point_2.py |
| 915 | |
| 916 | The :mod:`sqlite3` module has two default adapters for Python's built-in |
| 917 | :class:`datetime.date` and :class:`datetime.datetime` types. Now let's suppose |
| 918 | we want to store :class:`datetime.datetime` objects not in ISO representation, |
| 919 | but as a Unix timestamp. |
| 920 | |
| 921 | .. literalinclude:: ../includes/sqlite3/adapter_datetime.py |
| 922 | |
| 923 | |
| 924 | Converting SQLite values to custom Python types |
| 925 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 926 | |
| 927 | Writing an adapter lets you send custom Python types to SQLite. But to make it |
| 928 | really useful we need to make the Python to SQLite to Python roundtrip work. |
| 929 | |
| 930 | Enter converters. |
| 931 | |
| 932 | Let's go back to the :class:`Point` class. We stored the x and y coordinates |
| 933 | separated via semicolons as strings in SQLite. |
| 934 | |
| 935 | First, we'll define a converter function that accepts the string as a parameter |
| 936 | and constructs a :class:`Point` object from it. |
| 937 | |
| 938 | .. note:: |
| 939 | |
Zachary Ware | 9d08562 | 2014-04-01 12:21:56 -0500 | [diff] [blame] | 940 | Converter functions **always** get called with a :class:`bytes` object, no |
| 941 | matter under which data type you sent the value to SQLite. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 942 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 943 | :: |
| 944 | |
| 945 | def convert_point(s): |
Petri Lehtinen | 1ca9395 | 2012-02-15 22:17:21 +0200 | [diff] [blame] | 946 | x, y = map(float, s.split(b";")) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 947 | return Point(x, y) |
| 948 | |
| 949 | Now you need to make the :mod:`sqlite3` module know that what you select from |
| 950 | the database is actually a point. There are two ways of doing this: |
| 951 | |
| 952 | * Implicitly via the declared type |
| 953 | |
| 954 | * Explicitly via the column name |
| 955 | |
| 956 | Both ways are described in section :ref:`sqlite3-module-contents`, in the entries |
| 957 | for the constants :const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES`. |
| 958 | |
| 959 | The following example illustrates both approaches. |
| 960 | |
| 961 | .. literalinclude:: ../includes/sqlite3/converter_point.py |
| 962 | |
| 963 | |
| 964 | Default adapters and converters |
| 965 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 966 | |
| 967 | There are default adapters for the date and datetime types in the datetime |
| 968 | module. They will be sent as ISO dates/ISO timestamps to SQLite. |
| 969 | |
| 970 | The default converters are registered under the name "date" for |
| 971 | :class:`datetime.date` and under the name "timestamp" for |
| 972 | :class:`datetime.datetime`. |
| 973 | |
| 974 | This way, you can use date/timestamps from Python without any additional |
| 975 | fiddling in most cases. The format of the adapters is also compatible with the |
| 976 | experimental SQLite date/time functions. |
| 977 | |
| 978 | The following example demonstrates this. |
| 979 | |
| 980 | .. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py |
| 981 | |
Petri Lehtinen | 5f79409 | 2013-02-26 21:32:02 +0200 | [diff] [blame] | 982 | If a timestamp stored in SQLite has a fractional part longer than 6 |
| 983 | numbers, its value will be truncated to microsecond precision by the |
| 984 | timestamp converter. |
| 985 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 986 | |
| 987 | .. _sqlite3-controlling-transactions: |
| 988 | |
| 989 | Controlling Transactions |
| 990 | ------------------------ |
| 991 | |
| 992 | By default, the :mod:`sqlite3` module opens transactions implicitly before a |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 993 | Data Modification Language (DML) statement (i.e. |
Berker Peksag | ab994ed | 2016-09-11 12:57:15 +0300 | [diff] [blame] | 994 | ``INSERT``/``UPDATE``/``DELETE``/``REPLACE``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 995 | |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 996 | You can control which kind of ``BEGIN`` statements sqlite3 implicitly executes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 997 | (or none at all) via the *isolation_level* parameter to the :func:`connect` |
| 998 | call, or via the :attr:`isolation_level` property of connections. |
| 999 | |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 1000 | If you want **autocommit mode**, then set :attr:`isolation_level` to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1001 | |
| 1002 | 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] | 1003 | statement, or set it to one of SQLite's supported isolation levels: "DEFERRED", |
| 1004 | "IMMEDIATE" or "EXCLUSIVE". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1005 | |
Berker Peksag | fe70d92 | 2017-02-26 18:31:12 +0300 | [diff] [blame] | 1006 | The current transaction state is exposed through the |
| 1007 | :attr:`Connection.in_transaction` attribute of the connection object. |
| 1008 | |
Berker Peksag | ab994ed | 2016-09-11 12:57:15 +0300 | [diff] [blame] | 1009 | .. versionchanged:: 3.6 |
| 1010 | :mod:`sqlite3` used to implicitly commit an open transaction before DDL |
| 1011 | statements. This is no longer the case. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1012 | |
| 1013 | |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 1014 | Using :mod:`sqlite3` efficiently |
| 1015 | -------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1016 | |
| 1017 | |
| 1018 | Using shortcut methods |
| 1019 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 1020 | |
| 1021 | Using the nonstandard :meth:`execute`, :meth:`executemany` and |
| 1022 | :meth:`executescript` methods of the :class:`Connection` object, your code can |
| 1023 | be written more concisely because you don't have to create the (often |
| 1024 | superfluous) :class:`Cursor` objects explicitly. Instead, the :class:`Cursor` |
| 1025 | objects are created implicitly and these shortcut methods return the cursor |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 1026 | 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] | 1027 | directly using only a single call on the :class:`Connection` object. |
| 1028 | |
| 1029 | .. literalinclude:: ../includes/sqlite3/shortcut_methods.py |
| 1030 | |
| 1031 | |
| 1032 | Accessing columns by name instead of by index |
| 1033 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1034 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 1035 | One useful feature of the :mod:`sqlite3` module is the built-in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1036 | :class:`sqlite3.Row` class designed to be used as a row factory. |
| 1037 | |
| 1038 | Rows wrapped with this class can be accessed both by index (like tuples) and |
| 1039 | case-insensitively by name: |
| 1040 | |
| 1041 | .. literalinclude:: ../includes/sqlite3/rowclass.py |
| 1042 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 1043 | |
| 1044 | Using the connection as a context manager |
| 1045 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1046 | |
Gerhard Häring | 0d7d6cf | 2008-03-29 01:32:44 +0000 | [diff] [blame] | 1047 | Connection objects can be used as context managers |
| 1048 | that automatically commit or rollback transactions. In the event of an |
| 1049 | exception, the transaction is rolled back; otherwise, the transaction is |
| 1050 | committed: |
| 1051 | |
| 1052 | .. literalinclude:: ../includes/sqlite3/ctx_manager.py |
Gerhard Häring | c34d76c | 2010-08-06 06:12:05 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
| 1055 | Common issues |
| 1056 | ------------- |
| 1057 | |
| 1058 | Multithreading |
| 1059 | ^^^^^^^^^^^^^^ |
| 1060 | |
| 1061 | Older SQLite versions had issues with sharing connections between threads. |
| 1062 | That's why the Python module disallows sharing connections and cursors between |
| 1063 | threads. If you still try to do so, you will get an exception at runtime. |
| 1064 | |
| 1065 | The only exception is calling the :meth:`~Connection.interrupt` method, which |
| 1066 | only makes sense to call from a different thread. |
| 1067 | |
Gerhard Häring | e0941c5 | 2010-10-03 21:47:06 +0000 | [diff] [blame] | 1068 | .. rubric:: Footnotes |
| 1069 | |
| 1070 | .. [#f1] The sqlite3 module is not built with loadable extension support by |
Senthil Kumaran | 946eb86 | 2011-07-03 10:17:22 -0700 | [diff] [blame] | 1071 | default, because some platforms (notably Mac OS X) have SQLite |
| 1072 | libraries which are compiled without this feature. To get loadable |
| 1073 | extension support, you must pass --enable-loadable-sqlite-extensions to |
| 1074 | configure. |