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