src = sqlite3.connect(":memory:", isolation_level=None)
dst = sqlite3.connect("tutorial.db", isolation_level=None)
src.backup(dst)
+ src.close()
+ dst.close()
del src, dst
.. _sqlite3-intro:
>>> title, year = res.fetchone()
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
+ >>> new_con.close()
You've now created an SQLite database using the :mod:`!sqlite3` module,
inserted data and retrieved values from it in multiple ways.
>>> for row in con.execute("SELECT md5(?)", (b"foo",)):
... print(row)
('acbd18db4cc2f85cedef654fccc4a4d8',)
+ >>> con.close()
.. method:: create_aggregate(name, n_arg, aggregate_class)
FROM test ORDER BY x
""")
print(cur.fetchall())
+ con.close()
.. testoutput::
:hide:
src = sqlite3.connect('example.db')
dst = sqlite3.connect(':memory:')
src.backup(dst)
+ dst.close()
+ src.close()
.. versionadded:: 3.7
>>> con.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED)
1
+ .. testcleanup:: sqlite3.limits
+
+ con.close()
+
.. versionadded:: 3.11
.. _SQLite limit category: https://www.sqlite.org/c3ref/c_limit_attached.html
# cur is an sqlite3.Cursor object
cur.executemany("INSERT INTO data VALUES(?)", rows)
+ .. testcleanup:: sqlite3.cursor
+
+ con.close()
+
.. note::
Any resulting rows are discarded,
>>> cur = con.cursor()
>>> cur.connection == con
True
+ >>> con.close()
.. attribute:: description
greeting = blob.read()
print(greeting) # outputs "b'Hello, world!'"
+ con.close()
.. testoutput::
:hide:
params = (1972,)
cur.execute("SELECT * FROM lang WHERE first_appeared = ?", params)
print(cur.fetchall())
+ con.close()
.. testoutput::
:hide:
cur.execute("SELECT ?", (Point(4.0, -3.2),))
print(cur.fetchone()[0])
+ con.close()
.. testoutput::
:hide:
cur.execute("SELECT ?", (Point(1.0, 2.5),))
print(cur.fetchone()[0])
+ con.close()
.. testoutput::
:hide:
cur.execute("INSERT INTO test(p) VALUES(?)", (p,))
cur.execute('SELECT p AS "p [point]" FROM test')
print("with column names:", cur.fetchone()[0])
+ cur.close()
+ con.close()
.. testoutput::
:hide:
res = con2.execute("SELECT data FROM shared")
assert res.fetchone() == (28,)
+ con1.close()
+ con2.close()
More information about this feature, including a list of parameters,
can be found in the `SQLite URI documentation`_.
'Earth'
>>> row["RADIUS"] # Column names are case-insensitive.
6378
+ >>> con.close()
.. note::
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
... print(row)
{'a': 1, 'b': 2}
+ >>> con.close()
The following row factory returns a :term:`named tuple`:
1
>>> row.b # Attribute access.
2
+ >>> con.close()
With some adjustments, the above recipe can be adapted to use a
:class:`~dataclasses.dataclass`, or any other custom class,