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