8
8
9
9
**Source code: ** :source: `Lib/sqlite3/ `
10
10
11
+ .. Make sure we always doctest the tutorial with an empty database.
12
+
13
+ .. testsetup ::
14
+
15
+ import sqlite3
16
+ src = sqlite3.connect(":memory: ", isolation_level=None)
17
+ dst = sqlite3.connect("tutorial.db", isolation_level=None)
18
+ src.backup(dst)
19
+ del src, dst
11
20
12
21
.. _sqlite3-intro :
13
22
@@ -65,7 +74,9 @@ First, we need to create a new database and open
65
74
a database connection to allow :mod: `!sqlite3 ` to work with it.
66
75
Call :func: `sqlite3.connect ` to to create a connection to
67
76
the database :file: `tutorial.db ` in the current working directory,
68
- implicitly creating it if it does not exist::
77
+ implicitly creating it if it does not exist:
78
+
79
+ .. testcode ::
69
80
70
81
import sqlite3
71
82
con = sqlite3.connect("tutorial.db")
@@ -75,7 +86,9 @@ represents the connection to the on-disk database.
75
86
76
87
In order to execute SQL statements and fetch results from SQL queries,
77
88
we will need to use a database cursor.
78
- Call :meth: `con.cursor() <Connection.cursor> ` to create the :class: `Cursor `::
89
+ Call :meth: `con.cursor() <Connection.cursor> ` to create the :class: `Cursor `:
90
+
91
+ .. testcode ::
79
92
80
93
cur = con.cursor()
81
94
@@ -86,7 +99,9 @@ For simplicity, we can just use column names in the table declaration --
86
99
thanks to the `flexible typing `_ feature of SQLite,
87
100
specifying the data types is optional.
88
101
Execute the ``CREATE TABLE `` statement
89
- by calling :meth: `cur.execute(...) <Cursor.execute> `::
102
+ by calling :meth: `cur.execute(...) <Cursor.execute> `:
103
+
104
+ .. testcode ::
90
105
91
106
cur.execute("CREATE TABLE movie(title, year, score)")
92
107
@@ -99,7 +114,9 @@ which should now contain an entry for the ``movie`` table definition
99
114
(see `The Schema Table `_ for details).
100
115
Execute that query by calling :meth: `cur.execute(...) <Cursor.execute> `,
101
116
assign the result to ``res ``,
102
- and call :meth: `res.fetchone() <Cursor.fetchone> ` to fetch the resulting row::
117
+ and call :meth: `res.fetchone() <Cursor.fetchone> ` to fetch the resulting row:
118
+
119
+ .. doctest ::
103
120
104
121
>>> res = cur.execute(" SELECT name FROM sqlite_master" )
105
122
>>> res.fetchone()
@@ -108,15 +125,19 @@ and call :meth:`res.fetchone() <Cursor.fetchone>` to fetch the resulting row::
108
125
We can see that the table has been created,
109
126
as the query returns a :class: `tuple ` containing the table's name.
110
127
If we query ``sqlite_master `` for a non-existent table ``spam ``,
111
- :meth: `!res.fetchone() ` will return ``None ``::
128
+ :meth: `!res.fetchone() ` will return ``None ``:
129
+
130
+ .. doctest ::
112
131
113
132
>>> res = cur.execute(" SELECT name FROM sqlite_master WHERE name='spam'" )
114
133
>>> res.fetchone() is None
115
134
True
116
135
117
136
Now, add two rows of data supplied as SQL literals
118
137
by executing an ``INSERT `` statement,
119
- once again by calling :meth: `cur.execute(...) <Cursor.execute> `::
138
+ once again by calling :meth: `cur.execute(...) <Cursor.execute> `:
139
+
140
+ .. testcode ::
120
141
121
142
cur.execute("""
122
143
INSERT INTO movie VALUES
@@ -128,15 +149,19 @@ The ``INSERT`` statement implicitly opens a transaction,
128
149
which needs to be committed before changes are saved in the database
129
150
(see :ref: `sqlite3-controlling-transactions ` for details).
130
151
Call :meth: `con.commit() <Connection.commit> ` on the connection object
131
- to commit the transaction::
152
+ to commit the transaction:
153
+
154
+ .. testcode ::
132
155
133
156
con.commit()
134
157
135
158
We can verify that the data was inserted correctly
136
159
by executing a ``SELECT `` query.
137
160
Use the now-familiar :meth: `cur.execute(...) <Cursor.execute> ` to
138
161
assign the result to ``res ``,
139
- and call :meth: `res.fetchall() <Cursor.fetchall> ` to return all resulting rows::
162
+ and call :meth: `res.fetchall() <Cursor.fetchall> ` to return all resulting rows:
163
+
164
+ .. doctest ::
140
165
141
166
>>> res = cur.execute(" SELECT score FROM movie" )
142
167
>>> res.fetchall()
@@ -146,7 +171,9 @@ The result is a :class:`list` of two :class:`!tuple`\s, one per row,
146
171
each containing that row's ``score `` value.
147
172
148
173
Now, insert three more rows by calling
149
- :meth: `cur.executemany(...) <Cursor.executemany> `::
174
+ :meth: `cur.executemany(...) <Cursor.executemany> `:
175
+
176
+ .. testcode ::
150
177
151
178
data = [
152
179
("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
@@ -164,14 +191,16 @@ to avoid `SQL injection attacks`_
164
191
165
192
We can verify that the new rows were inserted
166
193
by executing a ``SELECT `` query,
167
- this time iterating over the results of the query::
194
+ this time iterating over the results of the query:
195
+
196
+ .. doctest ::
168
197
169
198
>>> for row in cur.execute(" SELECT year, title FROM movie ORDER BY year" ):
170
199
... print (row)
171
- (1971, " And Now for Something Completely Different" )
172
- (1975, " Monty Python and the Holy Grail" )
200
+ (1971, ' And Now for Something Completely Different' )
201
+ (1975, ' Monty Python and the Holy Grail' )
173
202
(1979, "Monty Python's Life of Brian")
174
- (1982, " Monty Python Live at the Hollywood Bowl" )
203
+ (1982, ' Monty Python Live at the Hollywood Bowl' )
175
204
(1983, "Monty Python's The Meaning of Life")
176
205
177
206
Each row is a two-item :class: `tuple ` of ``(year, title) ``,
@@ -180,15 +209,17 @@ matching the columns selected in the query.
180
209
Finally, verify that the database has been written to disk
181
210
by calling :meth: `con.close() <Connection.close> `
182
211
to close the existing connection, opening a new one,
183
- creating a new cursor, then querying the database::
212
+ creating a new cursor, then querying the database:
213
+
214
+ .. doctest ::
184
215
185
216
>>> con.close()
186
217
>>> new_con = sqlite3.connect(" tutorial.db" )
187
218
>>> new_cur = new_con.cursor()
188
- >>> res = new_cur.execute("SELECT year, title FROM movie ORDER BY score DESC"):
219
+ >>> res = new_cur.execute(" SELECT title, year FROM movie ORDER BY score DESC" )
189
220
>>> title, year = res.fetchone()
190
221
>>> print (f ' The highest scoring Monty Python movie is { title!r } , released in { year} ' )
191
- ' The highest scoring Monty Python movie is " Monty Python and the Holy Grail" , released in 1975'
222
+ The highest scoring Monty Python movie is ' Monty Python and the Holy Grail' , released in 1975
192
223
193
224
You've now created an SQLite database using the :mod: `!sqlite3 ` module,
194
225
inserted data and retrieved values from it in multiple ways.
0 commit comments