gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2916 lines
300KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="sqlite3 — DB-API 2.0 interface for SQLite databases" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/sqlite3.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/sqlite3/ SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/sqlite3/ SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>sqlite3 — DB-API 2.0 interface for SQLite databases &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Data Compression and Archiving" href="archiving.html" />
  33. <link rel="prev" title="dbm — Interfaces to Unix “databases”" href="dbm.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/sqlite3.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a><ul>
  86. <li><a class="reference internal" href="#tutorial">Tutorial</a></li>
  87. <li><a class="reference internal" href="#reference">Reference</a><ul>
  88. <li><a class="reference internal" href="#module-functions">Module functions</a></li>
  89. <li><a class="reference internal" href="#module-constants">Module constants</a></li>
  90. <li><a class="reference internal" href="#connection-objects">Connection objects</a></li>
  91. <li><a class="reference internal" href="#cursor-objects">Cursor objects</a></li>
  92. <li><a class="reference internal" href="#row-objects">Row objects</a></li>
  93. <li><a class="reference internal" href="#blob-objects">Blob objects</a></li>
  94. <li><a class="reference internal" href="#prepareprotocol-objects">PrepareProtocol objects</a></li>
  95. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  96. <li><a class="reference internal" href="#sqlite-and-python-types">SQLite and Python types</a></li>
  97. <li><a class="reference internal" href="#default-adapters-and-converters-deprecated">Default adapters and converters (deprecated)</a></li>
  98. <li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>
  99. </ul>
  100. </li>
  101. <li><a class="reference internal" href="#how-to-guides">How-to guides</a><ul>
  102. <li><a class="reference internal" href="#how-to-use-placeholders-to-bind-values-in-sql-queries">How to use placeholders to bind values in SQL queries</a></li>
  103. <li><a class="reference internal" href="#how-to-adapt-custom-python-types-to-sqlite-values">How to adapt custom Python types to SQLite values</a><ul>
  104. <li><a class="reference internal" href="#how-to-write-adaptable-objects">How to write adaptable objects</a></li>
  105. <li><a class="reference internal" href="#how-to-register-adapter-callables">How to register adapter callables</a></li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#how-to-convert-sqlite-values-to-custom-python-types">How to convert SQLite values to custom Python types</a></li>
  109. <li><a class="reference internal" href="#adapter-and-converter-recipes">Adapter and converter recipes</a></li>
  110. <li><a class="reference internal" href="#how-to-use-connection-shortcut-methods">How to use connection shortcut methods</a></li>
  111. <li><a class="reference internal" href="#how-to-use-the-connection-context-manager">How to use the connection context manager</a></li>
  112. <li><a class="reference internal" href="#how-to-work-with-sqlite-uris">How to work with SQLite URIs</a></li>
  113. <li><a class="reference internal" href="#how-to-create-and-use-row-factories">How to create and use row factories</a></li>
  114. <li><a class="reference internal" href="#how-to-handle-non-utf-8-text-encodings">How to handle non-UTF-8 text encodings</a></li>
  115. </ul>
  116. </li>
  117. <li><a class="reference internal" href="#explanation">Explanation</a><ul>
  118. <li><a class="reference internal" href="#transaction-control">Transaction control</a><ul>
  119. <li><a class="reference internal" href="#transaction-control-via-the-autocommit-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">autocommit</span></code> attribute</a></li>
  120. <li><a class="reference internal" href="#transaction-control-via-the-isolation-level-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">isolation_level</span></code> attribute</a></li>
  121. </ul>
  122. </li>
  123. </ul>
  124. </li>
  125. </ul>
  126. </li>
  127. </ul>
  128. </div>
  129. <div>
  130. <h4>Previous topic</h4>
  131. <p class="topless"><a href="dbm.html"
  132. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a></p>
  133. </div>
  134. <div>
  135. <h4>Next topic</h4>
  136. <p class="topless"><a href="archiving.html"
  137. title="next chapter">Data Compression and Archiving</a></p>
  138. </div>
  139. <div role="note" aria-label="source link">
  140. <h3>This Page</h3>
  141. <ul class="this-page-menu">
  142. <li><a href="../bugs.html">Report a Bug</a></li>
  143. <li>
  144. <a href="https://github.com/python/cpython/blob/main/Doc/library/sqlite3.rst"
  145. rel="nofollow">Show Source
  146. </a>
  147. </li>
  148. </ul>
  149. </div>
  150. </nav>
  151. </div>
  152. </div>
  153. <div class="related" role="navigation" aria-label="related navigation">
  154. <h3>Navigation</h3>
  155. <ul>
  156. <li class="right" style="margin-right: 10px">
  157. <a href="../genindex.html" title="General Index"
  158. accesskey="I">index</a></li>
  159. <li class="right" >
  160. <a href="../py-modindex.html" title="Python Module Index"
  161. >modules</a> |</li>
  162. <li class="right" >
  163. <a href="archiving.html" title="Data Compression and Archiving"
  164. accesskey="N">next</a> |</li>
  165. <li class="right" >
  166. <a href="dbm.html" title="dbm — Interfaces to Unix “databases”"
  167. accesskey="P">previous</a> |</li>
  168. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  169. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  170. <li class="switchers">
  171. <div class="language_switcher_placeholder"></div>
  172. <div class="version_switcher_placeholder"></div>
  173. </li>
  174. <li>
  175. </li>
  176. <li id="cpython-language-and-version">
  177. <a href="../index.html">3.12.3 Documentation</a> &#187;
  178. </li>
  179. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  180. <li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">Data Persistence</a> &#187;</li>
  181. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a></li>
  182. <li class="right">
  183. <div class="inline-search" role="search">
  184. <form class="inline-search" action="../search.html" method="get">
  185. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  186. <input type="submit" value="Go" />
  187. </form>
  188. </div>
  189. |
  190. </li>
  191. <li class="right">
  192. <label class="theme-selector-label">
  193. Theme
  194. <select class="theme-selector" oninput="activateTheme(this.value)">
  195. <option value="auto" selected>Auto</option>
  196. <option value="light">Light</option>
  197. <option value="dark">Dark</option>
  198. </select>
  199. </label> |</li>
  200. </ul>
  201. </div>
  202. <div class="document">
  203. <div class="documentwrapper">
  204. <div class="bodywrapper">
  205. <div class="body" role="main">
  206. <section id="module-sqlite3">
  207. <span id="sqlite3-db-api-2-0-interface-for-sqlite-databases"></span><h1><a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> — DB-API 2.0 interface for SQLite databases<a class="headerlink" href="#module-sqlite3" title="Link to this heading">¶</a></h1>
  208. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/sqlite3/">Lib/sqlite3/</a></p>
  209. <p id="sqlite3-intro">SQLite is a C library that provides a lightweight disk-based database that
  210. doesn’t require a separate server process and allows accessing the database
  211. using a nonstandard variant of the SQL query language. Some applications can use
  212. SQLite for internal data storage. It’s also possible to prototype an
  213. application using SQLite and then port the code to a larger database such as
  214. PostgreSQL or Oracle.</p>
  215. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module was written by Gerhard Häring. It provides an SQL interface
  216. compliant with the DB-API 2.0 specification described by <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>, and
  217. requires SQLite 3.7.15 or newer.</p>
  218. <p>This document includes four main sections:</p>
  219. <ul class="simple">
  220. <li><p><a class="reference internal" href="#sqlite3-tutorial"><span class="std std-ref">Tutorial</span></a> teaches how to use the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module.</p></li>
  221. <li><p><a class="reference internal" href="#sqlite3-reference"><span class="std std-ref">Reference</span></a> describes the classes and functions this module
  222. defines.</p></li>
  223. <li><p><a class="reference internal" href="#sqlite3-howtos"><span class="std std-ref">How-to guides</span></a> details how to handle specific tasks.</p></li>
  224. <li><p><a class="reference internal" href="#sqlite3-explanation"><span class="std std-ref">Explanation</span></a> provides in-depth background on
  225. transaction control.</p></li>
  226. </ul>
  227. <div class="admonition seealso">
  228. <p class="admonition-title">See also</p>
  229. <dl class="simple">
  230. <dt><a class="reference external" href="https://www.sqlite.org">https://www.sqlite.org</a></dt><dd><p>The SQLite web page; the documentation describes the syntax and the
  231. available data types for the supported SQL dialect.</p>
  232. </dd>
  233. <dt><a class="reference external" href="https://www.w3schools.com/sql/">https://www.w3schools.com/sql/</a></dt><dd><p>Tutorial, reference and examples for learning SQL syntax.</p>
  234. </dd>
  235. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a> - Database API Specification 2.0</dt><dd><p>PEP written by Marc-André Lemburg.</p>
  236. </dd>
  237. </dl>
  238. </div>
  239. <section id="tutorial">
  240. <span id="sqlite3-tutorial"></span><h2>Tutorial<a class="headerlink" href="#tutorial" title="Link to this heading">¶</a></h2>
  241. <p>In this tutorial, you will create a database of Monty Python movies
  242. using basic <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> functionality.
  243. It assumes a fundamental understanding of database concepts,
  244. including <a class="reference external" href="https://en.wikipedia.org/wiki/Cursor_(databases)">cursors</a> and <a class="reference external" href="https://en.wikipedia.org/wiki/Database_transaction">transactions</a>.</p>
  245. <p>First, we need to create a new database and open
  246. a database connection to allow <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> to work with it.
  247. Call <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">sqlite3.connect()</span></code></a> to create a connection to
  248. the database <code class="file docutils literal notranslate"><span class="pre">tutorial.db</span></code> in the current working directory,
  249. implicitly creating it if it does not exist:</p>
  250. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sqlite3</span>
  251. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;tutorial.db&quot;</span><span class="p">)</span>
  252. </pre></div>
  253. </div>
  254. <p>The returned <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object <code class="docutils literal notranslate"><span class="pre">con</span></code>
  255. represents the connection to the on-disk database.</p>
  256. <p>In order to execute SQL statements and fetch results from SQL queries,
  257. we will need to use a database cursor.
  258. Call <a class="reference internal" href="#sqlite3.Connection.cursor" title="sqlite3.Connection.cursor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">con.cursor()</span></code></a> to create the <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a>:</p>
  259. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  260. </pre></div>
  261. </div>
  262. <p>Now that we’ve got a database connection and a cursor,
  263. we can create a database table <code class="docutils literal notranslate"><span class="pre">movie</span></code> with columns for title,
  264. release year, and review score.
  265. For simplicity, we can just use column names in the table declaration –
  266. thanks to the <a class="reference external" href="https://www.sqlite.org/flextypegood.html">flexible typing</a> feature of SQLite,
  267. specifying the data types is optional.
  268. Execute the <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">TABLE</span></code> statement
  269. by calling <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cur.execute(...)</span></code></a>:</p>
  270. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE movie(title, year, score)&quot;</span><span class="p">)</span>
  271. </pre></div>
  272. </div>
  273. <p>We can verify that the new table has been created by querying
  274. the <code class="docutils literal notranslate"><span class="pre">sqlite_master</span></code> table built-in to SQLite,
  275. which should now contain an entry for the <code class="docutils literal notranslate"><span class="pre">movie</span></code> table definition
  276. (see <a class="reference external" href="https://www.sqlite.org/schematab.html">The Schema Table</a> for details).
  277. Execute that query by calling <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cur.execute(...)</span></code></a>,
  278. assign the result to <code class="docutils literal notranslate"><span class="pre">res</span></code>,
  279. and call <a class="reference internal" href="#sqlite3.Cursor.fetchone" title="sqlite3.Cursor.fetchone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">res.fetchone()</span></code></a> to fetch the resulting row:</p>
  280. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT name FROM sqlite_master&quot;</span><span class="p">)</span>
  281. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
  282. <span class="go">(&#39;movie&#39;,)</span>
  283. </pre></div>
  284. </div>
  285. <p>We can see that the table has been created,
  286. as the query returns a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> containing the table’s name.
  287. If we query <code class="docutils literal notranslate"><span class="pre">sqlite_master</span></code> for a non-existent table <code class="docutils literal notranslate"><span class="pre">spam</span></code>,
  288. <code class="xref py py-meth docutils literal notranslate"><span class="pre">res.fetchone()</span></code> will return <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
  289. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT name FROM sqlite_master WHERE name=&#39;spam&#39;&quot;</span><span class="p">)</span>
  290. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">None</span>
  291. <span class="go">True</span>
  292. </pre></div>
  293. </div>
  294. <p>Now, add two rows of data supplied as SQL literals
  295. by executing an <code class="docutils literal notranslate"><span class="pre">INSERT</span></code> statement,
  296. once again by calling <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cur.execute(...)</span></code></a>:</p>
  297. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  298. <span class="s2"> INSERT INTO movie VALUES</span>
  299. <span class="s2"> (&#39;Monty Python and the Holy Grail&#39;, 1975, 8.2),</span>
  300. <span class="s2"> (&#39;And Now for Something Completely Different&#39;, 1971, 7.5)</span>
  301. <span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
  302. </pre></div>
  303. </div>
  304. <p>The <code class="docutils literal notranslate"><span class="pre">INSERT</span></code> statement implicitly opens a transaction,
  305. which needs to be committed before changes are saved in the database
  306. (see <a class="reference internal" href="#sqlite3-controlling-transactions"><span class="std std-ref">Transaction control</span></a> for details).
  307. Call <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">con.commit()</span></code></a> on the connection object
  308. to commit the transaction:</p>
  309. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
  310. </pre></div>
  311. </div>
  312. <p>We can verify that the data was inserted correctly
  313. by executing a <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> query.
  314. Use the now-familiar <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cur.execute(...)</span></code></a> to
  315. assign the result to <code class="docutils literal notranslate"><span class="pre">res</span></code>,
  316. and call <a class="reference internal" href="#sqlite3.Cursor.fetchall" title="sqlite3.Cursor.fetchall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">res.fetchall()</span></code></a> to return all resulting rows:</p>
  317. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT score FROM movie&quot;</span><span class="p">)</span>
  318. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
  319. <span class="go">[(8.2,), (7.5,)]</span>
  320. </pre></div>
  321. </div>
  322. <p>The result is a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> of two <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code>s, one per row,
  323. each containing that row’s <code class="docutils literal notranslate"><span class="pre">score</span></code> value.</p>
  324. <p>Now, insert three more rows by calling
  325. <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cur.executemany(...)</span></code></a>:</p>
  326. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span>
  327. <span class="p">(</span><span class="s2">&quot;Monty Python Live at the Hollywood Bowl&quot;</span><span class="p">,</span> <span class="mi">1982</span><span class="p">,</span> <span class="mf">7.9</span><span class="p">),</span>
  328. <span class="p">(</span><span class="s2">&quot;Monty Python&#39;s The Meaning of Life&quot;</span><span class="p">,</span> <span class="mi">1983</span><span class="p">,</span> <span class="mf">7.5</span><span class="p">),</span>
  329. <span class="p">(</span><span class="s2">&quot;Monty Python&#39;s Life of Brian&quot;</span><span class="p">,</span> <span class="mi">1979</span><span class="p">,</span> <span class="mf">8.0</span><span class="p">),</span>
  330. <span class="p">]</span>
  331. <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO movie VALUES(?, ?, ?)&quot;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
  332. <span class="n">con</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span> <span class="c1"># Remember to commit the transaction after executing INSERT.</span>
  333. </pre></div>
  334. </div>
  335. <p>Notice that <code class="docutils literal notranslate"><span class="pre">?</span></code> placeholders are used to bind <code class="docutils literal notranslate"><span class="pre">data</span></code> to the query.
  336. Always use placeholders instead of <a class="reference internal" href="../tutorial/inputoutput.html#tut-formatting"><span class="std std-ref">string formatting</span></a>
  337. to bind Python values to SQL statements,
  338. to avoid <a class="reference external" href="https://en.wikipedia.org/wiki/SQL_injection">SQL injection attacks</a>
  339. (see <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">How to use placeholders to bind values in SQL queries</span></a> for more details).</p>
  340. <p>We can verify that the new rows were inserted
  341. by executing a <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> query,
  342. this time iterating over the results of the query:</p>
  343. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT year, title FROM movie ORDER BY year&quot;</span><span class="p">):</span>
  344. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  345. <span class="go">(1971, &#39;And Now for Something Completely Different&#39;)</span>
  346. <span class="go">(1975, &#39;Monty Python and the Holy Grail&#39;)</span>
  347. <span class="go">(1979, &quot;Monty Python&#39;s Life of Brian&quot;)</span>
  348. <span class="go">(1982, &#39;Monty Python Live at the Hollywood Bowl&#39;)</span>
  349. <span class="go">(1983, &quot;Monty Python&#39;s The Meaning of Life&quot;)</span>
  350. </pre></div>
  351. </div>
  352. <p>Each row is a two-item <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> of <code class="docutils literal notranslate"><span class="pre">(year,</span> <span class="pre">title)</span></code>,
  353. matching the columns selected in the query.</p>
  354. <p>Finally, verify that the database has been written to disk
  355. by calling <a class="reference internal" href="#sqlite3.Connection.close" title="sqlite3.Connection.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">con.close()</span></code></a>
  356. to close the existing connection, opening a new one,
  357. creating a new cursor, then querying the database:</p>
  358. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  359. <span class="gp">&gt;&gt;&gt; </span><span class="n">new_con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;tutorial.db&quot;</span><span class="p">)</span>
  360. <span class="gp">&gt;&gt;&gt; </span><span class="n">new_cur</span> <span class="o">=</span> <span class="n">new_con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  361. <span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">new_cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT title, year FROM movie ORDER BY score DESC&quot;</span><span class="p">)</span>
  362. <span class="gp">&gt;&gt;&gt; </span><span class="n">title</span><span class="p">,</span> <span class="n">year</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
  363. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;The highest scoring Monty Python movie is </span><span class="si">{</span><span class="n">title</span><span class="si">!r}</span><span class="s1">, released in </span><span class="si">{</span><span class="n">year</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  364. <span class="go">The highest scoring Monty Python movie is &#39;Monty Python and the Holy Grail&#39;, released in 1975</span>
  365. <span class="gp">&gt;&gt;&gt; </span><span class="n">new_con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  366. </pre></div>
  367. </div>
  368. <p>You’ve now created an SQLite database using the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module,
  369. inserted data and retrieved values from it in multiple ways.</p>
  370. <div class="admonition seealso">
  371. <p class="admonition-title">See also</p>
  372. <ul class="simple">
  373. <li><p><a class="reference internal" href="#sqlite3-howtos"><span class="std std-ref">How-to guides</span></a> for further reading:</p>
  374. <ul>
  375. <li><p><a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">How to use placeholders to bind values in SQL queries</span></a></p></li>
  376. <li><p><a class="reference internal" href="#sqlite3-adapters"><span class="std std-ref">How to adapt custom Python types to SQLite values</span></a></p></li>
  377. <li><p><a class="reference internal" href="#sqlite3-converters"><span class="std std-ref">How to convert SQLite values to custom Python types</span></a></p></li>
  378. <li><p><a class="reference internal" href="#sqlite3-connection-context-manager"><span class="std std-ref">How to use the connection context manager</span></a></p></li>
  379. <li><p><a class="reference internal" href="#sqlite3-howto-row-factory"><span class="std std-ref">How to create and use row factories</span></a></p></li>
  380. </ul>
  381. </li>
  382. <li><p><a class="reference internal" href="#sqlite3-explanation"><span class="std std-ref">Explanation</span></a> for in-depth background on transaction control.</p></li>
  383. </ul>
  384. </div>
  385. </section>
  386. <section id="reference">
  387. <span id="sqlite3-reference"></span><h2>Reference<a class="headerlink" href="#reference" title="Link to this heading">¶</a></h2>
  388. <section id="module-functions">
  389. <span id="sqlite3-module-functions"></span><span id="sqlite3-module-contents"></span><h3>Module functions<a class="headerlink" href="#module-functions" title="Link to this heading">¶</a></h3>
  390. <dl class="py function">
  391. <dt class="sig sig-object py" id="sqlite3.connect">
  392. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">connect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">database</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">5.0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">detect_types</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">isolation_level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'DEFERRED'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_same_thread</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sqlite3.Connection</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cached_statements</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">128</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">uri</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autocommit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">sqlite3.LEGACY_TRANSACTION_CONTROL</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.connect" title="Link to this definition">¶</a></dt>
  393. <dd><p>Open a connection to an SQLite database.</p>
  394. <dl class="field-list simple">
  395. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  396. <dd class="field-odd"><ul class="simple">
  397. <li><p><strong>database</strong> (<a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>) – The path to the database file to be opened.
  398. You can pass <code class="docutils literal notranslate"><span class="pre">&quot;:memory:&quot;</span></code> to create an <a class="reference external" href="https://sqlite.org/inmemorydb.html">SQLite database existing only
  399. in memory</a>, and open a connection
  400. to it.</p></li>
  401. <li><p><strong>timeout</strong> (<a class="reference internal" href="functions.html#float" title="float"><em>float</em></a>) – How many seconds the connection should wait before raising
  402. an <a class="reference internal" href="#sqlite3.OperationalError" title="sqlite3.OperationalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OperationalError</span></code></a> when a table is locked.
  403. If another connection opens a transaction to modify a table,
  404. that table will be locked until the transaction is committed.
  405. Default five seconds.</p></li>
  406. <li><p><strong>detect_types</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – Control whether and how data types not
  407. <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">natively supported by SQLite</span></a>
  408. are looked up to be converted to Python types,
  409. using the converters registered with <a class="reference internal" href="#sqlite3.register_converter" title="sqlite3.register_converter"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_converter()</span></code></a>.
  410. Set it to any combination (using <code class="docutils literal notranslate"><span class="pre">|</span></code>, bitwise or) of
  411. <a class="reference internal" href="#sqlite3.PARSE_DECLTYPES" title="sqlite3.PARSE_DECLTYPES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_DECLTYPES</span></code></a> and <a class="reference internal" href="#sqlite3.PARSE_COLNAMES" title="sqlite3.PARSE_COLNAMES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_COLNAMES</span></code></a>
  412. to enable this.
  413. Column names takes precedence over declared types if both flags are set.
  414. Types cannot be detected for generated fields (for example <code class="docutils literal notranslate"><span class="pre">max(data)</span></code>),
  415. even when the <em>detect_types</em> parameter is set; <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> will be
  416. returned instead.
  417. By default (<code class="docutils literal notranslate"><span class="pre">0</span></code>), type detection is disabled.</p></li>
  418. <li><p><strong>isolation_level</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em> | </em><em>None</em>) – Control legacy transaction handling behaviour.
  419. See <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.isolation_level</span></code></a> and
  420. <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">Transaction control via the isolation_level attribute</span></a> for more information.
  421. Can be <code class="docutils literal notranslate"><span class="pre">&quot;DEFERRED&quot;</span></code> (default), <code class="docutils literal notranslate"><span class="pre">&quot;EXCLUSIVE&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;IMMEDIATE&quot;</span></code>;
  422. or <code class="docutils literal notranslate"><span class="pre">None</span></code> to disable opening transactions implicitly.
  423. Has no effect unless <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.autocommit</span></code></a> is set to
  424. <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-const docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a> (the default).</p></li>
  425. <li><p><strong>check_same_thread</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – If <code class="docutils literal notranslate"><span class="pre">True</span></code> (default), <a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProgrammingError</span></code></a> will be raised
  426. if the database connection is used by a thread
  427. other than the one that created it.
  428. If <code class="docutils literal notranslate"><span class="pre">False</span></code>, the connection may be accessed in multiple threads;
  429. write operations may need to be serialized by the user
  430. to avoid data corruption.
  431. See <a class="reference internal" href="#sqlite3.threadsafety" title="sqlite3.threadsafety"><code class="xref py py-attr docutils literal notranslate"><span class="pre">threadsafety</span></code></a> for more information.</p></li>
  432. <li><p><strong>factory</strong> (<a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><em>Connection</em></a>) – A custom subclass of <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> to create the connection with,
  433. if not the default <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> class.</p></li>
  434. <li><p><strong>cached_statements</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The number of statements that <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>
  435. should internally cache for this connection, to avoid parsing overhead.
  436. By default, 128 statements.</p></li>
  437. <li><p><strong>uri</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – If set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, <em>database</em> is interpreted as a
  438. <abbr title="Uniform Resource Identifier">URI</abbr> with a file path
  439. and an optional query string.
  440. The scheme part <em>must</em> be <code class="docutils literal notranslate"><span class="pre">&quot;file:&quot;</span></code>,
  441. and the path can be relative or absolute.
  442. The query string allows passing parameters to SQLite,
  443. enabling various <a class="reference internal" href="#sqlite3-uri-tricks"><span class="std std-ref">How to work with SQLite URIs</span></a>.</p></li>
  444. <li><p><strong>autocommit</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – Control <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a> transaction handling behaviour.
  445. See <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.autocommit</span></code></a> and
  446. <a class="reference internal" href="#sqlite3-transaction-control-autocommit"><span class="std std-ref">Transaction control via the autocommit attribute</span></a> for more information.
  447. <em>autocommit</em> currently defaults to
  448. <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-const docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>.
  449. The default will change to <code class="docutils literal notranslate"><span class="pre">False</span></code> in a future Python release.</p></li>
  450. </ul>
  451. </dd>
  452. <dt class="field-even">Return type<span class="colon">:</span></dt>
  453. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><em>Connection</em></a></p>
  454. </dd>
  455. </dl>
  456. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">sqlite3.connect</span></code> with argument <code class="docutils literal notranslate"><span class="pre">database</span></code>.</p>
  457. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">sqlite3.connect/handle</span></code> with argument <code class="docutils literal notranslate"><span class="pre">connection_handle</span></code>.</p>
  458. <div class="versionchanged">
  459. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>uri</em> parameter.</p>
  460. </div>
  461. <div class="versionchanged">
  462. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>database</em> can now also be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>, not only a string.</p>
  463. </div>
  464. <div class="versionchanged">
  465. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="docutils literal notranslate"><span class="pre">sqlite3.connect/handle</span></code> auditing event.</p>
  466. </div>
  467. <div class="versionchanged">
  468. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>autocommit</em> parameter.</p>
  469. </div>
  470. </dd></dl>
  471. <dl class="py function">
  472. <dt class="sig sig-object py" id="sqlite3.complete_statement">
  473. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">complete_statement</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">statement</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.complete_statement" title="Link to this definition">¶</a></dt>
  474. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the string <em>statement</em> appears to contain
  475. one or more complete SQL statements.
  476. No syntactic verification or parsing of any kind is performed,
  477. other than checking that there are no unclosed string literals
  478. and the statement is terminated by a semicolon.</p>
  479. <p>For example:</p>
  480. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sqlite3</span><span class="o">.</span><span class="n">complete_statement</span><span class="p">(</span><span class="s2">&quot;SELECT foo FROM bar;&quot;</span><span class="p">)</span>
  481. <span class="go">True</span>
  482. <span class="gp">&gt;&gt;&gt; </span><span class="n">sqlite3</span><span class="o">.</span><span class="n">complete_statement</span><span class="p">(</span><span class="s2">&quot;SELECT foo&quot;</span><span class="p">)</span>
  483. <span class="go">False</span>
  484. </pre></div>
  485. </div>
  486. <p>This function may be useful during command-line input
  487. to determine if the entered text seems to form a complete SQL statement,
  488. or if additional input is needed before calling <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a>.</p>
  489. <p>See <code class="xref py py-func docutils literal notranslate"><span class="pre">runsource()</span></code> in <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/sqlite3/__main__.py">Lib/sqlite3/__main__.py</a>
  490. for real-world use.</p>
  491. </dd></dl>
  492. <dl class="py function">
  493. <dt class="sig sig-object py" id="sqlite3.enable_callback_tracebacks">
  494. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">enable_callback_tracebacks</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.enable_callback_tracebacks" title="Link to this definition">¶</a></dt>
  495. <dd><p>Enable or disable callback tracebacks.
  496. By default you will not get any tracebacks in user-defined functions,
  497. aggregates, converters, authorizer callbacks etc. If you want to debug them,
  498. you can call this function with <em>flag</em> set to <code class="docutils literal notranslate"><span class="pre">True</span></code>. Afterwards, you
  499. will get tracebacks from callbacks on <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>. Use <code class="docutils literal notranslate"><span class="pre">False</span></code>
  500. to disable the feature again.</p>
  501. <div class="admonition note">
  502. <p class="admonition-title">Note</p>
  503. <p>Errors in user-defined function callbacks are logged as unraisable exceptions.
  504. Use an <a class="reference internal" href="sys.html#sys.unraisablehook" title="sys.unraisablehook"><code class="xref py py-func docutils literal notranslate"><span class="pre">unraisable</span> <span class="pre">hook</span> <span class="pre">handler</span></code></a> for
  505. introspection of the failed callback.</p>
  506. </div>
  507. </dd></dl>
  508. <dl class="py function">
  509. <dt class="sig sig-object py" id="sqlite3.register_adapter">
  510. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">register_adapter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">adapter</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.register_adapter" title="Link to this definition">¶</a></dt>
  511. <dd><p>Register an <em>adapter</em> <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> to adapt the Python type <em>type</em>
  512. into an SQLite type.
  513. The adapter is called with a Python object of type <em>type</em> as its sole
  514. argument, and must return a value of a
  515. <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">type that SQLite natively understands</span></a>.</p>
  516. </dd></dl>
  517. <dl class="py function">
  518. <dt class="sig sig-object py" id="sqlite3.register_converter">
  519. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">register_converter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">converter</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.register_converter" title="Link to this definition">¶</a></dt>
  520. <dd><p>Register the <em>converter</em> <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> to convert SQLite objects of type
  521. <em>typename</em> into a Python object of a specific type.
  522. The converter is invoked for all SQLite values of type <em>typename</em>;
  523. it is passed a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object and should return an object of the
  524. desired Python type.
  525. Consult the parameter <em>detect_types</em> of
  526. <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a> for information regarding how type detection works.</p>
  527. <p>Note: <em>typename</em> and the name of the type in your query are matched
  528. case-insensitively.</p>
  529. </dd></dl>
  530. </section>
  531. <section id="module-constants">
  532. <span id="sqlite3-module-constants"></span><h3>Module constants<a class="headerlink" href="#module-constants" title="Link to this heading">¶</a></h3>
  533. <dl class="py data">
  534. <dt class="sig sig-object py" id="sqlite3.LEGACY_TRANSACTION_CONTROL">
  535. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></span><a class="headerlink" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="Link to this definition">¶</a></dt>
  536. <dd><p>Set <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> to this constant to select
  537. old style (pre-Python 3.12) transaction control behaviour.
  538. See <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">Transaction control via the isolation_level attribute</span></a> for more information.</p>
  539. </dd></dl>
  540. <dl class="py data">
  541. <dt class="sig sig-object py" id="sqlite3.PARSE_COLNAMES">
  542. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">PARSE_COLNAMES</span></span><a class="headerlink" href="#sqlite3.PARSE_COLNAMES" title="Link to this definition">¶</a></dt>
  543. <dd><p>Pass this flag value to the <em>detect_types</em> parameter of
  544. <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a> to look up a converter function by
  545. using the type name, parsed from the query column name,
  546. as the converter dictionary key.
  547. The type name must be wrapped in square brackets (<code class="docutils literal notranslate"><span class="pre">[]</span></code>).</p>
  548. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="ss">&quot;p [point]&quot;</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">test</span><span class="p">;</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">look</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">converter</span><span class="w"> </span><span class="ss">&quot;point&quot;</span>
  549. </pre></div>
  550. </div>
  551. <p>This flag may be combined with <a class="reference internal" href="#sqlite3.PARSE_DECLTYPES" title="sqlite3.PARSE_DECLTYPES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_DECLTYPES</span></code></a> using the <code class="docutils literal notranslate"><span class="pre">|</span></code>
  552. (bitwise or) operator.</p>
  553. </dd></dl>
  554. <dl class="py data">
  555. <dt class="sig sig-object py" id="sqlite3.PARSE_DECLTYPES">
  556. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">PARSE_DECLTYPES</span></span><a class="headerlink" href="#sqlite3.PARSE_DECLTYPES" title="Link to this definition">¶</a></dt>
  557. <dd><p>Pass this flag value to the <em>detect_types</em> parameter of
  558. <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a> to look up a converter function using
  559. the declared types for each column.
  560. The types are declared when the database table is created.
  561. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> will look up a converter function using the first word of the
  562. declared type as the converter dictionary key.
  563. For example:</p>
  564. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">test</span><span class="p">(</span>
  565. <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="nb">integer</span><span class="w"> </span><span class="k">primary</span><span class="w"> </span><span class="k">key</span><span class="p">,</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">look</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">converter</span><span class="w"> </span><span class="n">named</span><span class="w"> </span><span class="ss">&quot;integer&quot;</span>
  566. <span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="n">point</span><span class="p">,</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">look</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">converter</span><span class="w"> </span><span class="n">named</span><span class="w"> </span><span class="ss">&quot;point&quot;</span>
  567. <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="nb">number</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="o">!</span><span class="w"> </span><span class="n">will</span><span class="w"> </span><span class="n">look</span><span class="w"> </span><span class="n">up</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">converter</span><span class="w"> </span><span class="n">named</span><span class="w"> </span><span class="ss">&quot;number&quot;</span>
  568. <span class="w"> </span><span class="p">)</span>
  569. </pre></div>
  570. </div>
  571. <p>This flag may be combined with <a class="reference internal" href="#sqlite3.PARSE_COLNAMES" title="sqlite3.PARSE_COLNAMES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_COLNAMES</span></code></a> using the <code class="docutils literal notranslate"><span class="pre">|</span></code>
  572. (bitwise or) operator.</p>
  573. </dd></dl>
  574. <dl class="py data">
  575. <dt class="sig sig-object py" id="sqlite3.SQLITE_OK">
  576. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_OK</span></span><a class="headerlink" href="#sqlite3.SQLITE_OK" title="Link to this definition">¶</a></dt>
  577. <dt class="sig sig-object py" id="sqlite3.SQLITE_DENY">
  578. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DENY</span></span><a class="headerlink" href="#sqlite3.SQLITE_DENY" title="Link to this definition">¶</a></dt>
  579. <dt class="sig sig-object py" id="sqlite3.SQLITE_IGNORE">
  580. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_IGNORE</span></span><a class="headerlink" href="#sqlite3.SQLITE_IGNORE" title="Link to this definition">¶</a></dt>
  581. <dd><p>Flags that should be returned by the <em>authorizer_callback</em> <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a>
  582. passed to <a class="reference internal" href="#sqlite3.Connection.set_authorizer" title="sqlite3.Connection.set_authorizer"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.set_authorizer()</span></code></a>, to indicate whether:</p>
  583. <ul class="simple">
  584. <li><p>Access is allowed (<code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_OK</span></code>),</p></li>
  585. <li><p>The SQL statement should be aborted with an error (<code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_DENY</span></code>)</p></li>
  586. <li><p>The column should be treated as a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> value (<code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_IGNORE</span></code>)</p></li>
  587. </ul>
  588. </dd></dl>
  589. <dl class="py data">
  590. <dt class="sig sig-object py" id="sqlite3.apilevel">
  591. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">apilevel</span></span><a class="headerlink" href="#sqlite3.apilevel" title="Link to this definition">¶</a></dt>
  592. <dd><p>String constant stating the supported DB-API level. Required by the DB-API.
  593. Hard-coded to <code class="docutils literal notranslate"><span class="pre">&quot;2.0&quot;</span></code>.</p>
  594. </dd></dl>
  595. <dl class="py data">
  596. <dt class="sig sig-object py" id="sqlite3.paramstyle">
  597. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">paramstyle</span></span><a class="headerlink" href="#sqlite3.paramstyle" title="Link to this definition">¶</a></dt>
  598. <dd><p>String constant stating the type of parameter marker formatting expected by
  599. the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module. Required by the DB-API. Hard-coded to
  600. <code class="docutils literal notranslate"><span class="pre">&quot;qmark&quot;</span></code>.</p>
  601. <div class="admonition note">
  602. <p class="admonition-title">Note</p>
  603. <p>The <code class="docutils literal notranslate"><span class="pre">named</span></code> DB-API parameter style is also supported.</p>
  604. </div>
  605. </dd></dl>
  606. <dl class="py data">
  607. <dt class="sig sig-object py" id="sqlite3.sqlite_version">
  608. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">sqlite_version</span></span><a class="headerlink" href="#sqlite3.sqlite_version" title="Link to this definition">¶</a></dt>
  609. <dd><p>Version number of the runtime SQLite library as a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code></a>.</p>
  610. </dd></dl>
  611. <dl class="py data">
  612. <dt class="sig sig-object py" id="sqlite3.sqlite_version_info">
  613. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">sqlite_version_info</span></span><a class="headerlink" href="#sqlite3.sqlite_version_info" title="Link to this definition">¶</a></dt>
  614. <dd><p>Version number of the runtime SQLite library as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> of
  615. <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">integers</span></code></a>.</p>
  616. </dd></dl>
  617. <dl class="py data">
  618. <dt class="sig sig-object py" id="sqlite3.threadsafety">
  619. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">threadsafety</span></span><a class="headerlink" href="#sqlite3.threadsafety" title="Link to this definition">¶</a></dt>
  620. <dd><p>Integer constant required by the DB-API 2.0, stating the level of thread
  621. safety the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module supports. This attribute is set based on
  622. the default <a class="reference external" href="https://sqlite.org/threadsafe.html">threading mode</a> the
  623. underlying SQLite library is compiled with. The SQLite threading modes are:</p>
  624. <ol class="arabic simple">
  625. <li><p><strong>Single-thread</strong>: In this mode, all mutexes are disabled and SQLite is
  626. unsafe to use in more than a single thread at once.</p></li>
  627. <li><p><strong>Multi-thread</strong>: In this mode, SQLite can be safely used by multiple
  628. threads provided that no single database connection is used
  629. simultaneously in two or more threads.</p></li>
  630. <li><p><strong>Serialized</strong>: In serialized mode, SQLite can be safely used by
  631. multiple threads with no restriction.</p></li>
  632. </ol>
  633. <p>The mappings from SQLite threading modes to DB-API 2.0 threadsafety levels
  634. are as follows:</p>
  635. <table class="docutils align-default">
  636. <thead>
  637. <tr class="row-odd"><th class="head"><p>SQLite threading
  638. mode</p></th>
  639. <th class="head"><p><a class="reference external" href="https://peps.python.org/pep-0249/#threadsafety">threadsafety</a></p></th>
  640. <th class="head"><p><a class="reference external" href="https://sqlite.org/compile.html#threadsafe">SQLITE_THREADSAFE</a></p></th>
  641. <th class="head"><p>DB-API 2.0 meaning</p></th>
  642. </tr>
  643. </thead>
  644. <tbody>
  645. <tr class="row-even"><td><p>single-thread</p></td>
  646. <td><p>0</p></td>
  647. <td><p>0</p></td>
  648. <td><p>Threads may not share the
  649. module</p></td>
  650. </tr>
  651. <tr class="row-odd"><td><p>multi-thread</p></td>
  652. <td><p>1</p></td>
  653. <td><p>2</p></td>
  654. <td><p>Threads may share the module,
  655. but not connections</p></td>
  656. </tr>
  657. <tr class="row-even"><td><p>serialized</p></td>
  658. <td><p>3</p></td>
  659. <td><p>1</p></td>
  660. <td><p>Threads may share the module,
  661. connections and cursors</p></td>
  662. </tr>
  663. </tbody>
  664. </table>
  665. <div class="versionchanged">
  666. <p><span class="versionmodified changed">Changed in version 3.11: </span>Set <em>threadsafety</em> dynamically instead of hard-coding it to <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  667. </div>
  668. </dd></dl>
  669. <dl class="py data">
  670. <dt class="sig sig-object py" id="sqlite3.version">
  671. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#sqlite3.version" title="Link to this definition">¶</a></dt>
  672. <dd><p>Version number of this module as a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code></a>.
  673. This is not the version of the SQLite library.</p>
  674. <div class="deprecated-removed">
  675. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>This constant used to reflect the version number of the <code class="docutils literal notranslate"><span class="pre">pysqlite</span></code>
  676. package, a third-party library which used to upstream changes to
  677. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>. Today, it carries no meaning or practical value.</p>
  678. </div>
  679. </dd></dl>
  680. <dl class="py data">
  681. <dt class="sig sig-object py" id="sqlite3.version_info">
  682. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">version_info</span></span><a class="headerlink" href="#sqlite3.version_info" title="Link to this definition">¶</a></dt>
  683. <dd><p>Version number of this module as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> of <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">integers</span></code></a>.
  684. This is not the version of the SQLite library.</p>
  685. <div class="deprecated-removed">
  686. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>This constant used to reflect the version number of the <code class="docutils literal notranslate"><span class="pre">pysqlite</span></code>
  687. package, a third-party library which used to upstream changes to
  688. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>. Today, it carries no meaning or practical value.</p>
  689. </div>
  690. </dd></dl>
  691. <dl class="py data" id="sqlite3-dbconfig-constants">
  692. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_DEFENSIVE">
  693. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_DEFENSIVE</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_DEFENSIVE" title="Link to this definition">¶</a></dt>
  694. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_DQS_DDL">
  695. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_DQS_DDL</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_DQS_DDL" title="Link to this definition">¶</a></dt>
  696. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_DQS_DML">
  697. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_DQS_DML</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_DQS_DML" title="Link to this definition">¶</a></dt>
  698. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_FKEY">
  699. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_FKEY</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_FKEY" title="Link to this definition">¶</a></dt>
  700. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER">
  701. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER" title="Link to this definition">¶</a></dt>
  702. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION">
  703. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION" title="Link to this definition">¶</a></dt>
  704. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_QPSG">
  705. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_QPSG</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_QPSG" title="Link to this definition">¶</a></dt>
  706. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_TRIGGER">
  707. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_TRIGGER</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_TRIGGER" title="Link to this definition">¶</a></dt>
  708. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_ENABLE_VIEW">
  709. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_ENABLE_VIEW</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_ENABLE_VIEW" title="Link to this definition">¶</a></dt>
  710. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_LEGACY_ALTER_TABLE">
  711. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_LEGACY_ALTER_TABLE" title="Link to this definition">¶</a></dt>
  712. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_LEGACY_FILE_FORMAT">
  713. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_LEGACY_FILE_FORMAT" title="Link to this definition">¶</a></dt>
  714. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE">
  715. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE" title="Link to this definition">¶</a></dt>
  716. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_RESET_DATABASE">
  717. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_RESET_DATABASE</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_RESET_DATABASE" title="Link to this definition">¶</a></dt>
  718. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_TRIGGER_EQP">
  719. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_TRIGGER_EQP</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_TRIGGER_EQP" title="Link to this definition">¶</a></dt>
  720. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_TRUSTED_SCHEMA">
  721. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_TRUSTED_SCHEMA</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_TRUSTED_SCHEMA" title="Link to this definition">¶</a></dt>
  722. <dt class="sig sig-object py" id="sqlite3.SQLITE_DBCONFIG_WRITABLE_SCHEMA">
  723. <span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">SQLITE_DBCONFIG_WRITABLE_SCHEMA</span></span><a class="headerlink" href="#sqlite3.SQLITE_DBCONFIG_WRITABLE_SCHEMA" title="Link to this definition">¶</a></dt>
  724. <dd><p>These constants are used for the <a class="reference internal" href="#sqlite3.Connection.setconfig" title="sqlite3.Connection.setconfig"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.setconfig()</span></code></a>
  725. and <a class="reference internal" href="#sqlite3.Connection.getconfig" title="sqlite3.Connection.getconfig"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getconfig()</span></code></a> methods.</p>
  726. <p>The availability of these constants varies depending on the version of SQLite
  727. Python was compiled with.</p>
  728. <div class="versionadded">
  729. <p><span class="versionmodified added">New in version 3.12.</span></p>
  730. </div>
  731. <div class="admonition seealso">
  732. <p class="admonition-title">See also</p>
  733. <dl class="simple">
  734. <dt><a class="reference external" href="https://www.sqlite.org/c3ref/c_dbconfig_defensive.html">https://www.sqlite.org/c3ref/c_dbconfig_defensive.html</a></dt><dd><p>SQLite docs: Database Connection Configuration Options</p>
  735. </dd>
  736. </dl>
  737. </div>
  738. </dd></dl>
  739. </section>
  740. <section id="connection-objects">
  741. <span id="sqlite3-connection-objects"></span><h3>Connection objects<a class="headerlink" href="#connection-objects" title="Link to this heading">¶</a></h3>
  742. <dl class="py class">
  743. <dt class="sig sig-object py" id="sqlite3.Connection">
  744. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Connection</span></span><a class="headerlink" href="#sqlite3.Connection" title="Link to this definition">¶</a></dt>
  745. <dd><p>Each open SQLite database is represented by a <code class="docutils literal notranslate"><span class="pre">Connection</span></code> object,
  746. which is created using <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">sqlite3.connect()</span></code></a>.
  747. Their main purpose is creating <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> objects,
  748. and <a class="reference internal" href="#sqlite3-controlling-transactions"><span class="std std-ref">Transaction control</span></a>.</p>
  749. <div class="admonition seealso">
  750. <p class="admonition-title">See also</p>
  751. <ul class="simple">
  752. <li><p><a class="reference internal" href="#sqlite3-connection-shortcuts"><span class="std std-ref">How to use connection shortcut methods</span></a></p></li>
  753. <li><p><a class="reference internal" href="#sqlite3-connection-context-manager"><span class="std std-ref">How to use the connection context manager</span></a></p></li>
  754. </ul>
  755. </div>
  756. <p>An SQLite database connection has the following attributes and methods:</p>
  757. <dl class="py method">
  758. <dt class="sig sig-object py" id="sqlite3.Connection.cursor">
  759. <span class="sig-name descname"><span class="pre">cursor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">factory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Cursor</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.cursor" title="Link to this definition">¶</a></dt>
  760. <dd><p>Create and return a <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object.
  761. The cursor method accepts a single optional parameter <em>factory</em>. If
  762. supplied, this must be a <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> returning
  763. an instance of <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> or its subclasses.</p>
  764. </dd></dl>
  765. <dl class="py method">
  766. <dt class="sig sig-object py" id="sqlite3.Connection.blobopen">
  767. <span class="sig-name descname"><span class="pre">blobopen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">table</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">column</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">row</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">readonly</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'main'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.blobopen" title="Link to this definition">¶</a></dt>
  768. <dd><p>Open a <a class="reference internal" href="#sqlite3.Blob" title="sqlite3.Blob"><code class="xref py py-class docutils literal notranslate"><span class="pre">Blob</span></code></a> handle to an existing
  769. <abbr title="Binary Large OBject">BLOB</abbr>.</p>
  770. <dl class="field-list simple">
  771. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  772. <dd class="field-odd"><ul class="simple">
  773. <li><p><strong>table</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the table where the blob is located.</p></li>
  774. <li><p><strong>column</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the column where the blob is located.</p></li>
  775. <li><p><strong>row</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the row where the blob is located.</p></li>
  776. <li><p><strong>readonly</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – Set to <code class="docutils literal notranslate"><span class="pre">True</span></code> if the blob should be opened without write
  777. permissions.
  778. Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
  779. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the database where the blob is located.
  780. Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;main&quot;</span></code>.</p></li>
  781. </ul>
  782. </dd>
  783. <dt class="field-even">Raises<span class="colon">:</span></dt>
  784. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.OperationalError" title="sqlite3.OperationalError"><strong>OperationalError</strong></a> – When trying to open a blob in a <code class="docutils literal notranslate"><span class="pre">WITHOUT</span> <span class="pre">ROWID</span></code> table.</p>
  785. </dd>
  786. <dt class="field-odd">Return type<span class="colon">:</span></dt>
  787. <dd class="field-odd"><p><a class="reference internal" href="#sqlite3.Blob" title="sqlite3.Blob">Blob</a></p>
  788. </dd>
  789. </dl>
  790. <div class="admonition note">
  791. <p class="admonition-title">Note</p>
  792. <p>The blob size cannot be changed using the <a class="reference internal" href="#sqlite3.Blob" title="sqlite3.Blob"><code class="xref py py-class docutils literal notranslate"><span class="pre">Blob</span></code></a> class.
  793. Use the SQL function <code class="docutils literal notranslate"><span class="pre">zeroblob</span></code> to create a blob with a fixed size.</p>
  794. </div>
  795. <div class="versionadded">
  796. <p><span class="versionmodified added">New in version 3.11.</span></p>
  797. </div>
  798. </dd></dl>
  799. <dl class="py method">
  800. <dt class="sig sig-object py" id="sqlite3.Connection.commit">
  801. <span class="sig-name descname"><span class="pre">commit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.commit" title="Link to this definition">¶</a></dt>
  802. <dd><p>Commit any pending transaction to the database.
  803. If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, or there is no open transaction,
  804. this method does nothing.
  805. If <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, a new transaction is implicitly
  806. opened if a pending transaction was committed by this method.</p>
  807. </dd></dl>
  808. <dl class="py method">
  809. <dt class="sig sig-object py" id="sqlite3.Connection.rollback">
  810. <span class="sig-name descname"><span class="pre">rollback</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.rollback" title="Link to this definition">¶</a></dt>
  811. <dd><p>Roll back to the start of any pending transaction.
  812. If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, or there is no open transaction,
  813. this method does nothing.
  814. If <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, a new transaction is implicitly
  815. opened if a pending transaction was rolled back by this method.</p>
  816. </dd></dl>
  817. <dl class="py method">
  818. <dt class="sig sig-object py" id="sqlite3.Connection.close">
  819. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.close" title="Link to this definition">¶</a></dt>
  820. <dd><p>Close the database connection.
  821. If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  822. any pending transaction is implicitly rolled back.
  823. If <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> or <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>,
  824. no implicit transaction control is executed.
  825. Make sure to <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a> before closing
  826. to avoid losing pending changes.</p>
  827. </dd></dl>
  828. <dl class="py method">
  829. <dt class="sig sig-object py" id="sqlite3.Connection.execute">
  830. <span class="sig-name descname"><span class="pre">execute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.execute" title="Link to this definition">¶</a></dt>
  831. <dd><p>Create a new <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object and call
  832. <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> on it with the given <em>sql</em> and <em>parameters</em>.
  833. Return the new cursor object.</p>
  834. </dd></dl>
  835. <dl class="py method">
  836. <dt class="sig sig-object py" id="sqlite3.Connection.executemany">
  837. <span class="sig-name descname"><span class="pre">executemany</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameters</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.executemany" title="Link to this definition">¶</a></dt>
  838. <dd><p>Create a new <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object and call
  839. <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executemany()</span></code></a> on it with the given <em>sql</em> and <em>parameters</em>.
  840. Return the new cursor object.</p>
  841. </dd></dl>
  842. <dl class="py method">
  843. <dt class="sig sig-object py" id="sqlite3.Connection.executescript">
  844. <span class="sig-name descname"><span class="pre">executescript</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql_script</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.executescript" title="Link to this definition">¶</a></dt>
  845. <dd><p>Create a new <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object and call
  846. <a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executescript()</span></code></a> on it with the given <em>sql_script</em>.
  847. Return the new cursor object.</p>
  848. </dd></dl>
  849. <dl class="py method">
  850. <dt class="sig sig-object py" id="sqlite3.Connection.create_function">
  851. <span class="sig-name descname"><span class="pre">create_function</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">narg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">deterministic</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.create_function" title="Link to this definition">¶</a></dt>
  852. <dd><p>Create or remove a user-defined SQL function.</p>
  853. <dl class="field-list simple">
  854. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  855. <dd class="field-odd"><ul class="simple">
  856. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the SQL function.</p></li>
  857. <li><p><strong>narg</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The number of arguments the SQL function can accept.
  858. If <code class="docutils literal notranslate"><span class="pre">-1</span></code>, it may take any number of arguments.</p></li>
  859. <li><p><strong>func</strong> (<a class="reference internal" href="../glossary.html#term-callback"><span class="xref std std-term">callback</span></a> | None) – A <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> that is called when the SQL function is invoked.
  860. The callable must return <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">a type natively supported by SQLite</span></a>.
  861. Set to <code class="docutils literal notranslate"><span class="pre">None</span></code> to remove an existing SQL function.</p></li>
  862. <li><p><strong>deterministic</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – If <code class="docutils literal notranslate"><span class="pre">True</span></code>, the created SQL function is marked as
  863. <a class="reference external" href="https://sqlite.org/deterministic.html">deterministic</a>,
  864. which allows SQLite to perform additional optimizations.</p></li>
  865. </ul>
  866. </dd>
  867. <dt class="field-even">Raises<span class="colon">:</span></dt>
  868. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.NotSupportedError" title="sqlite3.NotSupportedError"><strong>NotSupportedError</strong></a> – If <em>deterministic</em> is used with SQLite versions older than 3.8.3.</p>
  869. </dd>
  870. </dl>
  871. <div class="versionchanged">
  872. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>deterministic</em> parameter.</p>
  873. </div>
  874. <p>Example:</p>
  875. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hashlib</span>
  876. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">md5sum</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
  877. <span class="gp">... </span> <span class="k">return</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  878. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  879. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">create_function</span><span class="p">(</span><span class="s2">&quot;md5&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">md5sum</span><span class="p">)</span>
  880. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT md5(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="sa">b</span><span class="s2">&quot;foo&quot;</span><span class="p">,)):</span>
  881. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  882. <span class="go">(&#39;acbd18db4cc2f85cedef654fccc4a4d8&#39;,)</span>
  883. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  884. </pre></div>
  885. </div>
  886. </dd></dl>
  887. <dl class="py method">
  888. <dt class="sig sig-object py" id="sqlite3.Connection.create_aggregate">
  889. <span class="sig-name descname"><span class="pre">create_aggregate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n_arg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">aggregate_class</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.create_aggregate" title="Link to this definition">¶</a></dt>
  890. <dd><p>Create or remove a user-defined SQL aggregate function.</p>
  891. <dl class="field-list simple">
  892. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  893. <dd class="field-odd"><ul class="simple">
  894. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the SQL aggregate function.</p></li>
  895. <li><p><strong>n_arg</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The number of arguments the SQL aggregate function can accept.
  896. If <code class="docutils literal notranslate"><span class="pre">-1</span></code>, it may take any number of arguments.</p></li>
  897. <li><p><strong>aggregate_class</strong> (<a class="reference internal" href="../glossary.html#term-class"><span class="xref std std-term">class</span></a> | None) – <p>A class must implement the following methods:</p>
  898. <ul>
  899. <li><p><code class="docutils literal notranslate"><span class="pre">step()</span></code>: Add a row to the aggregate.</p></li>
  900. <li><p><code class="docutils literal notranslate"><span class="pre">finalize()</span></code>: Return the final result of the aggregate as
  901. <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">a type natively supported by SQLite</span></a>.</p></li>
  902. </ul>
  903. <p>The number of arguments that the <code class="docutils literal notranslate"><span class="pre">step()</span></code> method must accept
  904. is controlled by <em>n_arg</em>.</p>
  905. <p>Set to <code class="docutils literal notranslate"><span class="pre">None</span></code> to remove an existing SQL aggregate function.</p>
  906. </p></li>
  907. </ul>
  908. </dd>
  909. </dl>
  910. <p>Example:</p>
  911. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MySum</span><span class="p">:</span>
  912. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  913. <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
  914. <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  915. <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">value</span>
  916. <span class="k">def</span> <span class="nf">finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  917. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>
  918. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  919. <span class="n">con</span><span class="o">.</span><span class="n">create_aggregate</span><span class="p">(</span><span class="s2">&quot;mysum&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MySum</span><span class="p">)</span>
  920. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(i)&quot;</span><span class="p">)</span>
  921. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(i) VALUES(1)&quot;</span><span class="p">)</span>
  922. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(i) VALUES(2)&quot;</span><span class="p">)</span>
  923. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT mysum(i) FROM test&quot;</span><span class="p">)</span>
  924. <span class="nb">print</span><span class="p">(</span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
  925. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  926. </pre></div>
  927. </div>
  928. </dd></dl>
  929. <dl class="py method">
  930. <dt class="sig sig-object py" id="sqlite3.Connection.create_window_function">
  931. <span class="sig-name descname"><span class="pre">create_window_function</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">num_params</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">aggregate_class</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.create_window_function" title="Link to this definition">¶</a></dt>
  932. <dd><p>Create or remove a user-defined aggregate window function.</p>
  933. <dl class="field-list simple">
  934. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  935. <dd class="field-odd"><ul class="simple">
  936. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the SQL aggregate window function to create or remove.</p></li>
  937. <li><p><strong>num_params</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The number of arguments the SQL aggregate window function can accept.
  938. If <code class="docutils literal notranslate"><span class="pre">-1</span></code>, it may take any number of arguments.</p></li>
  939. <li><p><strong>aggregate_class</strong> (<a class="reference internal" href="../glossary.html#term-class"><span class="xref std std-term">class</span></a> | None) – <p>A class that must implement the following methods:</p>
  940. <ul>
  941. <li><p><code class="docutils literal notranslate"><span class="pre">step()</span></code>: Add a row to the current window.</p></li>
  942. <li><p><code class="docutils literal notranslate"><span class="pre">value()</span></code>: Return the current value of the aggregate.</p></li>
  943. <li><p><code class="docutils literal notranslate"><span class="pre">inverse()</span></code>: Remove a row from the current window.</p></li>
  944. <li><p><code class="docutils literal notranslate"><span class="pre">finalize()</span></code>: Return the final result of the aggregate as
  945. <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">a type natively supported by SQLite</span></a>.</p></li>
  946. </ul>
  947. <p>The number of arguments that the <code class="docutils literal notranslate"><span class="pre">step()</span></code> and <code class="docutils literal notranslate"><span class="pre">value()</span></code> methods
  948. must accept is controlled by <em>num_params</em>.</p>
  949. <p>Set to <code class="docutils literal notranslate"><span class="pre">None</span></code> to remove an existing SQL aggregate window function.</p>
  950. </p></li>
  951. </ul>
  952. </dd>
  953. <dt class="field-even">Raises<span class="colon">:</span></dt>
  954. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.NotSupportedError" title="sqlite3.NotSupportedError"><strong>NotSupportedError</strong></a> – If used with a version of SQLite older than 3.25.0,
  955. which does not support aggregate window functions.</p>
  956. </dd>
  957. </dl>
  958. <div class="versionadded">
  959. <p><span class="versionmodified added">New in version 3.11.</span></p>
  960. </div>
  961. <p>Example:</p>
  962. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Example taken from https://www.sqlite.org/windowfunctions.html#udfwinfunc</span>
  963. <span class="k">class</span> <span class="nc">WindowSumInt</span><span class="p">:</span>
  964. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  965. <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
  966. <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  967. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Add a row to the current window.&quot;&quot;&quot;</span>
  968. <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">value</span>
  969. <span class="k">def</span> <span class="nf">value</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  970. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Return the current value of the aggregate.&quot;&quot;&quot;</span>
  971. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>
  972. <span class="k">def</span> <span class="nf">inverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  973. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Remove a row from the current window.&quot;&quot;&quot;</span>
  974. <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">-=</span> <span class="n">value</span>
  975. <span class="k">def</span> <span class="nf">finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  976. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Return the final value of the aggregate.</span>
  977. <span class="sd"> Any clean-up actions should be placed here.</span>
  978. <span class="sd"> &quot;&quot;&quot;</span>
  979. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>
  980. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  981. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(x, y)&quot;</span><span class="p">)</span>
  982. <span class="n">values</span> <span class="o">=</span> <span class="p">[</span>
  983. <span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
  984. <span class="p">(</span><span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span>
  985. <span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
  986. <span class="p">(</span><span class="s2">&quot;d&quot;</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span>
  987. <span class="p">(</span><span class="s2">&quot;e&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
  988. <span class="p">]</span>
  989. <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test VALUES(?, ?)&quot;</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
  990. <span class="n">con</span><span class="o">.</span><span class="n">create_window_function</span><span class="p">(</span><span class="s2">&quot;sumint&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">WindowSumInt</span><span class="p">)</span>
  991. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  992. <span class="s2"> SELECT x, sumint(y) OVER (</span>
  993. <span class="s2"> ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING</span>
  994. <span class="s2"> ) AS sum_y</span>
  995. <span class="s2"> FROM test ORDER BY x</span>
  996. <span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
  997. <span class="nb">print</span><span class="p">(</span><span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">())</span>
  998. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  999. </pre></div>
  1000. </div>
  1001. </dd></dl>
  1002. <dl class="py method">
  1003. <dt class="sig sig-object py" id="sqlite3.Connection.create_collation">
  1004. <span class="sig-name descname"><span class="pre">create_collation</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.create_collation" title="Link to this definition">¶</a></dt>
  1005. <dd><p>Create a collation named <em>name</em> using the collating function <em>callable</em>.
  1006. <em>callable</em> is passed two <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code></a> arguments,
  1007. and it should return an <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">integer</span></code></a>:</p>
  1008. <ul class="simple">
  1009. <li><p><code class="docutils literal notranslate"><span class="pre">1</span></code> if the first is ordered higher than the second</p></li>
  1010. <li><p><code class="docutils literal notranslate"><span class="pre">-1</span></code> if the first is ordered lower than the second</p></li>
  1011. <li><p><code class="docutils literal notranslate"><span class="pre">0</span></code> if they are ordered equal</p></li>
  1012. </ul>
  1013. <p>The following example shows a reverse sorting collation:</p>
  1014. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">collate_reverse</span><span class="p">(</span><span class="n">string1</span><span class="p">,</span> <span class="n">string2</span><span class="p">):</span>
  1015. <span class="k">if</span> <span class="n">string1</span> <span class="o">==</span> <span class="n">string2</span><span class="p">:</span>
  1016. <span class="k">return</span> <span class="mi">0</span>
  1017. <span class="k">elif</span> <span class="n">string1</span> <span class="o">&lt;</span> <span class="n">string2</span><span class="p">:</span>
  1018. <span class="k">return</span> <span class="mi">1</span>
  1019. <span class="k">else</span><span class="p">:</span>
  1020. <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
  1021. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  1022. <span class="n">con</span><span class="o">.</span><span class="n">create_collation</span><span class="p">(</span><span class="s2">&quot;reverse&quot;</span><span class="p">,</span> <span class="n">collate_reverse</span><span class="p">)</span>
  1023. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(x)&quot;</span><span class="p">)</span>
  1024. <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(x) VALUES(?)&quot;</span><span class="p">,</span> <span class="p">[(</span><span class="s2">&quot;a&quot;</span><span class="p">,),</span> <span class="p">(</span><span class="s2">&quot;b&quot;</span><span class="p">,)])</span>
  1025. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT x FROM test ORDER BY x COLLATE reverse&quot;</span><span class="p">)</span>
  1026. <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="p">:</span>
  1027. <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  1028. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1029. </pre></div>
  1030. </div>
  1031. <p>Remove a collation function by setting <em>callable</em> to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1032. <div class="versionchanged">
  1033. <p><span class="versionmodified changed">Changed in version 3.11: </span>The collation name can contain any Unicode character. Earlier, only
  1034. ASCII characters were allowed.</p>
  1035. </div>
  1036. </dd></dl>
  1037. <dl class="py method">
  1038. <dt class="sig sig-object py" id="sqlite3.Connection.interrupt">
  1039. <span class="sig-name descname"><span class="pre">interrupt</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.interrupt" title="Link to this definition">¶</a></dt>
  1040. <dd><p>Call this method from a different thread to abort any queries that might
  1041. be executing on the connection.
  1042. Aborted queries will raise an <a class="reference internal" href="#sqlite3.OperationalError" title="sqlite3.OperationalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OperationalError</span></code></a>.</p>
  1043. </dd></dl>
  1044. <dl class="py method">
  1045. <dt class="sig sig-object py" id="sqlite3.Connection.set_authorizer">
  1046. <span class="sig-name descname"><span class="pre">set_authorizer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">authorizer_callback</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.set_authorizer" title="Link to this definition">¶</a></dt>
  1047. <dd><p>Register <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> <em>authorizer_callback</em> to be invoked
  1048. for each attempt to access a column of a table in the database.
  1049. The callback should return one of <a class="reference internal" href="#sqlite3.SQLITE_OK" title="sqlite3.SQLITE_OK"><code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_OK</span></code></a>,
  1050. <a class="reference internal" href="#sqlite3.SQLITE_DENY" title="sqlite3.SQLITE_DENY"><code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_DENY</span></code></a>, or <a class="reference internal" href="#sqlite3.SQLITE_IGNORE" title="sqlite3.SQLITE_IGNORE"><code class="xref py py-const docutils literal notranslate"><span class="pre">SQLITE_IGNORE</span></code></a>
  1051. to signal how access to the column should be handled
  1052. by the underlying SQLite library.</p>
  1053. <p>The first argument to the callback signifies what kind of operation is to be
  1054. authorized. The second and third argument will be arguments or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  1055. depending on the first argument. The 4th argument is the name of the database
  1056. (“main”, “temp”, etc.) if applicable. The 5th argument is the name of the
  1057. inner-most trigger or view that is responsible for the access attempt or
  1058. <code class="docutils literal notranslate"><span class="pre">None</span></code> if this access attempt is directly from input SQL code.</p>
  1059. <p>Please consult the SQLite documentation about the possible values for the first
  1060. argument and the meaning of the second and third argument depending on the first
  1061. one. All necessary constants are available in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module.</p>
  1062. <p>Passing <code class="docutils literal notranslate"><span class="pre">None</span></code> as <em>authorizer_callback</em> will disable the authorizer.</p>
  1063. <div class="versionchanged">
  1064. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added support for disabling the authorizer using <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1065. </div>
  1066. </dd></dl>
  1067. <dl class="py method">
  1068. <dt class="sig sig-object py" id="sqlite3.Connection.set_progress_handler">
  1069. <span class="sig-name descname"><span class="pre">set_progress_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">progress_handler</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.set_progress_handler" title="Link to this definition">¶</a></dt>
  1070. <dd><p>Register <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> <em>progress_handler</em> to be invoked for every <em>n</em>
  1071. instructions of the SQLite virtual machine. This is useful if you want to
  1072. get called from SQLite during long-running operations, for example to update
  1073. a GUI.</p>
  1074. <p>If you want to clear any previously installed progress handler, call the
  1075. method with <code class="docutils literal notranslate"><span class="pre">None</span></code> for <em>progress_handler</em>.</p>
  1076. <p>Returning a non-zero value from the handler function will terminate the
  1077. currently executing query and cause it to raise a <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>
  1078. exception.</p>
  1079. </dd></dl>
  1080. <dl class="py method">
  1081. <dt class="sig sig-object py" id="sqlite3.Connection.set_trace_callback">
  1082. <span class="sig-name descname"><span class="pre">set_trace_callback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">trace_callback</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.set_trace_callback" title="Link to this definition">¶</a></dt>
  1083. <dd><p>Register <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> <em>trace_callback</em> to be invoked
  1084. for each SQL statement that is actually executed by the SQLite backend.</p>
  1085. <p>The only argument passed to the callback is the statement (as
  1086. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>) that is being executed. The return value of the callback is
  1087. ignored. Note that the backend does not only run statements passed to the
  1088. <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Cursor.execute()</span></code></a> methods. Other sources include the
  1089. <a class="reference internal" href="#sqlite3-controlling-transactions"><span class="std std-ref">transaction management</span></a> of the
  1090. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module and the execution of triggers defined in the current
  1091. database.</p>
  1092. <p>Passing <code class="docutils literal notranslate"><span class="pre">None</span></code> as <em>trace_callback</em> will disable the trace callback.</p>
  1093. <div class="admonition note">
  1094. <p class="admonition-title">Note</p>
  1095. <p>Exceptions raised in the trace callback are not propagated. As a
  1096. development and debugging aid, use
  1097. <a class="reference internal" href="#sqlite3.enable_callback_tracebacks" title="sqlite3.enable_callback_tracebacks"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable_callback_tracebacks()</span></code></a> to enable printing
  1098. tracebacks from exceptions raised in the trace callback.</p>
  1099. </div>
  1100. <div class="versionadded">
  1101. <p><span class="versionmodified added">New in version 3.3.</span></p>
  1102. </div>
  1103. </dd></dl>
  1104. <dl class="py method">
  1105. <dt class="sig sig-object py" id="sqlite3.Connection.enable_load_extension">
  1106. <span class="sig-name descname"><span class="pre">enable_load_extension</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">enabled</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.enable_load_extension" title="Link to this definition">¶</a></dt>
  1107. <dd><p>Enable the SQLite engine to load SQLite extensions from shared libraries
  1108. if <em>enabled</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>;
  1109. else, disallow loading SQLite extensions.
  1110. SQLite extensions can define new functions,
  1111. aggregates or whole new virtual table implementations. One well-known
  1112. extension is the fulltext-search extension distributed with SQLite.</p>
  1113. <div class="admonition note">
  1114. <p class="admonition-title">Note</p>
  1115. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module is not built with loadable extension support by
  1116. default, because some platforms (notably macOS) have SQLite
  1117. libraries which are compiled without this feature.
  1118. To get loadable extension support,
  1119. you must pass the <a class="reference internal" href="../using/configure.html#cmdoption-enable-loadable-sqlite-extensions"><code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-loadable-sqlite-extensions</span></code></a> option
  1120. to <strong class="program">configure</strong>.</p>
  1121. </div>
  1122. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">sqlite3.enable_load_extension</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">connection</span></code>, <code class="docutils literal notranslate"><span class="pre">enabled</span></code>.</p>
  1123. <div class="versionadded">
  1124. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1125. </div>
  1126. <div class="versionchanged">
  1127. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="docutils literal notranslate"><span class="pre">sqlite3.enable_load_extension</span></code> auditing event.</p>
  1128. </div>
  1129. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">con</span><span class="o">.</span><span class="n">enable_load_extension</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
  1130. <span class="c1"># Load the fulltext search extension</span>
  1131. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;select load_extension(&#39;./fts3.so&#39;)&quot;</span><span class="p">)</span>
  1132. <span class="c1"># alternatively you can load the extension using an API call:</span>
  1133. <span class="c1"># con.load_extension(&quot;./fts3.so&quot;)</span>
  1134. <span class="c1"># disable extension loading again</span>
  1135. <span class="n">con</span><span class="o">.</span><span class="n">enable_load_extension</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
  1136. <span class="c1"># example from SQLite wiki</span>
  1137. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE VIRTUAL TABLE recipe USING fts3(name, ingredients)&quot;</span><span class="p">)</span>
  1138. <span class="n">con</span><span class="o">.</span><span class="n">executescript</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  1139. <span class="s2"> INSERT INTO recipe (name, ingredients) VALUES(&#39;broccoli stew&#39;, &#39;broccoli peppers cheese tomatoes&#39;);</span>
  1140. <span class="s2"> INSERT INTO recipe (name, ingredients) VALUES(&#39;pumpkin stew&#39;, &#39;pumpkin onions garlic celery&#39;);</span>
  1141. <span class="s2"> INSERT INTO recipe (name, ingredients) VALUES(&#39;broccoli pie&#39;, &#39;broccoli cheese onions flour&#39;);</span>
  1142. <span class="s2"> INSERT INTO recipe (name, ingredients) VALUES(&#39;pumpkin pie&#39;, &#39;pumpkin sugar flour butter&#39;);</span>
  1143. <span class="s2"> &quot;&quot;&quot;</span><span class="p">)</span>
  1144. <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT rowid, name, ingredients FROM recipe WHERE name MATCH &#39;pie&#39;&quot;</span><span class="p">):</span>
  1145. <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  1146. </pre></div>
  1147. </div>
  1148. </dd></dl>
  1149. <dl class="py method">
  1150. <dt class="sig sig-object py" id="sqlite3.Connection.load_extension">
  1151. <span class="sig-name descname"><span class="pre">load_extension</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">entrypoint</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.load_extension" title="Link to this definition">¶</a></dt>
  1152. <dd><p>Load an SQLite extension from a shared library.
  1153. Enable extension loading with <a class="reference internal" href="#sqlite3.Connection.enable_load_extension" title="sqlite3.Connection.enable_load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable_load_extension()</span></code></a> before
  1154. calling this method.</p>
  1155. <dl class="field-list simple">
  1156. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1157. <dd class="field-odd"><ul class="simple">
  1158. <li><p><strong>path</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The path to the SQLite extension.</p></li>
  1159. <li><p><strong>entrypoint</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a><em> | </em><em>None</em>) – Entry point name.
  1160. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default),
  1161. SQLite will come up with an entry point name of its own;
  1162. see the SQLite docs <a class="reference external" href="https://www.sqlite.org/loadext.html#loading_an_extension">Loading an Extension</a> for details.</p></li>
  1163. </ul>
  1164. </dd>
  1165. </dl>
  1166. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">sqlite3.load_extension</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">connection</span></code>, <code class="docutils literal notranslate"><span class="pre">path</span></code>.</p>
  1167. <div class="versionadded">
  1168. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1169. </div>
  1170. <div class="versionchanged">
  1171. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <code class="docutils literal notranslate"><span class="pre">sqlite3.load_extension</span></code> auditing event.</p>
  1172. </div>
  1173. <div class="versionchanged">
  1174. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>entrypoint</em> parameter.</p>
  1175. </div>
  1176. </dd></dl>
  1177. <dl class="py method">
  1178. <dt class="sig sig-object py" id="sqlite3.Connection.iterdump">
  1179. <span class="sig-name descname"><span class="pre">iterdump</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.iterdump" title="Link to this definition">¶</a></dt>
  1180. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> to dump the database as SQL source code.
  1181. Useful when saving an in-memory database for later restoration.
  1182. Similar to the <code class="docutils literal notranslate"><span class="pre">.dump</span></code> command in the <strong class="program">sqlite3</strong> shell.</p>
  1183. <p>Example:</p>
  1184. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Convert file example.db to SQL dump file dump.sql</span>
  1185. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;example.db&#39;</span><span class="p">)</span>
  1186. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;dump.sql&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1187. <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">iterdump</span><span class="p">():</span>
  1188. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>
  1189. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1190. </pre></div>
  1191. </div>
  1192. <div class="admonition seealso">
  1193. <p class="admonition-title">See also</p>
  1194. <p><a class="reference internal" href="#sqlite3-howto-encoding"><span class="std std-ref">How to handle non-UTF-8 text encodings</span></a></p>
  1195. </div>
  1196. </dd></dl>
  1197. <dl class="py method">
  1198. <dt class="sig sig-object py" id="sqlite3.Connection.backup">
  1199. <span class="sig-name descname"><span class="pre">backup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pages</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">progress</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'main'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sleep</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0.250</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.backup" title="Link to this definition">¶</a></dt>
  1200. <dd><p>Create a backup of an SQLite database.</p>
  1201. <p>Works even if the database is being accessed by other clients
  1202. or concurrently by the same connection.</p>
  1203. <dl class="field-list simple">
  1204. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1205. <dd class="field-odd"><ul class="simple">
  1206. <li><p><strong>target</strong> (<a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><em>Connection</em></a>) – The database connection to save the backup to.</p></li>
  1207. <li><p><strong>pages</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The number of pages to copy at a time.
  1208. If equal to or less than <code class="docutils literal notranslate"><span class="pre">0</span></code>,
  1209. the entire database is copied in a single step.
  1210. Defaults to <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p></li>
  1211. <li><p><strong>progress</strong> (<a class="reference internal" href="../glossary.html#term-callback"><span class="xref std std-term">callback</span></a> | None) – If set to a <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a>,
  1212. it is invoked with three integer arguments for every backup iteration:
  1213. the <em>status</em> of the last iteration,
  1214. the <em>remaining</em> number of pages still to be copied,
  1215. and the <em>total</em> number of pages.
  1216. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
  1217. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The name of the database to back up.
  1218. Either <code class="docutils literal notranslate"><span class="pre">&quot;main&quot;</span></code> (the default) for the main database,
  1219. <code class="docutils literal notranslate"><span class="pre">&quot;temp&quot;</span></code> for the temporary database,
  1220. or the name of a custom database as attached using the
  1221. <code class="docutils literal notranslate"><span class="pre">ATTACH</span> <span class="pre">DATABASE</span></code> SQL statement.</p></li>
  1222. <li><p><strong>sleep</strong> (<a class="reference internal" href="functions.html#float" title="float"><em>float</em></a>) – The number of seconds to sleep between successive attempts
  1223. to back up remaining pages.</p></li>
  1224. </ul>
  1225. </dd>
  1226. </dl>
  1227. <p>Example 1, copy an existing database into another:</p>
  1228. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">progress</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">remaining</span><span class="p">,</span> <span class="n">total</span><span class="p">):</span>
  1229. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Copied </span><span class="si">{</span><span class="n">total</span><span class="o">-</span><span class="n">remaining</span><span class="si">}</span><span class="s1"> of </span><span class="si">{</span><span class="n">total</span><span class="si">}</span><span class="s1"> pages...&#39;</span><span class="p">)</span>
  1230. <span class="n">src</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;example.db&#39;</span><span class="p">)</span>
  1231. <span class="n">dst</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;backup.db&#39;</span><span class="p">)</span>
  1232. <span class="k">with</span> <span class="n">dst</span><span class="p">:</span>
  1233. <span class="n">src</span><span class="o">.</span><span class="n">backup</span><span class="p">(</span><span class="n">dst</span><span class="p">,</span> <span class="n">pages</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">progress</span><span class="o">=</span><span class="n">progress</span><span class="p">)</span>
  1234. <span class="n">dst</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1235. <span class="n">src</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1236. </pre></div>
  1237. </div>
  1238. <p>Example 2, copy an existing database into a transient copy:</p>
  1239. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">src</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;example.db&#39;</span><span class="p">)</span>
  1240. <span class="n">dst</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;:memory:&#39;</span><span class="p">)</span>
  1241. <span class="n">src</span><span class="o">.</span><span class="n">backup</span><span class="p">(</span><span class="n">dst</span><span class="p">)</span>
  1242. <span class="n">dst</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1243. <span class="n">src</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1244. </pre></div>
  1245. </div>
  1246. <div class="versionadded">
  1247. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1248. </div>
  1249. <div class="admonition seealso">
  1250. <p class="admonition-title">See also</p>
  1251. <p><a class="reference internal" href="#sqlite3-howto-encoding"><span class="std std-ref">How to handle non-UTF-8 text encodings</span></a></p>
  1252. </div>
  1253. </dd></dl>
  1254. <dl class="py method">
  1255. <dt class="sig sig-object py" id="sqlite3.Connection.getlimit">
  1256. <span class="sig-name descname"><span class="pre">getlimit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.getlimit" title="Link to this definition">¶</a></dt>
  1257. <dd><p>Get a connection runtime limit.</p>
  1258. <dl class="field-list simple">
  1259. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1260. <dd class="field-odd"><p><strong>category</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The <a class="reference external" href="https://www.sqlite.org/c3ref/c_limit_attached.html">SQLite limit category</a> to be queried.</p>
  1261. </dd>
  1262. <dt class="field-even">Return type<span class="colon">:</span></dt>
  1263. <dd class="field-even"><p><a class="reference internal" href="functions.html#int" title="int">int</a></p>
  1264. </dd>
  1265. <dt class="field-odd">Raises<span class="colon">:</span></dt>
  1266. <dd class="field-odd"><p><a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><strong>ProgrammingError</strong></a> – If <em>category</em> is not recognised by the underlying SQLite library.</p>
  1267. </dd>
  1268. </dl>
  1269. <p>Example, query the maximum length of an SQL statement
  1270. for <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> <code class="docutils literal notranslate"><span class="pre">con</span></code> (the default is 1000000000):</p>
  1271. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">getlimit</span><span class="p">(</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">SQLITE_LIMIT_SQL_LENGTH</span><span class="p">)</span>
  1272. <span class="go">1000000000</span>
  1273. </pre></div>
  1274. </div>
  1275. <div class="versionadded">
  1276. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1277. </div>
  1278. </dd></dl>
  1279. <dl class="py method">
  1280. <dt class="sig sig-object py" id="sqlite3.Connection.setlimit">
  1281. <span class="sig-name descname"><span class="pre">setlimit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.setlimit" title="Link to this definition">¶</a></dt>
  1282. <dd><p>Set a connection runtime limit.
  1283. Attempts to increase a limit above its hard upper bound are silently
  1284. truncated to the hard upper bound. Regardless of whether or not the limit
  1285. was changed, the prior value of the limit is returned.</p>
  1286. <dl class="field-list simple">
  1287. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1288. <dd class="field-odd"><ul class="simple">
  1289. <li><p><strong>category</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The <a class="reference external" href="https://www.sqlite.org/c3ref/c_limit_attached.html">SQLite limit category</a> to be set.</p></li>
  1290. <li><p><strong>limit</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – The value of the new limit.
  1291. If negative, the current limit is unchanged.</p></li>
  1292. </ul>
  1293. </dd>
  1294. <dt class="field-even">Return type<span class="colon">:</span></dt>
  1295. <dd class="field-even"><p><a class="reference internal" href="functions.html#int" title="int">int</a></p>
  1296. </dd>
  1297. <dt class="field-odd">Raises<span class="colon">:</span></dt>
  1298. <dd class="field-odd"><p><a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><strong>ProgrammingError</strong></a> – If <em>category</em> is not recognised by the underlying SQLite library.</p>
  1299. </dd>
  1300. </dl>
  1301. <p>Example, limit the number of attached databases to 1
  1302. for <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> <code class="docutils literal notranslate"><span class="pre">con</span></code> (the default limit is 10):</p>
  1303. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">setlimit</span><span class="p">(</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">SQLITE_LIMIT_ATTACHED</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  1304. <span class="go">10</span>
  1305. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">getlimit</span><span class="p">(</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">SQLITE_LIMIT_ATTACHED</span><span class="p">)</span>
  1306. <span class="go">1</span>
  1307. </pre></div>
  1308. </div>
  1309. <div class="versionadded">
  1310. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1311. </div>
  1312. </dd></dl>
  1313. <dl class="py method">
  1314. <dt class="sig sig-object py" id="sqlite3.Connection.getconfig">
  1315. <span class="sig-name descname"><span class="pre">getconfig</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">op</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.getconfig" title="Link to this definition">¶</a></dt>
  1316. <dd><p>Query a boolean connection configuration option.</p>
  1317. <dl class="field-list simple">
  1318. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1319. <dd class="field-odd"><p><strong>op</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – A <a class="reference internal" href="#sqlite3-dbconfig-constants"><span class="std std-ref">SQLITE_DBCONFIG code</span></a>.</p>
  1320. </dd>
  1321. <dt class="field-even">Return type<span class="colon">:</span></dt>
  1322. <dd class="field-even"><p><a class="reference internal" href="functions.html#bool" title="bool">bool</a></p>
  1323. </dd>
  1324. </dl>
  1325. <div class="versionadded">
  1326. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1327. </div>
  1328. </dd></dl>
  1329. <dl class="py method">
  1330. <dt class="sig sig-object py" id="sqlite3.Connection.setconfig">
  1331. <span class="sig-name descname"><span class="pre">setconfig</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">op</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">enable</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.setconfig" title="Link to this definition">¶</a></dt>
  1332. <dd><p>Set a boolean connection configuration option.</p>
  1333. <dl class="field-list simple">
  1334. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1335. <dd class="field-odd"><ul class="simple">
  1336. <li><p><strong>op</strong> (<a class="reference internal" href="functions.html#int" title="int"><em>int</em></a>) – A <a class="reference internal" href="#sqlite3-dbconfig-constants"><span class="std std-ref">SQLITE_DBCONFIG code</span></a>.</p></li>
  1337. <li><p><strong>enable</strong> (<a class="reference internal" href="functions.html#bool" title="bool"><em>bool</em></a>) – <code class="docutils literal notranslate"><span class="pre">True</span></code> if the configuration option should be enabled (default);
  1338. <code class="docutils literal notranslate"><span class="pre">False</span></code> if it should be disabled.</p></li>
  1339. </ul>
  1340. </dd>
  1341. </dl>
  1342. <div class="versionadded">
  1343. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1344. </div>
  1345. </dd></dl>
  1346. <dl class="py method">
  1347. <dt class="sig sig-object py" id="sqlite3.Connection.serialize">
  1348. <span class="sig-name descname"><span class="pre">serialize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'main'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.serialize" title="Link to this definition">¶</a></dt>
  1349. <dd><p>Serialize a database into a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object. For an
  1350. ordinary on-disk database file, the serialization is just a copy of the
  1351. disk file. For an in-memory database or a “temp” database, the
  1352. serialization is the same sequence of bytes which would be written to
  1353. disk if that database were backed up to disk.</p>
  1354. <dl class="field-list simple">
  1355. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1356. <dd class="field-odd"><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The database name to be serialized.
  1357. Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;main&quot;</span></code>.</p>
  1358. </dd>
  1359. <dt class="field-even">Return type<span class="colon">:</span></dt>
  1360. <dd class="field-even"><p><a class="reference internal" href="stdtypes.html#bytes" title="bytes">bytes</a></p>
  1361. </dd>
  1362. </dl>
  1363. <div class="admonition note">
  1364. <p class="admonition-title">Note</p>
  1365. <p>This method is only available if the underlying SQLite library has the
  1366. serialize API.</p>
  1367. </div>
  1368. <div class="versionadded">
  1369. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1370. </div>
  1371. </dd></dl>
  1372. <dl class="py method">
  1373. <dt class="sig sig-object py" id="sqlite3.Connection.deserialize">
  1374. <span class="sig-name descname"><span class="pre">deserialize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'main'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Connection.deserialize" title="Link to this definition">¶</a></dt>
  1375. <dd><p>Deserialize a <a class="reference internal" href="#sqlite3.Connection.serialize" title="sqlite3.Connection.serialize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">serialized</span></code></a> database into a
  1376. <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.
  1377. This method causes the database connection to disconnect from database
  1378. <em>name</em>, and reopen <em>name</em> as an in-memory database based on the
  1379. serialization contained in <em>data</em>.</p>
  1380. <dl class="field-list simple">
  1381. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1382. <dd class="field-odd"><ul class="simple">
  1383. <li><p><strong>data</strong> (<a class="reference internal" href="stdtypes.html#bytes" title="bytes"><em>bytes</em></a>) – A serialized database.</p></li>
  1384. <li><p><strong>name</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – The database name to deserialize into.
  1385. Defaults to <code class="docutils literal notranslate"><span class="pre">&quot;main&quot;</span></code>.</p></li>
  1386. </ul>
  1387. </dd>
  1388. <dt class="field-even">Raises<span class="colon">:</span></dt>
  1389. <dd class="field-even"><ul class="simple">
  1390. <li><p><a class="reference internal" href="#sqlite3.OperationalError" title="sqlite3.OperationalError"><strong>OperationalError</strong></a> – If the database connection is currently involved in a read
  1391. transaction or a backup operation.</p></li>
  1392. <li><p><a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><strong>DatabaseError</strong></a> – If <em>data</em> does not contain a valid SQLite database.</p></li>
  1393. <li><p><a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><strong>OverflowError</strong></a> – If <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len(data)</span></code></a> is larger than <code class="docutils literal notranslate"><span class="pre">2**63</span> <span class="pre">-</span> <span class="pre">1</span></code>.</p></li>
  1394. </ul>
  1395. </dd>
  1396. </dl>
  1397. <div class="admonition note">
  1398. <p class="admonition-title">Note</p>
  1399. <p>This method is only available if the underlying SQLite library has the
  1400. deserialize API.</p>
  1401. </div>
  1402. <div class="versionadded">
  1403. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1404. </div>
  1405. </dd></dl>
  1406. <dl class="py attribute">
  1407. <dt class="sig sig-object py" id="sqlite3.Connection.autocommit">
  1408. <span class="sig-name descname"><span class="pre">autocommit</span></span><a class="headerlink" href="#sqlite3.Connection.autocommit" title="Link to this definition">¶</a></dt>
  1409. <dd><p>This attribute controls <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>-compliant transaction behaviour.
  1410. <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code> has three allowed values:</p>
  1411. <ul>
  1412. <li><p><code class="docutils literal notranslate"><span class="pre">False</span></code>: Select <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>-compliant transaction behaviour,
  1413. implying that <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> ensures a transaction is always open.
  1414. Use <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a> and <a class="reference internal" href="#sqlite3.Connection.rollback" title="sqlite3.Connection.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rollback()</span></code></a> to close transactions.</p>
  1415. <p>This is the recommended value of <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code>.</p>
  1416. </li>
  1417. <li><p><code class="docutils literal notranslate"><span class="pre">True</span></code>: Use SQLite’s <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions">autocommit mode</a>.
  1418. <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a> and <a class="reference internal" href="#sqlite3.Connection.rollback" title="sqlite3.Connection.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rollback()</span></code></a> have no effect in this mode.</p></li>
  1419. <li><p><a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>:
  1420. Pre-Python 3.12 (non-<span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>-compliant) transaction control.
  1421. See <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a> for more details.</p>
  1422. <p>This is currently the default value of <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code>.</p>
  1423. </li>
  1424. </ul>
  1425. <p>Changing <code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code> to <code class="docutils literal notranslate"><span class="pre">False</span></code> will open a new transaction,
  1426. and changing it to <code class="docutils literal notranslate"><span class="pre">True</span></code> will commit any pending transaction.</p>
  1427. <p>See <a class="reference internal" href="#sqlite3-transaction-control-autocommit"><span class="std std-ref">Transaction control via the autocommit attribute</span></a> for more details.</p>
  1428. <div class="admonition note">
  1429. <p class="admonition-title">Note</p>
  1430. <p>The <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a> attribute has no effect unless
  1431. <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>.</p>
  1432. </div>
  1433. <div class="versionadded">
  1434. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1435. </div>
  1436. </dd></dl>
  1437. <dl class="py attribute">
  1438. <dt class="sig sig-object py" id="sqlite3.Connection.in_transaction">
  1439. <span class="sig-name descname"><span class="pre">in_transaction</span></span><a class="headerlink" href="#sqlite3.Connection.in_transaction" title="Link to this definition">¶</a></dt>
  1440. <dd><p>This read-only attribute corresponds to the low-level SQLite
  1441. <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions">autocommit mode</a>.</p>
  1442. <p><code class="docutils literal notranslate"><span class="pre">True</span></code> if a transaction is active (there are uncommitted changes),
  1443. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  1444. <div class="versionadded">
  1445. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1446. </div>
  1447. </dd></dl>
  1448. <dl class="py attribute">
  1449. <dt class="sig sig-object py" id="sqlite3.Connection.isolation_level">
  1450. <span class="sig-name descname"><span class="pre">isolation_level</span></span><a class="headerlink" href="#sqlite3.Connection.isolation_level" title="Link to this definition">¶</a></dt>
  1451. <dd><p>Controls the <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">legacy transaction handling mode</span></a> of <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>.
  1452. If set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, transactions are never implicitly opened.
  1453. If set to one of <code class="docutils literal notranslate"><span class="pre">&quot;DEFERRED&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;IMMEDIATE&quot;</span></code>, or <code class="docutils literal notranslate"><span class="pre">&quot;EXCLUSIVE&quot;</span></code>,
  1454. corresponding to the underlying <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions">SQLite transaction behaviour</a>,
  1455. <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">implicit transaction management</span></a> is performed.</p>
  1456. <p>If not overridden by the <em>isolation_level</em> parameter of <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a>,
  1457. the default is <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code>, which is an alias for <code class="docutils literal notranslate"><span class="pre">&quot;DEFERRED&quot;</span></code>.</p>
  1458. <div class="admonition note">
  1459. <p class="admonition-title">Note</p>
  1460. <p>Using <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> to control transaction handling is
  1461. recommended over using <code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code>.
  1462. <code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code> has no effect unless <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is
  1463. set to <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a> (the default).</p>
  1464. </div>
  1465. </dd></dl>
  1466. <dl class="py attribute">
  1467. <dt class="sig sig-object py" id="sqlite3.Connection.row_factory">
  1468. <span class="sig-name descname"><span class="pre">row_factory</span></span><a class="headerlink" href="#sqlite3.Connection.row_factory" title="Link to this definition">¶</a></dt>
  1469. <dd><p>The initial <a class="reference internal" href="#sqlite3.Cursor.row_factory" title="sqlite3.Cursor.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code></a>
  1470. for <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> objects created from this connection.
  1471. Assigning to this attribute does not affect the <code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code>
  1472. of existing cursors belonging to this connection, only new ones.
  1473. Is <code class="docutils literal notranslate"><span class="pre">None</span></code> by default,
  1474. meaning each row is returned as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>.</p>
  1475. <p>See <a class="reference internal" href="#sqlite3-howto-row-factory"><span class="std std-ref">How to create and use row factories</span></a> for more details.</p>
  1476. </dd></dl>
  1477. <dl class="py attribute">
  1478. <dt class="sig sig-object py" id="sqlite3.Connection.text_factory">
  1479. <span class="sig-name descname"><span class="pre">text_factory</span></span><a class="headerlink" href="#sqlite3.Connection.text_factory" title="Link to this definition">¶</a></dt>
  1480. <dd><p>A <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> that accepts a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> parameter
  1481. and returns a text representation of it.
  1482. The callable is invoked for SQLite values with the <code class="docutils literal notranslate"><span class="pre">TEXT</span></code> data type.
  1483. By default, this attribute is set to <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p>
  1484. <p>See <a class="reference internal" href="#sqlite3-howto-encoding"><span class="std std-ref">How to handle non-UTF-8 text encodings</span></a> for more details.</p>
  1485. </dd></dl>
  1486. <dl class="py attribute">
  1487. <dt class="sig sig-object py" id="sqlite3.Connection.total_changes">
  1488. <span class="sig-name descname"><span class="pre">total_changes</span></span><a class="headerlink" href="#sqlite3.Connection.total_changes" title="Link to this definition">¶</a></dt>
  1489. <dd><p>Return the total number of database rows that have been modified, inserted, or
  1490. deleted since the database connection was opened.</p>
  1491. </dd></dl>
  1492. </dd></dl>
  1493. </section>
  1494. <section id="cursor-objects">
  1495. <span id="sqlite3-cursor-objects"></span><h3>Cursor objects<a class="headerlink" href="#cursor-objects" title="Link to this heading">¶</a></h3>
  1496. <blockquote>
  1497. <div><p>A <code class="docutils literal notranslate"><span class="pre">Cursor</span></code> object represents a <a class="reference external" href="https://en.wikipedia.org/wiki/Cursor_(databases)">database cursor</a>
  1498. which is used to execute SQL statements,
  1499. and manage the context of a fetch operation.
  1500. Cursors are created using <a class="reference internal" href="#sqlite3.Connection.cursor" title="sqlite3.Connection.cursor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.cursor()</span></code></a>,
  1501. or by using any of the <a class="reference internal" href="#sqlite3-connection-shortcuts"><span class="std std-ref">connection shortcut methods</span></a>.</p>
  1502. <p>Cursor objects are <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterators</span></a>,
  1503. meaning that if you <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> a <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> query,
  1504. you can simply iterate over the cursor to fetch the resulting rows:</p>
  1505. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT t FROM data&quot;</span><span class="p">):</span>
  1506. <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  1507. </pre></div>
  1508. </div>
  1509. </div></blockquote>
  1510. <dl class="py class">
  1511. <dt class="sig sig-object py" id="sqlite3.Cursor">
  1512. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Cursor</span></span><a class="headerlink" href="#sqlite3.Cursor" title="Link to this definition">¶</a></dt>
  1513. <dd><p>A <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> instance has the following attributes and methods.</p>
  1514. <dl class="py method" id="index-7">
  1515. <span id="index-6"></span><dt class="sig sig-object py" id="sqlite3.Cursor.execute">
  1516. <span class="sig-name descname"><span class="pre">execute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.execute" title="Link to this definition">¶</a></dt>
  1517. <dd><p>Execute a single SQL statement,
  1518. optionally binding Python values using
  1519. <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">placeholders</span></a>.</p>
  1520. <dl class="field-list simple">
  1521. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1522. <dd class="field-odd"><ul class="simple">
  1523. <li><p><strong>sql</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – A single SQL statement.</p></li>
  1524. <li><p><strong>parameters</strong> (<a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> | <a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequence</span></a>) – Python values to bind to placeholders in <em>sql</em>.
  1525. A <code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code> if named placeholders are used.
  1526. A <span class="xref std std-term">sequence</span> if unnamed placeholders are used.
  1527. See <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">How to use placeholders to bind values in SQL queries</span></a>.</p></li>
  1528. </ul>
  1529. </dd>
  1530. <dt class="field-even">Raises<span class="colon">:</span></dt>
  1531. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><strong>ProgrammingError</strong></a> – If <em>sql</em> contains more than one SQL statement.</p>
  1532. </dd>
  1533. </dl>
  1534. <p>If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is
  1535. <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>,
  1536. <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1537. <em>sql</em> is an <code class="docutils literal notranslate"><span class="pre">INSERT</span></code>, <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>, <code class="docutils literal notranslate"><span class="pre">DELETE</span></code>, or <code class="docutils literal notranslate"><span class="pre">REPLACE</span></code> statement,
  1538. and there is no open transaction,
  1539. a transaction is implicitly opened before executing <em>sql</em>.</p>
  1540. <div class="deprecated-removed">
  1541. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span><a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> is emitted if
  1542. <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">named placeholders</span></a> are used
  1543. and <em>parameters</em> is a sequence instead of a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>.
  1544. Starting with Python 3.14, <a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProgrammingError</span></code></a> will
  1545. be raised instead.</p>
  1546. </div>
  1547. <p>Use <a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executescript()</span></code></a> to execute multiple SQL statements.</p>
  1548. </dd></dl>
  1549. <dl class="py method">
  1550. <dt class="sig sig-object py" id="sqlite3.Cursor.executemany">
  1551. <span class="sig-name descname"><span class="pre">executemany</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parameters</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.executemany" title="Link to this definition">¶</a></dt>
  1552. <dd><p>For every item in <em>parameters</em>,
  1553. repeatedly execute the <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">parameterized</span></a>
  1554. <abbr title="Data Manipulation Language">DML</abbr> SQL statement <em>sql</em>.</p>
  1555. <p>Uses the same implicit transaction handling as <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a>.</p>
  1556. <dl class="field-list simple">
  1557. <dt class="field-odd">Parameters<span class="colon">:</span></dt>
  1558. <dd class="field-odd"><ul class="simple">
  1559. <li><p><strong>sql</strong> (<a class="reference internal" href="stdtypes.html#str" title="str"><em>str</em></a>) – A single SQL DML statement.</p></li>
  1560. <li><p><strong>parameters</strong> (<a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>) – An <span class="xref std std-term">iterable</span> of parameters to bind with
  1561. the placeholders in <em>sql</em>.
  1562. See <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">How to use placeholders to bind values in SQL queries</span></a>.</p></li>
  1563. </ul>
  1564. </dd>
  1565. <dt class="field-even">Raises<span class="colon">:</span></dt>
  1566. <dd class="field-even"><p><a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><strong>ProgrammingError</strong></a> – If <em>sql</em> contains more than one SQL statement,
  1567. or is not a DML statement.</p>
  1568. </dd>
  1569. </dl>
  1570. <p>Example:</p>
  1571. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">rows</span> <span class="o">=</span> <span class="p">[</span>
  1572. <span class="p">(</span><span class="s2">&quot;row1&quot;</span><span class="p">,),</span>
  1573. <span class="p">(</span><span class="s2">&quot;row2&quot;</span><span class="p">,),</span>
  1574. <span class="p">]</span>
  1575. <span class="c1"># cur is an sqlite3.Cursor object</span>
  1576. <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO data VALUES(?)&quot;</span><span class="p">,</span> <span class="n">rows</span><span class="p">)</span>
  1577. </pre></div>
  1578. </div>
  1579. <div class="admonition note">
  1580. <p class="admonition-title">Note</p>
  1581. <p>Any resulting rows are discarded,
  1582. including DML statements with <a class="reference external" href="https://www.sqlite.org/lang_returning.html">RETURNING clauses</a>.</p>
  1583. </div>
  1584. <div class="deprecated-removed">
  1585. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span><a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> is emitted if
  1586. <a class="reference internal" href="#sqlite3-placeholders"><span class="std std-ref">named placeholders</span></a> are used
  1587. and the items in <em>parameters</em> are sequences
  1588. instead of <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>s.
  1589. Starting with Python 3.14, <a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProgrammingError</span></code></a> will
  1590. be raised instead.</p>
  1591. </div>
  1592. </dd></dl>
  1593. <dl class="py method">
  1594. <dt class="sig sig-object py" id="sqlite3.Cursor.executescript">
  1595. <span class="sig-name descname"><span class="pre">executescript</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sql_script</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.executescript" title="Link to this definition">¶</a></dt>
  1596. <dd><p>Execute the SQL statements in <em>sql_script</em>.
  1597. If the <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is
  1598. <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>
  1599. and there is a pending transaction,
  1600. an implicit <code class="docutils literal notranslate"><span class="pre">COMMIT</span></code> statement is executed first.
  1601. No other implicit transaction control is performed;
  1602. any transaction control must be added to <em>sql_script</em>.</p>
  1603. <p><em>sql_script</em> must be a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code></a>.</p>
  1604. <p>Example:</p>
  1605. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># cur is an sqlite3.Cursor object</span>
  1606. <span class="n">cur</span><span class="o">.</span><span class="n">executescript</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  1607. <span class="s2"> BEGIN;</span>
  1608. <span class="s2"> CREATE TABLE person(firstname, lastname, age);</span>
  1609. <span class="s2"> CREATE TABLE book(title, author, published);</span>
  1610. <span class="s2"> CREATE TABLE publisher(name, address);</span>
  1611. <span class="s2"> COMMIT;</span>
  1612. <span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
  1613. </pre></div>
  1614. </div>
  1615. </dd></dl>
  1616. <dl class="py method">
  1617. <dt class="sig sig-object py" id="sqlite3.Cursor.fetchone">
  1618. <span class="sig-name descname"><span class="pre">fetchone</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.fetchone" title="Link to this definition">¶</a></dt>
  1619. <dd><p>If <a class="reference internal" href="#sqlite3.Cursor.row_factory" title="sqlite3.Cursor.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1620. return the next row query result set as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>.
  1621. Else, pass it to the row factory and return its result.
  1622. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if no more data is available.</p>
  1623. </dd></dl>
  1624. <dl class="py method">
  1625. <dt class="sig sig-object py" id="sqlite3.Cursor.fetchmany">
  1626. <span class="sig-name descname"><span class="pre">fetchmany</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">cursor.arraysize</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.fetchmany" title="Link to this definition">¶</a></dt>
  1627. <dd><p>Return the next set of rows of a query result as a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>.
  1628. Return an empty list if no more rows are available.</p>
  1629. <p>The number of rows to fetch per call is specified by the <em>size</em> parameter.
  1630. If <em>size</em> is not given, <a class="reference internal" href="#sqlite3.Cursor.arraysize" title="sqlite3.Cursor.arraysize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arraysize</span></code></a> determines the number of rows
  1631. to be fetched.
  1632. If fewer than <em>size</em> rows are available,
  1633. as many rows as are available are returned.</p>
  1634. <p>Note there are performance considerations involved with the <em>size</em> parameter.
  1635. For optimal performance, it is usually best to use the arraysize attribute.
  1636. If the <em>size</em> parameter is used, then it is best for it to retain the same
  1637. value from one <a class="reference internal" href="#sqlite3.Cursor.fetchmany" title="sqlite3.Cursor.fetchmany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fetchmany()</span></code></a> call to the next.</p>
  1638. </dd></dl>
  1639. <dl class="py method">
  1640. <dt class="sig sig-object py" id="sqlite3.Cursor.fetchall">
  1641. <span class="sig-name descname"><span class="pre">fetchall</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.fetchall" title="Link to this definition">¶</a></dt>
  1642. <dd><p>Return all (remaining) rows of a query result as a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>.
  1643. Return an empty list if no rows are available.
  1644. Note that the <a class="reference internal" href="#sqlite3.Cursor.arraysize" title="sqlite3.Cursor.arraysize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arraysize</span></code></a> attribute can affect the performance of
  1645. this operation.</p>
  1646. </dd></dl>
  1647. <dl class="py method">
  1648. <dt class="sig sig-object py" id="sqlite3.Cursor.close">
  1649. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.close" title="Link to this definition">¶</a></dt>
  1650. <dd><p>Close the cursor now (rather than whenever <code class="docutils literal notranslate"><span class="pre">__del__</span></code> is called).</p>
  1651. <p>The cursor will be unusable from this point forward; a <a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProgrammingError</span></code></a>
  1652. exception will be raised if any operation is attempted with the cursor.</p>
  1653. </dd></dl>
  1654. <dl class="py method">
  1655. <dt class="sig sig-object py" id="sqlite3.Cursor.setinputsizes">
  1656. <span class="sig-name descname"><span class="pre">setinputsizes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sizes</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.setinputsizes" title="Link to this definition">¶</a></dt>
  1657. <dd><p>Required by the DB-API. Does nothing in <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>.</p>
  1658. </dd></dl>
  1659. <dl class="py method">
  1660. <dt class="sig sig-object py" id="sqlite3.Cursor.setoutputsize">
  1661. <span class="sig-name descname"><span class="pre">setoutputsize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">column</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Cursor.setoutputsize" title="Link to this definition">¶</a></dt>
  1662. <dd><p>Required by the DB-API. Does nothing in <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>.</p>
  1663. </dd></dl>
  1664. <dl class="py attribute">
  1665. <dt class="sig sig-object py" id="sqlite3.Cursor.arraysize">
  1666. <span class="sig-name descname"><span class="pre">arraysize</span></span><a class="headerlink" href="#sqlite3.Cursor.arraysize" title="Link to this definition">¶</a></dt>
  1667. <dd><p>Read/write attribute that controls the number of rows returned by <a class="reference internal" href="#sqlite3.Cursor.fetchmany" title="sqlite3.Cursor.fetchmany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fetchmany()</span></code></a>.
  1668. The default value is 1 which means a single row would be fetched per call.</p>
  1669. </dd></dl>
  1670. <dl class="py attribute">
  1671. <dt class="sig sig-object py" id="sqlite3.Cursor.connection">
  1672. <span class="sig-name descname"><span class="pre">connection</span></span><a class="headerlink" href="#sqlite3.Cursor.connection" title="Link to this definition">¶</a></dt>
  1673. <dd><p>Read-only attribute that provides the SQLite database <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>
  1674. belonging to the cursor. A <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object created by
  1675. calling <a class="reference internal" href="#sqlite3.Connection.cursor" title="sqlite3.Connection.cursor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">con.cursor()</span></code></a> will have a
  1676. <a class="reference internal" href="#sqlite3.Cursor.connection" title="sqlite3.Cursor.connection"><code class="xref py py-attr docutils literal notranslate"><span class="pre">connection</span></code></a> attribute that refers to <em>con</em>:</p>
  1677. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  1678. <span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  1679. <span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">connection</span> <span class="o">==</span> <span class="n">con</span>
  1680. <span class="go">True</span>
  1681. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1682. </pre></div>
  1683. </div>
  1684. </dd></dl>
  1685. <dl class="py attribute">
  1686. <dt class="sig sig-object py" id="sqlite3.Cursor.description">
  1687. <span class="sig-name descname"><span class="pre">description</span></span><a class="headerlink" href="#sqlite3.Cursor.description" title="Link to this definition">¶</a></dt>
  1688. <dd><p>Read-only attribute that provides the column names of the last query. To
  1689. remain compatible with the Python DB API, it returns a 7-tuple for each
  1690. column where the last six items of each tuple are <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1691. <p>It is set for <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statements without any matching rows as well.</p>
  1692. </dd></dl>
  1693. <dl class="py attribute">
  1694. <dt class="sig sig-object py" id="sqlite3.Cursor.lastrowid">
  1695. <span class="sig-name descname"><span class="pre">lastrowid</span></span><a class="headerlink" href="#sqlite3.Cursor.lastrowid" title="Link to this definition">¶</a></dt>
  1696. <dd><p>Read-only attribute that provides the row id of the last inserted row. It
  1697. is only updated after successful <code class="docutils literal notranslate"><span class="pre">INSERT</span></code> or <code class="docutils literal notranslate"><span class="pre">REPLACE</span></code> statements
  1698. using the <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> method. For other statements, after
  1699. <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executemany()</span></code></a> or <a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executescript()</span></code></a>, or if the insertion failed,
  1700. the value of <code class="docutils literal notranslate"><span class="pre">lastrowid</span></code> is left unchanged. The initial value of
  1701. <code class="docutils literal notranslate"><span class="pre">lastrowid</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1702. <div class="admonition note">
  1703. <p class="admonition-title">Note</p>
  1704. <p>Inserts into <code class="docutils literal notranslate"><span class="pre">WITHOUT</span> <span class="pre">ROWID</span></code> tables are not recorded.</p>
  1705. </div>
  1706. <div class="versionchanged">
  1707. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <code class="docutils literal notranslate"><span class="pre">REPLACE</span></code> statement.</p>
  1708. </div>
  1709. </dd></dl>
  1710. <dl class="py attribute">
  1711. <dt class="sig sig-object py" id="sqlite3.Cursor.rowcount">
  1712. <span class="sig-name descname"><span class="pre">rowcount</span></span><a class="headerlink" href="#sqlite3.Cursor.rowcount" title="Link to this definition">¶</a></dt>
  1713. <dd><p>Read-only attribute that provides the number of modified rows for
  1714. <code class="docutils literal notranslate"><span class="pre">INSERT</span></code>, <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>, <code class="docutils literal notranslate"><span class="pre">DELETE</span></code>, and <code class="docutils literal notranslate"><span class="pre">REPLACE</span></code> statements;
  1715. is <code class="docutils literal notranslate"><span class="pre">-1</span></code> for other statements,
  1716. including <abbr title="Common Table Expression">CTE</abbr> queries.
  1717. It is only updated by the <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> and <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executemany()</span></code></a> methods,
  1718. after the statement has run to completion.
  1719. This means that any resulting rows must be fetched in order for
  1720. <code class="xref py py-attr docutils literal notranslate"><span class="pre">rowcount</span></code> to be updated.</p>
  1721. </dd></dl>
  1722. <dl class="py attribute">
  1723. <dt class="sig sig-object py" id="sqlite3.Cursor.row_factory">
  1724. <span class="sig-name descname"><span class="pre">row_factory</span></span><a class="headerlink" href="#sqlite3.Cursor.row_factory" title="Link to this definition">¶</a></dt>
  1725. <dd><p>Control how a row fetched from this <code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code> is represented.
  1726. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, a row is represented as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>.
  1727. Can be set to the included <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlite3.Row</span></code></a>;
  1728. or a <a class="reference internal" href="../glossary.html#term-callable"><span class="xref std std-term">callable</span></a> that accepts two arguments,
  1729. a <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> object and the <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code> of row values,
  1730. and returns a custom object representing an SQLite row.</p>
  1731. <p>Defaults to what <a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.row_factory</span></code></a> was set to
  1732. when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code> was created.
  1733. Assigning to this attribute does not affect
  1734. <a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.row_factory</span></code></a> of the parent connection.</p>
  1735. <p>See <a class="reference internal" href="#sqlite3-howto-row-factory"><span class="std std-ref">How to create and use row factories</span></a> for more details.</p>
  1736. </dd></dl>
  1737. </dd></dl>
  1738. </section>
  1739. <section id="row-objects">
  1740. <span id="sqlite3-row-objects"></span><span id="sqlite3-columns-by-name"></span><h3>Row objects<a class="headerlink" href="#row-objects" title="Link to this heading">¶</a></h3>
  1741. <dl class="py class">
  1742. <dt class="sig sig-object py" id="sqlite3.Row">
  1743. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Row</span></span><a class="headerlink" href="#sqlite3.Row" title="Link to this definition">¶</a></dt>
  1744. <dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code> instance serves as a highly optimized
  1745. <a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code></a> for <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects.
  1746. It supports iteration, equality testing, <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a>,
  1747. and <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> access by column name and index.</p>
  1748. <p>Two <code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code> objects compare equal
  1749. if they have identical column names and values.</p>
  1750. <p>See <a class="reference internal" href="#sqlite3-howto-row-factory"><span class="std std-ref">How to create and use row factories</span></a> for more details.</p>
  1751. <dl class="py method">
  1752. <dt class="sig sig-object py" id="sqlite3.Row.keys">
  1753. <span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Row.keys" title="Link to this definition">¶</a></dt>
  1754. <dd><p>Return a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> of column names as <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">strings</span></code></a>.
  1755. Immediately after a query,
  1756. it is the first member of each tuple in <a class="reference internal" href="#sqlite3.Cursor.description" title="sqlite3.Cursor.description"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Cursor.description</span></code></a>.</p>
  1757. </dd></dl>
  1758. <div class="versionchanged">
  1759. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support of slicing.</p>
  1760. </div>
  1761. </dd></dl>
  1762. </section>
  1763. <section id="blob-objects">
  1764. <span id="sqlite3-blob-objects"></span><h3>Blob objects<a class="headerlink" href="#blob-objects" title="Link to this heading">¶</a></h3>
  1765. <dl class="py class">
  1766. <dt class="sig sig-object py" id="sqlite3.Blob">
  1767. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Blob</span></span><a class="headerlink" href="#sqlite3.Blob" title="Link to this definition">¶</a></dt>
  1768. <dd><div class="versionadded">
  1769. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1770. </div>
  1771. <p>A <a class="reference internal" href="#sqlite3.Blob" title="sqlite3.Blob"><code class="xref py py-class docutils literal notranslate"><span class="pre">Blob</span></code></a> instance is a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a>
  1772. that can read and write data in an SQLite <abbr title="Binary Large OBject">BLOB</abbr>.
  1773. Call <a class="reference internal" href="functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len(blob)</span></code></a> to get the size (number of bytes) of the blob.
  1774. Use indices and <a class="reference internal" href="../glossary.html#term-slice"><span class="xref std std-term">slices</span></a> for direct access to the blob data.</p>
  1775. <p>Use the <a class="reference internal" href="#sqlite3.Blob" title="sqlite3.Blob"><code class="xref py py-class docutils literal notranslate"><span class="pre">Blob</span></code></a> as a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> to ensure that the blob
  1776. handle is closed after use.</p>
  1777. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  1778. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(blob_col blob)&quot;</span><span class="p">)</span>
  1779. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(blob_col) VALUES(zeroblob(13))&quot;</span><span class="p">)</span>
  1780. <span class="c1"># Write to our blob, using two write operations:</span>
  1781. <span class="k">with</span> <span class="n">con</span><span class="o">.</span><span class="n">blobopen</span><span class="p">(</span><span class="s2">&quot;test&quot;</span><span class="p">,</span> <span class="s2">&quot;blob_col&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="k">as</span> <span class="n">blob</span><span class="p">:</span>
  1782. <span class="n">blob</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;hello, &quot;</span><span class="p">)</span>
  1783. <span class="n">blob</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;world.&quot;</span><span class="p">)</span>
  1784. <span class="c1"># Modify the first and last bytes of our blob</span>
  1785. <span class="n">blob</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;H&quot;</span><span class="p">)</span>
  1786. <span class="n">blob</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">ord</span><span class="p">(</span><span class="s2">&quot;!&quot;</span><span class="p">)</span>
  1787. <span class="c1"># Read the contents of our blob</span>
  1788. <span class="k">with</span> <span class="n">con</span><span class="o">.</span><span class="n">blobopen</span><span class="p">(</span><span class="s2">&quot;test&quot;</span><span class="p">,</span> <span class="s2">&quot;blob_col&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="k">as</span> <span class="n">blob</span><span class="p">:</span>
  1789. <span class="n">greeting</span> <span class="o">=</span> <span class="n">blob</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  1790. <span class="nb">print</span><span class="p">(</span><span class="n">greeting</span><span class="p">)</span> <span class="c1"># outputs &quot;b&#39;Hello, world!&#39;&quot;</span>
  1791. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1792. </pre></div>
  1793. </div>
  1794. <dl class="py method">
  1795. <dt class="sig sig-object py" id="sqlite3.Blob.close">
  1796. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Blob.close" title="Link to this definition">¶</a></dt>
  1797. <dd><p>Close the blob.</p>
  1798. <p>The blob will be unusable from this point onward. An
  1799. <a class="reference internal" href="#sqlite3.Error" title="sqlite3.Error"><code class="xref py py-class docutils literal notranslate"><span class="pre">Error</span></code></a> (or subclass) exception will be raised if any
  1800. further operation is attempted with the blob.</p>
  1801. </dd></dl>
  1802. <dl class="py method">
  1803. <dt class="sig sig-object py" id="sqlite3.Blob.read">
  1804. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">length</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Blob.read" title="Link to this definition">¶</a></dt>
  1805. <dd><p>Read <em>length</em> bytes of data from the blob at the current offset position.
  1806. If the end of the blob is reached, the data up to
  1807. <abbr title="End of File">EOF</abbr> will be returned. When <em>length</em> is not
  1808. specified, or is negative, <a class="reference internal" href="#sqlite3.Blob.read" title="sqlite3.Blob.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> will read until the end of
  1809. the blob.</p>
  1810. </dd></dl>
  1811. <dl class="py method">
  1812. <dt class="sig sig-object py" id="sqlite3.Blob.write">
  1813. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Blob.write" title="Link to this definition">¶</a></dt>
  1814. <dd><p>Write <em>data</em> to the blob at the current offset. This function cannot
  1815. change the blob length. Writing beyond the end of the blob will raise
  1816. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  1817. </dd></dl>
  1818. <dl class="py method">
  1819. <dt class="sig sig-object py" id="sqlite3.Blob.tell">
  1820. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Blob.tell" title="Link to this definition">¶</a></dt>
  1821. <dd><p>Return the current access position of the blob.</p>
  1822. </dd></dl>
  1823. <dl class="py method">
  1824. <dt class="sig sig-object py" id="sqlite3.Blob.seek">
  1825. <span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">offset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">origin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">os.SEEK_SET</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#sqlite3.Blob.seek" title="Link to this definition">¶</a></dt>
  1826. <dd><p>Set the current access position of the blob to <em>offset</em>. The <em>origin</em>
  1827. argument defaults to <a class="reference internal" href="os.html#os.SEEK_SET" title="os.SEEK_SET"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code></a> (absolute blob positioning).
  1828. Other values for <em>origin</em> are <a class="reference internal" href="os.html#os.SEEK_CUR" title="os.SEEK_CUR"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code></a> (seek relative to the
  1829. current position) and <a class="reference internal" href="os.html#os.SEEK_END" title="os.SEEK_END"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.SEEK_END</span></code></a> (seek relative to the blob’s
  1830. end).</p>
  1831. </dd></dl>
  1832. </dd></dl>
  1833. </section>
  1834. <section id="prepareprotocol-objects">
  1835. <h3>PrepareProtocol objects<a class="headerlink" href="#prepareprotocol-objects" title="Link to this heading">¶</a></h3>
  1836. <dl class="py class">
  1837. <dt class="sig sig-object py" id="sqlite3.PrepareProtocol">
  1838. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">PrepareProtocol</span></span><a class="headerlink" href="#sqlite3.PrepareProtocol" title="Link to this definition">¶</a></dt>
  1839. <dd><p>The PrepareProtocol type’s single purpose is to act as a <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0246/"><strong>PEP 246</strong></a> style
  1840. adaption protocol for objects that can <a class="reference internal" href="#sqlite3-conform"><span class="std std-ref">adapt themselves</span></a> to <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">native SQLite types</span></a>.</p>
  1841. </dd></dl>
  1842. </section>
  1843. <section id="exceptions">
  1844. <span id="sqlite3-exceptions"></span><h3>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h3>
  1845. <p>The exception hierarchy is defined by the DB-API 2.0 (<span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>).</p>
  1846. <dl class="py exception">
  1847. <dt class="sig sig-object py" id="sqlite3.Warning">
  1848. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Warning</span></span><a class="headerlink" href="#sqlite3.Warning" title="Link to this definition">¶</a></dt>
  1849. <dd><p>This exception is not currently raised by the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module,
  1850. but may be raised by applications using <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>,
  1851. for example if a user-defined function truncates data while inserting.
  1852. <code class="docutils literal notranslate"><span class="pre">Warning</span></code> is a subclass of <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p>
  1853. </dd></dl>
  1854. <dl class="py exception">
  1855. <dt class="sig sig-object py" id="sqlite3.Error">
  1856. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#sqlite3.Error" title="Link to this definition">¶</a></dt>
  1857. <dd><p>The base class of the other exceptions in this module.
  1858. Use this to catch all errors with one single <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> statement.
  1859. <code class="docutils literal notranslate"><span class="pre">Error</span></code> is a subclass of <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p>
  1860. <p>If the exception originated from within the SQLite library,
  1861. the following two attributes are added to the exception:</p>
  1862. <dl class="py attribute">
  1863. <dt class="sig sig-object py" id="sqlite3.Error.sqlite_errorcode">
  1864. <span class="sig-name descname"><span class="pre">sqlite_errorcode</span></span><a class="headerlink" href="#sqlite3.Error.sqlite_errorcode" title="Link to this definition">¶</a></dt>
  1865. <dd><p>The numeric error code from the
  1866. <a class="reference external" href="https://sqlite.org/rescode.html">SQLite API</a></p>
  1867. <div class="versionadded">
  1868. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1869. </div>
  1870. </dd></dl>
  1871. <dl class="py attribute">
  1872. <dt class="sig sig-object py" id="sqlite3.Error.sqlite_errorname">
  1873. <span class="sig-name descname"><span class="pre">sqlite_errorname</span></span><a class="headerlink" href="#sqlite3.Error.sqlite_errorname" title="Link to this definition">¶</a></dt>
  1874. <dd><p>The symbolic name of the numeric error code
  1875. from the <a class="reference external" href="https://sqlite.org/rescode.html">SQLite API</a></p>
  1876. <div class="versionadded">
  1877. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1878. </div>
  1879. </dd></dl>
  1880. </dd></dl>
  1881. <dl class="py exception">
  1882. <dt class="sig sig-object py" id="sqlite3.InterfaceError">
  1883. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">InterfaceError</span></span><a class="headerlink" href="#sqlite3.InterfaceError" title="Link to this definition">¶</a></dt>
  1884. <dd><p>Exception raised for misuse of the low-level SQLite C API.
  1885. In other words, if this exception is raised, it probably indicates a bug in the
  1886. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module.
  1887. <code class="docutils literal notranslate"><span class="pre">InterfaceError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.Error" title="sqlite3.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a>.</p>
  1888. </dd></dl>
  1889. <dl class="py exception">
  1890. <dt class="sig sig-object py" id="sqlite3.DatabaseError">
  1891. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">DatabaseError</span></span><a class="headerlink" href="#sqlite3.DatabaseError" title="Link to this definition">¶</a></dt>
  1892. <dd><p>Exception raised for errors that are related to the database.
  1893. This serves as the base exception for several types of database errors.
  1894. It is only raised implicitly through the specialised subclasses.
  1895. <code class="docutils literal notranslate"><span class="pre">DatabaseError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.Error" title="sqlite3.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a>.</p>
  1896. </dd></dl>
  1897. <dl class="py exception">
  1898. <dt class="sig sig-object py" id="sqlite3.DataError">
  1899. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">DataError</span></span><a class="headerlink" href="#sqlite3.DataError" title="Link to this definition">¶</a></dt>
  1900. <dd><p>Exception raised for errors caused by problems with the processed data,
  1901. like numeric values out of range, and strings which are too long.
  1902. <code class="docutils literal notranslate"><span class="pre">DataError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1903. </dd></dl>
  1904. <dl class="py exception">
  1905. <dt class="sig sig-object py" id="sqlite3.OperationalError">
  1906. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">OperationalError</span></span><a class="headerlink" href="#sqlite3.OperationalError" title="Link to this definition">¶</a></dt>
  1907. <dd><p>Exception raised for errors that are related to the database’s operation,
  1908. and not necessarily under the control of the programmer.
  1909. For example, the database path is not found,
  1910. or a transaction could not be processed.
  1911. <code class="docutils literal notranslate"><span class="pre">OperationalError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1912. </dd></dl>
  1913. <dl class="py exception">
  1914. <dt class="sig sig-object py" id="sqlite3.IntegrityError">
  1915. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">IntegrityError</span></span><a class="headerlink" href="#sqlite3.IntegrityError" title="Link to this definition">¶</a></dt>
  1916. <dd><p>Exception raised when the relational integrity of the database is affected,
  1917. e.g. a foreign key check fails. It is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1918. </dd></dl>
  1919. <dl class="py exception">
  1920. <dt class="sig sig-object py" id="sqlite3.InternalError">
  1921. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">InternalError</span></span><a class="headerlink" href="#sqlite3.InternalError" title="Link to this definition">¶</a></dt>
  1922. <dd><p>Exception raised when SQLite encounters an internal error.
  1923. If this is raised, it may indicate that there is a problem with the runtime
  1924. SQLite library.
  1925. <code class="docutils literal notranslate"><span class="pre">InternalError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1926. </dd></dl>
  1927. <dl class="py exception">
  1928. <dt class="sig sig-object py" id="sqlite3.ProgrammingError">
  1929. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">ProgrammingError</span></span><a class="headerlink" href="#sqlite3.ProgrammingError" title="Link to this definition">¶</a></dt>
  1930. <dd><p>Exception raised for <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> API programming errors,
  1931. for example supplying the wrong number of bindings to a query,
  1932. or trying to operate on a closed <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.
  1933. <code class="docutils literal notranslate"><span class="pre">ProgrammingError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1934. </dd></dl>
  1935. <dl class="py exception">
  1936. <dt class="sig sig-object py" id="sqlite3.NotSupportedError">
  1937. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">sqlite3.</span></span><span class="sig-name descname"><span class="pre">NotSupportedError</span></span><a class="headerlink" href="#sqlite3.NotSupportedError" title="Link to this definition">¶</a></dt>
  1938. <dd><p>Exception raised in case a method or database API is not supported by the
  1939. underlying SQLite library. For example, setting <em>deterministic</em> to
  1940. <code class="docutils literal notranslate"><span class="pre">True</span></code> in <a class="reference internal" href="#sqlite3.Connection.create_function" title="sqlite3.Connection.create_function"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_function()</span></code></a>, if the underlying SQLite library
  1941. does not support deterministic functions.
  1942. <code class="docutils literal notranslate"><span class="pre">NotSupportedError</span></code> is a subclass of <a class="reference internal" href="#sqlite3.DatabaseError" title="sqlite3.DatabaseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DatabaseError</span></code></a>.</p>
  1943. </dd></dl>
  1944. </section>
  1945. <section id="sqlite-and-python-types">
  1946. <span id="sqlite3-types"></span><h3>SQLite and Python types<a class="headerlink" href="#sqlite-and-python-types" title="Link to this heading">¶</a></h3>
  1947. <p>SQLite natively supports the following types: <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, <code class="docutils literal notranslate"><span class="pre">INTEGER</span></code>,
  1948. <code class="docutils literal notranslate"><span class="pre">REAL</span></code>, <code class="docutils literal notranslate"><span class="pre">TEXT</span></code>, <code class="docutils literal notranslate"><span class="pre">BLOB</span></code>.</p>
  1949. <p>The following Python types can thus be sent to SQLite without any problem:</p>
  1950. <table class="docutils align-default">
  1951. <thead>
  1952. <tr class="row-odd"><th class="head"><p>Python type</p></th>
  1953. <th class="head"><p>SQLite type</p></th>
  1954. </tr>
  1955. </thead>
  1956. <tbody>
  1957. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  1958. <td><p><code class="docutils literal notranslate"><span class="pre">NULL</span></code></p></td>
  1959. </tr>
  1960. <tr class="row-odd"><td><p><a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  1961. <td><p><code class="docutils literal notranslate"><span class="pre">INTEGER</span></code></p></td>
  1962. </tr>
  1963. <tr class="row-even"><td><p><a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
  1964. <td><p><code class="docutils literal notranslate"><span class="pre">REAL</span></code></p></td>
  1965. </tr>
  1966. <tr class="row-odd"><td><p><a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a></p></td>
  1967. <td><p><code class="docutils literal notranslate"><span class="pre">TEXT</span></code></p></td>
  1968. </tr>
  1969. <tr class="row-even"><td><p><a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p></td>
  1970. <td><p><code class="docutils literal notranslate"><span class="pre">BLOB</span></code></p></td>
  1971. </tr>
  1972. </tbody>
  1973. </table>
  1974. <p>This is how SQLite types are converted to Python types by default:</p>
  1975. <table class="docutils align-default">
  1976. <thead>
  1977. <tr class="row-odd"><th class="head"><p>SQLite type</p></th>
  1978. <th class="head"><p>Python type</p></th>
  1979. </tr>
  1980. </thead>
  1981. <tbody>
  1982. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">NULL</span></code></p></td>
  1983. <td><p><code class="docutils literal notranslate"><span class="pre">None</span></code></p></td>
  1984. </tr>
  1985. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">INTEGER</span></code></p></td>
  1986. <td><p><a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
  1987. </tr>
  1988. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">REAL</span></code></p></td>
  1989. <td><p><a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
  1990. </tr>
  1991. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">TEXT</span></code></p></td>
  1992. <td><p>depends on <a class="reference internal" href="#sqlite3.Connection.text_factory" title="sqlite3.Connection.text_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">text_factory</span></code></a>,
  1993. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> by default</p></td>
  1994. </tr>
  1995. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">BLOB</span></code></p></td>
  1996. <td><p><a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p></td>
  1997. </tr>
  1998. </tbody>
  1999. </table>
  2000. <p>The type system of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module is extensible in two ways: you can
  2001. store additional Python types in an SQLite database via
  2002. <a class="reference internal" href="#sqlite3-adapters"><span class="std std-ref">object adapters</span></a>,
  2003. and you can let the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module convert SQLite types to
  2004. Python types via <a class="reference internal" href="#sqlite3-converters"><span class="std std-ref">converters</span></a>.</p>
  2005. </section>
  2006. <section id="default-adapters-and-converters-deprecated">
  2007. <span id="sqlite3-default-converters"></span><h3>Default adapters and converters (deprecated)<a class="headerlink" href="#default-adapters-and-converters-deprecated" title="Link to this heading">¶</a></h3>
  2008. <div class="admonition note">
  2009. <p class="admonition-title">Note</p>
  2010. <p>The default adapters and converters are deprecated as of Python 3.12.
  2011. Instead, use the <a class="reference internal" href="#sqlite3-adapter-converter-recipes"><span class="std std-ref">Adapter and converter recipes</span></a>
  2012. and tailor them to your needs.</p>
  2013. </div>
  2014. <p>The deprecated default adapters and converters consist of:</p>
  2015. <ul class="simple">
  2016. <li><p>An adapter for <a class="reference internal" href="datetime.html#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.date</span></code></a> objects to <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">strings</span></code></a> in
  2017. <a class="reference external" href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format.</p></li>
  2018. <li><p>An adapter for <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> objects to strings in
  2019. ISO 8601 format.</p></li>
  2020. <li><p>A converter for <a class="reference internal" href="#sqlite3-converters"><span class="std std-ref">declared</span></a> “date” types to
  2021. <a class="reference internal" href="datetime.html#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.date</span></code></a> objects.</p></li>
  2022. <li><p>A converter for declared “timestamp” types to
  2023. <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> objects.
  2024. Fractional parts will be truncated to 6 digits (microsecond precision).</p></li>
  2025. </ul>
  2026. <div class="admonition note">
  2027. <p class="admonition-title">Note</p>
  2028. <p>The default “timestamp” converter ignores UTC offsets in the database and
  2029. always returns a naive <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> object. To preserve UTC
  2030. offsets in timestamps, either leave converters disabled, or register an
  2031. offset-aware converter with <a class="reference internal" href="#sqlite3.register_converter" title="sqlite3.register_converter"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_converter()</span></code></a>.</p>
  2032. </div>
  2033. <div class="deprecated">
  2034. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  2035. </div>
  2036. </section>
  2037. <section id="command-line-interface">
  2038. <span id="sqlite3-cli"></span><h3>Command-line interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h3>
  2039. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module can be invoked as a script,
  2040. using the interpreter’s <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> switch,
  2041. in order to provide a simple SQLite shell.
  2042. The argument signature is as follows:</p>
  2043. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">sqlite3</span> <span class="p">[</span><span class="o">-</span><span class="n">h</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">v</span><span class="p">]</span> <span class="p">[</span><span class="n">filename</span><span class="p">]</span> <span class="p">[</span><span class="n">sql</span><span class="p">]</span>
  2044. </pre></div>
  2045. </div>
  2046. <p>Type <code class="docutils literal notranslate"><span class="pre">.quit</span></code> or CTRL-D to exit the shell.</p>
  2047. <dl class="std option">
  2048. <dt class="sig sig-object std" id="cmdoption-python-m-sqlite3-h-v-filename-sql-h">
  2049. <span id="cmdoption-python-m-sqlite3-h-v-filename-sql-help"></span><span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-python-m-sqlite3-h-v-filename-sql-h" title="Link to this definition">¶</a></dt>
  2050. <dd><p>Print CLI help.</p>
  2051. </dd></dl>
  2052. <dl class="std option">
  2053. <dt class="sig sig-object std" id="cmdoption-python-m-sqlite3-h-v-filename-sql-v">
  2054. <span id="cmdoption-python-m-sqlite3-h-v-filename-sql-version"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--version</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-python-m-sqlite3-h-v-filename-sql-v" title="Link to this definition">¶</a></dt>
  2055. <dd><p>Print underlying SQLite library version.</p>
  2056. </dd></dl>
  2057. <div class="versionadded">
  2058. <p><span class="versionmodified added">New in version 3.12.</span></p>
  2059. </div>
  2060. </section>
  2061. </section>
  2062. <section id="how-to-guides">
  2063. <span id="sqlite3-howtos"></span><h2>How-to guides<a class="headerlink" href="#how-to-guides" title="Link to this heading">¶</a></h2>
  2064. <section id="how-to-use-placeholders-to-bind-values-in-sql-queries">
  2065. <span id="sqlite3-placeholders"></span><h3>How to use placeholders to bind values in SQL queries<a class="headerlink" href="#how-to-use-placeholders-to-bind-values-in-sql-queries" title="Link to this heading">¶</a></h3>
  2066. <p>SQL operations usually need to use values from Python variables. However,
  2067. beware of using Python’s string operations to assemble queries, as they
  2068. are vulnerable to <a class="reference external" href="https://en.wikipedia.org/wiki/SQL_injection">SQL injection attacks</a>. For example, an attacker can simply
  2069. close the single quote and inject <code class="docutils literal notranslate"><span class="pre">OR</span> <span class="pre">TRUE</span></code> to select all rows:</p>
  2070. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Never do this -- insecure!</span>
  2071. <span class="gp">&gt;&gt;&gt; </span><span class="n">symbol</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
  2072. <span class="go">&#39; OR TRUE; --</span>
  2073. <span class="gp">&gt;&gt;&gt; </span><span class="n">sql</span> <span class="o">=</span> <span class="s2">&quot;SELECT * FROM stocks WHERE symbol = &#39;</span><span class="si">%s</span><span class="s2">&#39;&quot;</span> <span class="o">%</span> <span class="n">symbol</span>
  2074. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
  2075. <span class="go">SELECT * FROM stocks WHERE symbol = &#39;&#39; OR TRUE; --&#39;</span>
  2076. <span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span>
  2077. </pre></div>
  2078. </div>
  2079. <p>Instead, use the DB-API’s parameter substitution. To insert a variable into a
  2080. query string, use a placeholder in the string, and substitute the actual values
  2081. into the query by providing them as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> of values to the second
  2082. argument of the cursor’s <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> method.</p>
  2083. <p>An SQL statement may use one of two kinds of placeholders:
  2084. question marks (qmark style) or named placeholders (named style).
  2085. For the qmark style, <em>parameters</em> must be a
  2086. <a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequence</span></a> whose length must match the number of placeholders,
  2087. or a <a class="reference internal" href="#sqlite3.ProgrammingError" title="sqlite3.ProgrammingError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProgrammingError</span></code></a> is raised.
  2088. For the named style, <em>parameters</em> must be
  2089. an instance of a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> (or a subclass),
  2090. which must contain keys for all named parameters;
  2091. any extra items are ignored.
  2092. Here’s an example of both styles:</p>
  2093. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2094. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE lang(name, first_appeared)&quot;</span><span class="p">)</span>
  2095. <span class="c1"># This is the named style used with executemany():</span>
  2096. <span class="n">data</span> <span class="o">=</span> <span class="p">(</span>
  2097. <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;C&quot;</span><span class="p">,</span> <span class="s2">&quot;year&quot;</span><span class="p">:</span> <span class="mi">1972</span><span class="p">},</span>
  2098. <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Fortran&quot;</span><span class="p">,</span> <span class="s2">&quot;year&quot;</span><span class="p">:</span> <span class="mi">1957</span><span class="p">},</span>
  2099. <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Python&quot;</span><span class="p">,</span> <span class="s2">&quot;year&quot;</span><span class="p">:</span> <span class="mi">1991</span><span class="p">},</span>
  2100. <span class="p">{</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Go&quot;</span><span class="p">,</span> <span class="s2">&quot;year&quot;</span><span class="p">:</span> <span class="mi">2009</span><span class="p">},</span>
  2101. <span class="p">)</span>
  2102. <span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO lang VALUES(:name, :year)&quot;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
  2103. <span class="c1"># This is the qmark style used in a SELECT query:</span>
  2104. <span class="n">params</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1972</span><span class="p">,)</span>
  2105. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM lang WHERE first_appeared = ?&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
  2106. <span class="nb">print</span><span class="p">(</span><span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">())</span>
  2107. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2108. </pre></div>
  2109. </div>
  2110. <div class="admonition note">
  2111. <p class="admonition-title">Note</p>
  2112. <p><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a> numeric placeholders are <em>not</em> supported.
  2113. If used, they will be interpreted as named placeholders.</p>
  2114. </div>
  2115. </section>
  2116. <section id="how-to-adapt-custom-python-types-to-sqlite-values">
  2117. <span id="sqlite3-adapters"></span><h3>How to adapt custom Python types to SQLite values<a class="headerlink" href="#how-to-adapt-custom-python-types-to-sqlite-values" title="Link to this heading">¶</a></h3>
  2118. <p>SQLite supports only a limited set of data types natively.
  2119. To store custom Python types in SQLite databases, <em>adapt</em> them to one of the
  2120. <a class="reference internal" href="#sqlite3-types"><span class="std std-ref">Python types SQLite natively understands</span></a>.</p>
  2121. <p>There are two ways to adapt Python objects to SQLite types:
  2122. letting your object adapt itself, or using an <em>adapter callable</em>.
  2123. The latter will take precedence above the former.
  2124. For a library that exports a custom type,
  2125. it may make sense to enable that type to adapt itself.
  2126. As an application developer, it may make more sense to take direct control by
  2127. registering custom adapter functions.</p>
  2128. <section id="how-to-write-adaptable-objects">
  2129. <span id="sqlite3-conform"></span><h4>How to write adaptable objects<a class="headerlink" href="#how-to-write-adaptable-objects" title="Link to this heading">¶</a></h4>
  2130. <p>Suppose we have a <code class="xref py py-class docutils literal notranslate"><span class="pre">Point</span></code> class that represents a pair of coordinates,
  2131. <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code>, in a Cartesian coordinate system.
  2132. The coordinate pair will be stored as a text string in the database,
  2133. using a semicolon to separate the coordinates.
  2134. This can be implemented by adding a <code class="docutils literal notranslate"><span class="pre">__conform__(self,</span> <span class="pre">protocol)</span></code>
  2135. method which returns the adapted value.
  2136. The object passed to <em>protocol</em> will be of type <a class="reference internal" href="#sqlite3.PrepareProtocol" title="sqlite3.PrepareProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">PrepareProtocol</span></code></a>.</p>
  2137. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
  2138. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  2139. <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
  2140. <span class="k">def</span> <span class="nf">__conform__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">protocol</span><span class="p">):</span>
  2141. <span class="k">if</span> <span class="n">protocol</span> <span class="ow">is</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">PrepareProtocol</span><span class="p">:</span>
  2142. <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="si">}</span><span class="s2">;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="si">}</span><span class="s2">&quot;</span>
  2143. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2144. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  2145. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">),))</span>
  2146. <span class="nb">print</span><span class="p">(</span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
  2147. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2148. </pre></div>
  2149. </div>
  2150. </section>
  2151. <section id="how-to-register-adapter-callables">
  2152. <h4>How to register adapter callables<a class="headerlink" href="#how-to-register-adapter-callables" title="Link to this heading">¶</a></h4>
  2153. <p>The other possibility is to create a function that converts the Python object
  2154. to an SQLite-compatible type.
  2155. This function can then be registered using <a class="reference internal" href="#sqlite3.register_adapter" title="sqlite3.register_adapter"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_adapter()</span></code></a>.</p>
  2156. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
  2157. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  2158. <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
  2159. <span class="k">def</span> <span class="nf">adapt_point</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
  2160. <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="si">}</span><span class="s2">;</span><span class="si">{</span><span class="n">point</span><span class="o">.</span><span class="n">y</span><span class="si">}</span><span class="s2">&quot;</span>
  2161. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span> <span class="n">adapt_point</span><span class="p">)</span>
  2162. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2163. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  2164. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">),))</span>
  2165. <span class="nb">print</span><span class="p">(</span><span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
  2166. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2167. </pre></div>
  2168. </div>
  2169. </section>
  2170. </section>
  2171. <section id="how-to-convert-sqlite-values-to-custom-python-types">
  2172. <span id="sqlite3-converters"></span><h3>How to convert SQLite values to custom Python types<a class="headerlink" href="#how-to-convert-sqlite-values-to-custom-python-types" title="Link to this heading">¶</a></h3>
  2173. <p>Writing an adapter lets you convert <em>from</em> custom Python types <em>to</em> SQLite
  2174. values.
  2175. To be able to convert <em>from</em> SQLite values <em>to</em> custom Python types,
  2176. we use <em>converters</em>.</p>
  2177. <p>Let’s go back to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Point</span></code> class. We stored the x and y coordinates
  2178. separated via semicolons as strings in SQLite.</p>
  2179. <p>First, we’ll define a converter function that accepts the string as a parameter
  2180. and constructs a <code class="xref py py-class docutils literal notranslate"><span class="pre">Point</span></code> object from it.</p>
  2181. <div class="admonition note">
  2182. <p class="admonition-title">Note</p>
  2183. <p>Converter functions are <strong>always</strong> passed a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object,
  2184. no matter the underlying SQLite data type.</p>
  2185. </div>
  2186. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">convert_point</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
  2187. <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;;&quot;</span><span class="p">))</span>
  2188. <span class="k">return</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
  2189. </pre></div>
  2190. </div>
  2191. <p>We now need to tell <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> when it should convert a given SQLite value.
  2192. This is done when connecting to a database, using the <em>detect_types</em> parameter
  2193. of <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a>. There are three options:</p>
  2194. <ul class="simple">
  2195. <li><p>Implicit: set <em>detect_types</em> to <a class="reference internal" href="#sqlite3.PARSE_DECLTYPES" title="sqlite3.PARSE_DECLTYPES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_DECLTYPES</span></code></a></p></li>
  2196. <li><p>Explicit: set <em>detect_types</em> to <a class="reference internal" href="#sqlite3.PARSE_COLNAMES" title="sqlite3.PARSE_COLNAMES"><code class="xref py py-const docutils literal notranslate"><span class="pre">PARSE_COLNAMES</span></code></a></p></li>
  2197. <li><p>Both: set <em>detect_types</em> to
  2198. <code class="docutils literal notranslate"><span class="pre">sqlite3.PARSE_DECLTYPES</span> <span class="pre">|</span> <span class="pre">sqlite3.PARSE_COLNAMES</span></code>.
  2199. Column names take precedence over declared types.</p></li>
  2200. </ul>
  2201. <p>The following example illustrates the implicit and explicit approaches:</p>
  2202. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
  2203. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  2204. <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
  2205. <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  2206. <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;Point(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="si">}</span><span class="s2">)&quot;</span>
  2207. <span class="k">def</span> <span class="nf">adapt_point</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
  2208. <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="si">}</span><span class="s2">;</span><span class="si">{</span><span class="n">point</span><span class="o">.</span><span class="n">y</span><span class="si">}</span><span class="s2">&quot;</span>
  2209. <span class="k">def</span> <span class="nf">convert_point</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
  2210. <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;;&quot;</span><span class="p">)))</span>
  2211. <span class="k">return</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
  2212. <span class="c1"># Register the adapter and converter</span>
  2213. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span> <span class="n">adapt_point</span><span class="p">)</span>
  2214. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_converter</span><span class="p">(</span><span class="s2">&quot;point&quot;</span><span class="p">,</span> <span class="n">convert_point</span><span class="p">)</span>
  2215. <span class="c1"># 1) Parse using declared types</span>
  2216. <span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">)</span>
  2217. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">,</span> <span class="n">detect_types</span><span class="o">=</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_DECLTYPES</span><span class="p">)</span>
  2218. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(p point)&quot;</span><span class="p">)</span>
  2219. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(p) VALUES(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
  2220. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT p FROM test&quot;</span><span class="p">)</span>
  2221. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;with declared types:&quot;</span><span class="p">,</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
  2222. <span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2223. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2224. <span class="c1"># 2) Parse using column names</span>
  2225. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">,</span> <span class="n">detect_types</span><span class="o">=</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_COLNAMES</span><span class="p">)</span>
  2226. <span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE test(p)&quot;</span><span class="p">)</span>
  2227. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO test(p) VALUES(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
  2228. <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&#39;SELECT p AS &quot;p [point]&quot; FROM test&#39;</span><span class="p">)</span>
  2229. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;with column names:&quot;</span><span class="p">,</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span>
  2230. <span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2231. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2232. </pre></div>
  2233. </div>
  2234. </section>
  2235. <section id="adapter-and-converter-recipes">
  2236. <span id="sqlite3-adapter-converter-recipes"></span><h3>Adapter and converter recipes<a class="headerlink" href="#adapter-and-converter-recipes" title="Link to this heading">¶</a></h3>
  2237. <p>This section shows recipes for common adapters and converters.</p>
  2238. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">datetime</span>
  2239. <span class="kn">import</span> <span class="nn">sqlite3</span>
  2240. <span class="k">def</span> <span class="nf">adapt_date_iso</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2241. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Adapt datetime.date to ISO 8601 date.&quot;&quot;&quot;</span>
  2242. <span class="k">return</span> <span class="n">val</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
  2243. <span class="k">def</span> <span class="nf">adapt_datetime_iso</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2244. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Adapt datetime.datetime to timezone-naive ISO 8601 date.&quot;&quot;&quot;</span>
  2245. <span class="k">return</span> <span class="n">val</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
  2246. <span class="k">def</span> <span class="nf">adapt_datetime_epoch</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2247. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Adapt datetime.datetime to Unix timestamp.&quot;&quot;&quot;</span>
  2248. <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">val</span><span class="o">.</span><span class="n">timestamp</span><span class="p">())</span>
  2249. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">adapt_date_iso</span><span class="p">)</span>
  2250. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">adapt_datetime_iso</span><span class="p">)</span>
  2251. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">adapt_datetime_epoch</span><span class="p">)</span>
  2252. <span class="k">def</span> <span class="nf">convert_date</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2253. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Convert ISO 8601 date to datetime.date object.&quot;&quot;&quot;</span>
  2254. <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">val</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
  2255. <span class="k">def</span> <span class="nf">convert_datetime</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2256. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Convert ISO 8601 datetime to datetime.datetime object.&quot;&quot;&quot;</span>
  2257. <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromisoformat</span><span class="p">(</span><span class="n">val</span><span class="o">.</span><span class="n">decode</span><span class="p">())</span>
  2258. <span class="k">def</span> <span class="nf">convert_timestamp</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
  2259. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Convert Unix epoch timestamp to datetime.datetime object.&quot;&quot;&quot;</span>
  2260. <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">val</span><span class="p">))</span>
  2261. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_converter</span><span class="p">(</span><span class="s2">&quot;date&quot;</span><span class="p">,</span> <span class="n">convert_date</span><span class="p">)</span>
  2262. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_converter</span><span class="p">(</span><span class="s2">&quot;datetime&quot;</span><span class="p">,</span> <span class="n">convert_datetime</span><span class="p">)</span>
  2263. <span class="n">sqlite3</span><span class="o">.</span><span class="n">register_converter</span><span class="p">(</span><span class="s2">&quot;timestamp&quot;</span><span class="p">,</span> <span class="n">convert_timestamp</span><span class="p">)</span>
  2264. </pre></div>
  2265. </div>
  2266. </section>
  2267. <section id="how-to-use-connection-shortcut-methods">
  2268. <span id="sqlite3-connection-shortcuts"></span><h3>How to use connection shortcut methods<a class="headerlink" href="#how-to-use-connection-shortcut-methods" title="Link to this heading">¶</a></h3>
  2269. <p>Using the <a class="reference internal" href="#sqlite3.Connection.execute" title="sqlite3.Connection.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a>,
  2270. <a class="reference internal" href="#sqlite3.Connection.executemany" title="sqlite3.Connection.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executemany()</span></code></a>, and <a class="reference internal" href="#sqlite3.Connection.executescript" title="sqlite3.Connection.executescript"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executescript()</span></code></a>
  2271. methods of the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> class, your code can
  2272. be written more concisely because you don’t have to create the (often
  2273. superfluous) <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> objects explicitly. Instead, the <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a>
  2274. objects are created implicitly and these shortcut methods return the cursor
  2275. objects. This way, you can execute a <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement and iterate over it
  2276. directly using only a single call on the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object.</p>
  2277. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Create and fill the table.</span>
  2278. <span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2279. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE lang(name, first_appeared)&quot;</span><span class="p">)</span>
  2280. <span class="n">data</span> <span class="o">=</span> <span class="p">[</span>
  2281. <span class="p">(</span><span class="s2">&quot;C++&quot;</span><span class="p">,</span> <span class="mi">1985</span><span class="p">),</span>
  2282. <span class="p">(</span><span class="s2">&quot;Objective-C&quot;</span><span class="p">,</span> <span class="mi">1984</span><span class="p">),</span>
  2283. <span class="p">]</span>
  2284. <span class="n">con</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s2">&quot;INSERT INTO lang(name, first_appeared) VALUES(?, ?)&quot;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
  2285. <span class="c1"># Print the table contents</span>
  2286. <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT name, first_appeared FROM lang&quot;</span><span class="p">):</span>
  2287. <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  2288. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;I just deleted&quot;</span><span class="p">,</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;DELETE FROM lang&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">rowcount</span><span class="p">,</span> <span class="s2">&quot;rows&quot;</span><span class="p">)</span>
  2289. <span class="c1"># close() is not a shortcut method and it&#39;s not called automatically;</span>
  2290. <span class="c1"># the connection object should be closed manually</span>
  2291. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2292. </pre></div>
  2293. </div>
  2294. </section>
  2295. <section id="how-to-use-the-connection-context-manager">
  2296. <span id="sqlite3-connection-context-manager"></span><h3>How to use the connection context manager<a class="headerlink" href="#how-to-use-the-connection-context-manager" title="Link to this heading">¶</a></h3>
  2297. <p>A <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object can be used as a context manager that
  2298. automatically commits or rolls back open transactions when leaving the body of
  2299. the context manager.
  2300. If the body of the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement finishes without exceptions,
  2301. the transaction is committed.
  2302. If this commit fails,
  2303. or if the body of the <code class="docutils literal notranslate"><span class="pre">with</span></code> statement raises an uncaught exception,
  2304. the transaction is rolled back.
  2305. If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  2306. a new transaction is implicitly opened after committing or rolling back.</p>
  2307. <p>If there is no open transaction upon leaving the body of the <code class="docutils literal notranslate"><span class="pre">with</span></code> statement,
  2308. or if <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
  2309. the context manager does nothing.</p>
  2310. <div class="admonition note">
  2311. <p class="admonition-title">Note</p>
  2312. <p>The context manager neither implicitly opens a new transaction
  2313. nor closes the connection. If you need a closing context manager, consider
  2314. using <a class="reference internal" href="contextlib.html#contextlib.closing" title="contextlib.closing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">contextlib.closing()</span></code></a>.</p>
  2315. </div>
  2316. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2317. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE lang(id INTEGER PRIMARY KEY, name VARCHAR UNIQUE)&quot;</span><span class="p">)</span>
  2318. <span class="c1"># Successful, con.commit() is called automatically afterwards</span>
  2319. <span class="k">with</span> <span class="n">con</span><span class="p">:</span>
  2320. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO lang(name) VALUES(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s2">&quot;Python&quot;</span><span class="p">,))</span>
  2321. <span class="c1"># con.rollback() is called after the with block finishes with an exception,</span>
  2322. <span class="c1"># the exception is still raised and must be caught</span>
  2323. <span class="k">try</span><span class="p">:</span>
  2324. <span class="k">with</span> <span class="n">con</span><span class="p">:</span>
  2325. <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO lang(name) VALUES(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s2">&quot;Python&quot;</span><span class="p">,))</span>
  2326. <span class="k">except</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">IntegrityError</span><span class="p">:</span>
  2327. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;couldn&#39;t add Python twice&quot;</span><span class="p">)</span>
  2328. <span class="c1"># Connection object used as context manager only commits or rollbacks transactions,</span>
  2329. <span class="c1"># so the connection object should be closed manually</span>
  2330. <span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2331. </pre></div>
  2332. </div>
  2333. </section>
  2334. <section id="how-to-work-with-sqlite-uris">
  2335. <span id="sqlite3-uri-tricks"></span><h3>How to work with SQLite URIs<a class="headerlink" href="#how-to-work-with-sqlite-uris" title="Link to this heading">¶</a></h3>
  2336. <p>Some useful URI tricks include:</p>
  2337. <ul class="simple">
  2338. <li><p>Open a database in read-only mode:</p></li>
  2339. </ul>
  2340. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;file:tutorial.db?mode=ro&quot;</span><span class="p">,</span> <span class="n">uri</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  2341. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE readonly(data)&quot;</span><span class="p">)</span>
  2342. <span class="gt">Traceback (most recent call last):</span>
  2343. <span class="gr">OperationalError</span>: <span class="n">attempt to write a readonly database</span>
  2344. </pre></div>
  2345. </div>
  2346. <ul class="simple">
  2347. <li><p>Do not implicitly create a new database file if it does not already exist;
  2348. will raise <a class="reference internal" href="#sqlite3.OperationalError" title="sqlite3.OperationalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OperationalError</span></code></a> if unable to create a new file:</p></li>
  2349. </ul>
  2350. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;file:nosuchdb.db?mode=rw&quot;</span><span class="p">,</span> <span class="n">uri</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  2351. <span class="gt">Traceback (most recent call last):</span>
  2352. <span class="gr">OperationalError</span>: <span class="n">unable to open database file</span>
  2353. </pre></div>
  2354. </div>
  2355. <ul class="simple">
  2356. <li><p>Create a shared named in-memory database:</p></li>
  2357. </ul>
  2358. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">db</span> <span class="o">=</span> <span class="s2">&quot;file:mem1?mode=memory&amp;cache=shared&quot;</span>
  2359. <span class="n">con1</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">uri</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  2360. <span class="n">con2</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">uri</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  2361. <span class="k">with</span> <span class="n">con1</span><span class="p">:</span>
  2362. <span class="n">con1</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;CREATE TABLE shared(data)&quot;</span><span class="p">)</span>
  2363. <span class="n">con1</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;INSERT INTO shared VALUES(28)&quot;</span><span class="p">)</span>
  2364. <span class="n">res</span> <span class="o">=</span> <span class="n">con2</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT data FROM shared&quot;</span><span class="p">)</span>
  2365. <span class="k">assert</span> <span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mi">28</span><span class="p">,)</span>
  2366. <span class="n">con1</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2367. <span class="n">con2</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2368. </pre></div>
  2369. </div>
  2370. <p>More information about this feature, including a list of parameters,
  2371. can be found in the <a class="reference external" href="https://www.sqlite.org/uri.html">SQLite URI documentation</a>.</p>
  2372. </section>
  2373. <section id="how-to-create-and-use-row-factories">
  2374. <span id="sqlite3-howto-row-factory"></span><h3>How to create and use row factories<a class="headerlink" href="#how-to-create-and-use-row-factories" title="Link to this heading">¶</a></h3>
  2375. <p>By default, <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> represents each row as a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>.
  2376. If a <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code> does not suit your needs,
  2377. you can use the <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlite3.Row</span></code></a> class
  2378. or a custom <a class="reference internal" href="#sqlite3.Cursor.row_factory" title="sqlite3.Cursor.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code></a>.</p>
  2379. <p>While <code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code> exists as an attribute both on the
  2380. <a class="reference internal" href="#sqlite3.Cursor" title="sqlite3.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> and the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>,
  2381. it is recommended to set <a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection.row_factory</span></code></a>,
  2382. so all cursors created from the connection will use the same row factory.</p>
  2383. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code> provides indexed and case-insensitive named access to columns,
  2384. with minimal memory overhead and performance impact over a <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code>.
  2385. To use <code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code> as a row factory,
  2386. assign it to the <code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code> attribute:</p>
  2387. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2388. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Row</span>
  2389. </pre></div>
  2390. </div>
  2391. <p>Queries now return <code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code> objects:</p>
  2392. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT &#39;Earth&#39; AS name, 6378 AS radius&quot;</span><span class="p">)</span>
  2393. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
  2394. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
  2395. <span class="go">[&#39;name&#39;, &#39;radius&#39;]</span>
  2396. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># Access by index.</span>
  2397. <span class="go">&#39;Earth&#39;</span>
  2398. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span> <span class="c1"># Access by name.</span>
  2399. <span class="go">&#39;Earth&#39;</span>
  2400. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="p">[</span><span class="s2">&quot;RADIUS&quot;</span><span class="p">]</span> <span class="c1"># Column names are case-insensitive.</span>
  2401. <span class="go">6378</span>
  2402. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2403. </pre></div>
  2404. </div>
  2405. <div class="admonition note">
  2406. <p class="admonition-title">Note</p>
  2407. <p>The <code class="docutils literal notranslate"><span class="pre">FROM</span></code> clause can be omitted in the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement, as in the
  2408. above example. In such cases, SQLite returns a single row with columns
  2409. defined by expressions, e.g. literals, with the given aliases
  2410. <code class="docutils literal notranslate"><span class="pre">expr</span> <span class="pre">AS</span> <span class="pre">alias</span></code>.</p>
  2411. </div>
  2412. <p>You can create a custom <a class="reference internal" href="#sqlite3.Cursor.row_factory" title="sqlite3.Cursor.row_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">row_factory</span></code></a>
  2413. that returns each row as a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>, with column names mapped to values:</p>
  2414. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dict_factory</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">row</span><span class="p">):</span>
  2415. <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">column</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">description</span><span class="p">]</span>
  2416. <span class="k">return</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">value</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="n">row</span><span class="p">)}</span>
  2417. </pre></div>
  2418. </div>
  2419. <p>Using it, queries now return a <code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code> instead of a <code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code>:</p>
  2420. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2421. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">dict_factory</span>
  2422. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT 1 AS a, 2 AS b&quot;</span><span class="p">):</span>
  2423. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  2424. <span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2}</span>
  2425. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2426. </pre></div>
  2427. </div>
  2428. <p>The following row factory returns a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>:</p>
  2429. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
  2430. <span class="k">def</span> <span class="nf">namedtuple_factory</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">row</span><span class="p">):</span>
  2431. <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">column</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">description</span><span class="p">]</span>
  2432. <span class="bp">cls</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s2">&quot;Row&quot;</span><span class="p">,</span> <span class="n">fields</span><span class="p">)</span>
  2433. <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_make</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
  2434. </pre></div>
  2435. </div>
  2436. <p><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple_factory()</span></code> can be used as follows:</p>
  2437. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">&quot;:memory:&quot;</span><span class="p">)</span>
  2438. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">namedtuple_factory</span>
  2439. <span class="gp">&gt;&gt;&gt; </span><span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;SELECT 1 AS a, 2 AS b&quot;</span><span class="p">)</span>
  2440. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
  2441. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span>
  2442. <span class="go">Row(a=1, b=2)</span>
  2443. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># Indexed access.</span>
  2444. <span class="go">1</span>
  2445. <span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="o">.</span><span class="n">b</span> <span class="c1"># Attribute access.</span>
  2446. <span class="go">2</span>
  2447. <span class="gp">&gt;&gt;&gt; </span><span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  2448. </pre></div>
  2449. </div>
  2450. <p>With some adjustments, the above recipe can be adapted to use a
  2451. <a class="reference internal" href="dataclasses.html#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-class docutils literal notranslate"><span class="pre">dataclass</span></code></a>, or any other custom class,
  2452. instead of a <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">namedtuple</span></code></a>.</p>
  2453. </section>
  2454. <section id="how-to-handle-non-utf-8-text-encodings">
  2455. <span id="sqlite3-howto-encoding"></span><h3>How to handle non-UTF-8 text encodings<a class="headerlink" href="#how-to-handle-non-utf-8-text-encodings" title="Link to this heading">¶</a></h3>
  2456. <p>By default, <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> uses <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> to adapt SQLite values
  2457. with the <code class="docutils literal notranslate"><span class="pre">TEXT</span></code> data type.
  2458. This works well for UTF-8 encoded text, but it might fail for other encodings
  2459. and invalid UTF-8.
  2460. You can use a custom <a class="reference internal" href="#sqlite3.Connection.text_factory" title="sqlite3.Connection.text_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">text_factory</span></code></a> to handle such cases.</p>
  2461. <p>Because of SQLite’s <a class="reference external" href="https://www.sqlite.org/flextypegood.html">flexible typing</a>, it is not uncommon to encounter table
  2462. columns with the <code class="docutils literal notranslate"><span class="pre">TEXT</span></code> data type containing non-UTF-8 encodings,
  2463. or even arbitrary data.
  2464. To demonstrate, let’s assume we have a database with ISO-8859-2 (Latin-2)
  2465. encoded text, for example a table of Czech-English dictionary entries.
  2466. Assuming we now have a <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> instance <code class="xref py py-data docutils literal notranslate"><span class="pre">con</span></code>
  2467. connected to this database,
  2468. we can decode the Latin-2 encoded text using this <a class="reference internal" href="#sqlite3.Connection.text_factory" title="sqlite3.Connection.text_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">text_factory</span></code></a>:</p>
  2469. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span><span class="o">.</span><span class="n">text_factory</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;latin2&quot;</span><span class="p">)</span>
  2470. </pre></div>
  2471. </div>
  2472. <p>For invalid UTF-8 or arbitrary data in stored in <code class="docutils literal notranslate"><span class="pre">TEXT</span></code> table columns,
  2473. you can use the following technique, borrowed from the <a class="reference internal" href="../howto/unicode.html#unicode-howto"><span class="std std-ref">Unicode HOWTO</span></a>:</p>
  2474. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">con</span><span class="o">.</span><span class="n">text_factory</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s2">&quot;surrogateescape&quot;</span><span class="p">)</span>
  2475. </pre></div>
  2476. </div>
  2477. <div class="admonition note">
  2478. <p class="admonition-title">Note</p>
  2479. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> module API does not support strings
  2480. containing surrogates.</p>
  2481. </div>
  2482. <div class="admonition seealso">
  2483. <p class="admonition-title">See also</p>
  2484. <p><a class="reference internal" href="../howto/unicode.html#unicode-howto"><span class="std std-ref">Unicode HOWTO</span></a></p>
  2485. </div>
  2486. </section>
  2487. </section>
  2488. <section id="explanation">
  2489. <span id="sqlite3-explanation"></span><h2>Explanation<a class="headerlink" href="#explanation" title="Link to this heading">¶</a></h2>
  2490. <section id="transaction-control">
  2491. <span id="sqlite3-controlling-transactions"></span><span id="sqlite3-transaction-control"></span><h3>Transaction control<a class="headerlink" href="#transaction-control" title="Link to this heading">¶</a></h3>
  2492. <p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> offers multiple methods of controlling whether,
  2493. when and how database transactions are opened and closed.
  2494. <a class="reference internal" href="#sqlite3-transaction-control-autocommit"><span class="std std-ref">Transaction control via the autocommit attribute</span></a> is recommended,
  2495. while <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">Transaction control via the isolation_level attribute</span></a>
  2496. retains the pre-Python 3.12 behaviour.</p>
  2497. <section id="transaction-control-via-the-autocommit-attribute">
  2498. <span id="sqlite3-transaction-control-autocommit"></span><h4>Transaction control via the <code class="docutils literal notranslate"><span class="pre">autocommit</span></code> attribute<a class="headerlink" href="#transaction-control-via-the-autocommit-attribute" title="Link to this heading">¶</a></h4>
  2499. <p>The recommended way of controlling transaction behaviour is through
  2500. the <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.autocommit</span></code></a> attribute,
  2501. which should preferably be set using the <em>autocommit</em> parameter
  2502. of <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a>.</p>
  2503. <p>It is suggested to set <em>autocommit</em> to <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  2504. which implies <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>-compliant transaction control.
  2505. This means:</p>
  2506. <ul class="simple">
  2507. <li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> ensures that a transaction is always open,
  2508. so <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><code class="xref py py-func docutils literal notranslate"><span class="pre">connect()</span></code></a>, <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.commit()</span></code></a>, and <a class="reference internal" href="#sqlite3.Connection.rollback" title="sqlite3.Connection.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.rollback()</span></code></a>
  2509. will implicitly open a new transaction
  2510. (immediately after closing the pending one, for the latter two).
  2511. <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> uses <code class="docutils literal notranslate"><span class="pre">BEGIN</span> <span class="pre">DEFERRED</span></code> statements when opening transactions.</p></li>
  2512. <li><p>Transactions should be committed explicitly using <code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code>.</p></li>
  2513. <li><p>Transactions should be rolled back explicitly using <code class="xref py py-meth docutils literal notranslate"><span class="pre">rollback()</span></code>.</p></li>
  2514. <li><p>An implicit rollback is performed if the database is
  2515. <a class="reference internal" href="#sqlite3.Connection.close" title="sqlite3.Connection.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>-ed with pending changes.</p></li>
  2516. </ul>
  2517. <p>Set <em>autocommit</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> to enable SQLite’s <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions">autocommit mode</a>.
  2518. In this mode, <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.commit()</span></code></a> and <a class="reference internal" href="#sqlite3.Connection.rollback" title="sqlite3.Connection.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.rollback()</span></code></a>
  2519. have no effect.
  2520. Note that SQLite’s autocommit mode is distinct from
  2521. the <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>-compliant <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.autocommit</span></code></a> attribute;
  2522. use <a class="reference internal" href="#sqlite3.Connection.in_transaction" title="sqlite3.Connection.in_transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.in_transaction</span></code></a> to query
  2523. the low-level SQLite autocommit mode.</p>
  2524. <p>Set <em>autocommit</em> to <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a>
  2525. to leave transaction control behaviour to the
  2526. <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.isolation_level</span></code></a> attribute.
  2527. See <a class="reference internal" href="#sqlite3-transaction-control-isolation-level"><span class="std std-ref">Transaction control via the isolation_level attribute</span></a> for more information.</p>
  2528. </section>
  2529. <section id="transaction-control-via-the-isolation-level-attribute">
  2530. <span id="sqlite3-transaction-control-isolation-level"></span><h4>Transaction control via the <code class="docutils literal notranslate"><span class="pre">isolation_level</span></code> attribute<a class="headerlink" href="#transaction-control-via-the-isolation-level-attribute" title="Link to this heading">¶</a></h4>
  2531. <div class="admonition note">
  2532. <p class="admonition-title">Note</p>
  2533. <p>The recommended way of controlling transactions is via the
  2534. <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> attribute.
  2535. See <a class="reference internal" href="#sqlite3-transaction-control-autocommit"><span class="std std-ref">Transaction control via the autocommit attribute</span></a>.</p>
  2536. </div>
  2537. <p>If <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.autocommit</span></code></a> is set to
  2538. <a class="reference internal" href="#sqlite3.LEGACY_TRANSACTION_CONTROL" title="sqlite3.LEGACY_TRANSACTION_CONTROL"><code class="xref py py-data docutils literal notranslate"><span class="pre">LEGACY_TRANSACTION_CONTROL</span></code></a> (the default),
  2539. transaction behaviour is controlled using
  2540. the <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Connection.isolation_level</span></code></a> attribute.
  2541. Otherwise, <code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code> has no effect.</p>
  2542. <p>If the connection attribute <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a>
  2543. is not <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  2544. new transactions are implicitly opened before
  2545. <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> and <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executemany()</span></code></a> executes
  2546. <code class="docutils literal notranslate"><span class="pre">INSERT</span></code>, <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>, <code class="docutils literal notranslate"><span class="pre">DELETE</span></code>, or <code class="docutils literal notranslate"><span class="pre">REPLACE</span></code> statements;
  2547. for other statements, no implicit transaction handling is performed.
  2548. Use the <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a> and <a class="reference internal" href="#sqlite3.Connection.rollback" title="sqlite3.Connection.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rollback()</span></code></a> methods
  2549. to respectively commit and roll back pending transactions.
  2550. You can choose the underlying <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions">SQLite transaction behaviour</a> —
  2551. that is, whether and what type of <code class="docutils literal notranslate"><span class="pre">BEGIN</span></code> statements <code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code>
  2552. implicitly executes –
  2553. via the <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a> attribute.</p>
  2554. <p>If <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  2555. no transactions are implicitly opened at all.
  2556. This leaves the underlying SQLite library in <a class="reference external" href="https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions">autocommit mode</a>,
  2557. but also allows the user to perform their own transaction handling
  2558. using explicit SQL statements.
  2559. The underlying SQLite library autocommit mode can be queried using the
  2560. <a class="reference internal" href="#sqlite3.Connection.in_transaction" title="sqlite3.Connection.in_transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">in_transaction</span></code></a> attribute.</p>
  2561. <p>The <a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><code class="xref py py-meth docutils literal notranslate"><span class="pre">executescript()</span></code></a> method implicitly commits
  2562. any pending transaction before execution of the given SQL script,
  2563. regardless of the value of <a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">isolation_level</span></code></a>.</p>
  2564. <div class="versionchanged">
  2565. <p><span class="versionmodified changed">Changed in version 3.6: </span><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> used to implicitly commit an open transaction before DDL
  2566. statements. This is no longer the case.</p>
  2567. </div>
  2568. <div class="versionchanged">
  2569. <p><span class="versionmodified changed">Changed in version 3.12: </span>The recommended way of controlling transactions is now via the
  2570. <a class="reference internal" href="#sqlite3.Connection.autocommit" title="sqlite3.Connection.autocommit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">autocommit</span></code></a> attribute.</p>
  2571. </div>
  2572. </section>
  2573. </section>
  2574. </section>
  2575. </section>
  2576. <div class="clearer"></div>
  2577. </div>
  2578. </div>
  2579. </div>
  2580. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2581. <div class="sphinxsidebarwrapper">
  2582. <div>
  2583. <h3><a href="../contents.html">Table of Contents</a></h3>
  2584. <ul>
  2585. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a><ul>
  2586. <li><a class="reference internal" href="#tutorial">Tutorial</a></li>
  2587. <li><a class="reference internal" href="#reference">Reference</a><ul>
  2588. <li><a class="reference internal" href="#module-functions">Module functions</a></li>
  2589. <li><a class="reference internal" href="#module-constants">Module constants</a></li>
  2590. <li><a class="reference internal" href="#connection-objects">Connection objects</a></li>
  2591. <li><a class="reference internal" href="#cursor-objects">Cursor objects</a></li>
  2592. <li><a class="reference internal" href="#row-objects">Row objects</a></li>
  2593. <li><a class="reference internal" href="#blob-objects">Blob objects</a></li>
  2594. <li><a class="reference internal" href="#prepareprotocol-objects">PrepareProtocol objects</a></li>
  2595. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  2596. <li><a class="reference internal" href="#sqlite-and-python-types">SQLite and Python types</a></li>
  2597. <li><a class="reference internal" href="#default-adapters-and-converters-deprecated">Default adapters and converters (deprecated)</a></li>
  2598. <li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>
  2599. </ul>
  2600. </li>
  2601. <li><a class="reference internal" href="#how-to-guides">How-to guides</a><ul>
  2602. <li><a class="reference internal" href="#how-to-use-placeholders-to-bind-values-in-sql-queries">How to use placeholders to bind values in SQL queries</a></li>
  2603. <li><a class="reference internal" href="#how-to-adapt-custom-python-types-to-sqlite-values">How to adapt custom Python types to SQLite values</a><ul>
  2604. <li><a class="reference internal" href="#how-to-write-adaptable-objects">How to write adaptable objects</a></li>
  2605. <li><a class="reference internal" href="#how-to-register-adapter-callables">How to register adapter callables</a></li>
  2606. </ul>
  2607. </li>
  2608. <li><a class="reference internal" href="#how-to-convert-sqlite-values-to-custom-python-types">How to convert SQLite values to custom Python types</a></li>
  2609. <li><a class="reference internal" href="#adapter-and-converter-recipes">Adapter and converter recipes</a></li>
  2610. <li><a class="reference internal" href="#how-to-use-connection-shortcut-methods">How to use connection shortcut methods</a></li>
  2611. <li><a class="reference internal" href="#how-to-use-the-connection-context-manager">How to use the connection context manager</a></li>
  2612. <li><a class="reference internal" href="#how-to-work-with-sqlite-uris">How to work with SQLite URIs</a></li>
  2613. <li><a class="reference internal" href="#how-to-create-and-use-row-factories">How to create and use row factories</a></li>
  2614. <li><a class="reference internal" href="#how-to-handle-non-utf-8-text-encodings">How to handle non-UTF-8 text encodings</a></li>
  2615. </ul>
  2616. </li>
  2617. <li><a class="reference internal" href="#explanation">Explanation</a><ul>
  2618. <li><a class="reference internal" href="#transaction-control">Transaction control</a><ul>
  2619. <li><a class="reference internal" href="#transaction-control-via-the-autocommit-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">autocommit</span></code> attribute</a></li>
  2620. <li><a class="reference internal" href="#transaction-control-via-the-isolation-level-attribute">Transaction control via the <code class="docutils literal notranslate"><span class="pre">isolation_level</span></code> attribute</a></li>
  2621. </ul>
  2622. </li>
  2623. </ul>
  2624. </li>
  2625. </ul>
  2626. </li>
  2627. </ul>
  2628. </div>
  2629. <div>
  2630. <h4>Previous topic</h4>
  2631. <p class="topless"><a href="dbm.html"
  2632. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code> — Interfaces to Unix “databases”</a></p>
  2633. </div>
  2634. <div>
  2635. <h4>Next topic</h4>
  2636. <p class="topless"><a href="archiving.html"
  2637. title="next chapter">Data Compression and Archiving</a></p>
  2638. </div>
  2639. <div role="note" aria-label="source link">
  2640. <h3>This Page</h3>
  2641. <ul class="this-page-menu">
  2642. <li><a href="../bugs.html">Report a Bug</a></li>
  2643. <li>
  2644. <a href="https://github.com/python/cpython/blob/main/Doc/library/sqlite3.rst"
  2645. rel="nofollow">Show Source
  2646. </a>
  2647. </li>
  2648. </ul>
  2649. </div>
  2650. </div>
  2651. <div id="sidebarbutton" title="Collapse sidebar">
  2652. <span>«</span>
  2653. </div>
  2654. </div>
  2655. <div class="clearer"></div>
  2656. </div>
  2657. <div class="related" role="navigation" aria-label="related navigation">
  2658. <h3>Navigation</h3>
  2659. <ul>
  2660. <li class="right" style="margin-right: 10px">
  2661. <a href="../genindex.html" title="General Index"
  2662. >index</a></li>
  2663. <li class="right" >
  2664. <a href="../py-modindex.html" title="Python Module Index"
  2665. >modules</a> |</li>
  2666. <li class="right" >
  2667. <a href="archiving.html" title="Data Compression and Archiving"
  2668. >next</a> |</li>
  2669. <li class="right" >
  2670. <a href="dbm.html" title="dbm — Interfaces to Unix “databases”"
  2671. >previous</a> |</li>
  2672. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2673. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2674. <li class="switchers">
  2675. <div class="language_switcher_placeholder"></div>
  2676. <div class="version_switcher_placeholder"></div>
  2677. </li>
  2678. <li>
  2679. </li>
  2680. <li id="cpython-language-and-version">
  2681. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2682. </li>
  2683. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  2684. <li class="nav-item nav-item-2"><a href="persistence.html" >Data Persistence</a> &#187;</li>
  2685. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code> — DB-API 2.0 interface for SQLite databases</a></li>
  2686. <li class="right">
  2687. <div class="inline-search" role="search">
  2688. <form class="inline-search" action="../search.html" method="get">
  2689. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2690. <input type="submit" value="Go" />
  2691. </form>
  2692. </div>
  2693. |
  2694. </li>
  2695. <li class="right">
  2696. <label class="theme-selector-label">
  2697. Theme
  2698. <select class="theme-selector" oninput="activateTheme(this.value)">
  2699. <option value="auto" selected>Auto</option>
  2700. <option value="light">Light</option>
  2701. <option value="dark">Dark</option>
  2702. </select>
  2703. </label> |</li>
  2704. </ul>
  2705. </div>
  2706. <div class="footer">
  2707. &copy;
  2708. <a href="../copyright.html">
  2709. Copyright
  2710. </a>
  2711. 2001-2024, Python Software Foundation.
  2712. <br />
  2713. This page is licensed under the Python Software Foundation License Version 2.
  2714. <br />
  2715. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2716. <br />
  2717. See <a href="/license.html">History and License</a> for more information.<br />
  2718. <br />
  2719. The Python Software Foundation is a non-profit corporation.
  2720. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2721. <br />
  2722. <br />
  2723. Last updated on Apr 09, 2024 (13:47 UTC).
  2724. <a href="/bugs.html">Found a bug</a>?
  2725. <br />
  2726. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2727. </div>
  2728. </body>
  2729. </html>
上海开阖软件有限公司 沪ICP备12045867号-1