SQLite
suggest changeSQLite is a lightweight, disk-based database. Since it does not require a separate database server, it is often used for prototyping or for small applications that are often used by a single user or by one user at a given time.
import sqlite3
with sqlite3.connect("users.db") as connection:
cursor = connection.cursor()
cursor.execute("CREATE TABLE user (name TEXT, age INTEGER)")
cursor.executemany(
"INSERT INTO user VALUES (?, ?)",
[("User A", 42), ("User B", 43)],
)
cursor.execute("SELECT * FROM user")
print(cursor.fetchall())
The code above connects to the database stored in the file named users.db, creating the file first if it doesn’t already exist. You can interact with the database via SQL statements.
The result of this example should be:
[('User A', 42), ('User B', 43)]
The SQLite Syntax: An in-depth analysis
Getting started
- Import the sqlite3 module using
>>> import sqlite3
- To use the module, you must first create a Connection object that represents the database. Here the data will be stored in the
users.dbfile:
>>> conn = sqlite3.connect('users.db')
Alternatively, you can also supply the special name :memory: to create a temporary database in RAM, as follows:
>>> conn = sqlite3.connect(':memory:')
- Once you have a
Connection, you can create aCursorobject and call itsexecute()method to perform SQL commands:
c = conn.cursor()
# Create table
c.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''')
# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
# Save (commit) the changes
conn.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
Important Attributes and Functions of Connection
isolation_level
It is an attribute used to get or set the current isolation level. None for autocommit mode or one of DEFERRED, IMMEDIATE or EXCLUSIVE.
cursor
The cursor object is used to execute SQL commands and queries.
commit()
Commits the current transaction.
rollback()
Rolls back any changes made since the previous call to commit().
close()
Closes the database connection. It does not call commit() automatically. If close() is called without first calling commit() (assuming you are not in autocommit mode) then all changes made will be lost.
total_changes
An attribute that logs the total number of rows modified, deleted or inserted since the database was opened.
execute,executemany, andexecutescript
These functions perform the same way as those of the cursor object. This is a shortcut since calling these functions through the connection object results in the creation of an intermediate cursor object and calls the corresponding method of the cursor object.
row_factory
You can change this attribute to a callable that accepts the cursor and the original row as a tuple and will return the real result row.
def dict_factory(cursor, row):
d = {}
for i, col in enumerate(cursor.description):
d[col[0]] = row[i]
return d
conn = sqlite3.connect(":memory:")
conn.row_factory = dict_factory
Important Functions of Cursor
execute(sql[, parameters])
Executes a single SQL statement. The SQL statement may be parametrized (i.e. placeholders instead of SQL literals). The sqlite3 module supports two kinds of placeholders: question marks ? (qmark style) and named placeholders :name (named style).
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.execute("create table people (name, age)")
who = "Sophia"
age = 37
# This is the qmark style:
cur.execute("insert into people values (?, ?)",
(who, age))
# And this is the named style:
cur.execute("select * from people where name=:who and age=:age",
{"who": who, "age": age}) # the keys correspond to the placeholders in SQL
print(cur.fetchone())
Beware: don’t use %s for inserting strings into SQL commands as it can make your program vulnerable to an SQL injection attack (see https://stackoverflow.com/documentation/sql/3517/sql-injection ).
-
executemany(sql, seq_of_parameters) -
executescript(sql_script)
This is a nonstandard convenience method for executing multiple SQL statements at once. It issues a `COMMIT` statement first, then executes the SQL script it gets as a parameter.
`sql_script` can be an instance of `str` or `bytes`.
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.executescript("""
create table person(
firstname,
lastname,
age
);
create table book(
title,
author,
published
);
insert into book(title, author, published)
values (
'Dirk Gently''s Holistic Detective Agency',
'Douglas Adams',
1987
);
""")
The next set of functions are used in conjunction with SELECT statements in SQL. To retrieve data after executing a SELECT statement, you can either treat the cursor as an iterator, call the cursor’s fetchone() method to retrieve a single matching row, or call fetchall() to get a list of the matching rows.
Example of the iterator form:
import sqlite3
stocks = [('2006-01-05', 'BUY', 'RHAT', 100, 35.14),
('2006-03-28', 'BUY', 'IBM', 1000, 45.0),
('2006-04-06', 'SELL', 'IBM', 500, 53.0),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)]
conn = sqlite3.connect(":memory:")
conn.execute("create table stocks (date text, buysell text, symb text, amount int, price real)")
conn.executemany("insert into stocks values (?, ?, ?, ?, ?)", stocks)
cur = conn.cursor()
for row in cur.execute('SELECT * FROM stocks ORDER BY price'):
print(row)
# Output:
# ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
# ('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
# ('2006-04-06', 'SELL', 'IBM', 500, 53.0)
# ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)
fetchone()
Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.
cur.execute('SELECT * FROM stocks ORDER BY price')
i = cur.fetchone()
while(i):
print(i)
i = cur.fetchone()
# Output:
# ('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
# ('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
# ('2006-04-06', 'SELL', 'IBM', 500, 53.0)
# ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)
fetchmany(size=cursor.arraysize)
Fetches the next set of rows of a query result (specified by size), returning a list. If size is omitted, fetchmany returns up to cursor.arraysize rows. arraysize defaults to 1, so the call returns one row until you change it. An empty list is returned when no more rows are available.
cur.execute('SELECT * FROM stocks ORDER BY price')
print(cur.fetchmany(2))
# Output:
# [('2006-01-05', 'BUY', 'RHAT', 100, 35.14), ('2006-03-28', 'BUY', 'IBM', 1000, 45.0)]
fetchall()
Fetches all (remaining) rows of a query result, returning a list.
cur.execute('SELECT * FROM stocks ORDER BY price')
print(cur.fetchall())
# Output:
# [('2006-01-05', 'BUY', 'RHAT', 100, 35.14), ('2006-03-28', 'BUY', 'IBM', 1000, 45.0), ('2006-04-06', 'SELL', 'IBM', 500, 53.0), ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)]
SQLite and Python data types
SQLite natively supports the following types: NULL, INTEGER, REAL, TEXT, BLOB.
This is how the data types are converted when moving from SQL to Python or vice versa.
None <-> NULL
int <-> INTEGER/INT
float <-> REAL/FLOAT
str <-> TEXT/VARCHAR(n)
bytes <-> BLOB