Python Language Tutorial => SQLite
Example
SQLite 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
conn = sqlite3.connect("users.db")
c = conn.cursor()
c.execute("CREATE TABLE user (name text, age integer)")
c.execute("INSERT INTO user VALUES ('User A', 42)")
c.execute("INSERT INTO user VALUES ('User B', 43)")
conn.commit()
c.execute("SELECT * FROM user")
print(c.fetchall())
conn.close()
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:
[(u'User A', 42), (u'User B', 43)]
The SQLite Syntax: An in-depth analysis
Getting started
-
Import the sqlite 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 example.db file:
>>> 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_levelIt is an attribute used to get or set the current isolation level. None for autocommit mode or one of
DEFERRED,IMMEDIATEorEXCLUSIVE.
-
cursorThe 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. Ifclose()is called without first callingcommit()(assuming you are not in autocommit mode) then all changes made will be lost.
-
total_changesAn attribute that logs the total number of rows modified, deleted or inserted since the database was opened.
-
execute,executemany, andexecutescriptThese 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_factoryYou 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
%sfor inserting strings into SQL commands as it can make your program vulnerable to an SQL injection attack (see SQL Injection ).
-
executemany(sql, seq_of_parameters)Executes an SQL command against all parameter sequences or mappings found in the sequence sql. The sqlite3 module also allows using an iterator yielding parameters instead of a sequence.
L = [(1, 'abcd', 'dfj', 300), # A list of tuples to be inserted into the database (2, 'cfgd', 'dyfj', 400), (3, 'sdd', 'dfjh', 300.50)] conn = sqlite3.connect("test1.db") conn.execute("create table if not exists book (id int, name text, author text, price real)") conn.executemany("insert into book values (?, ?, ?, ?)", L) for row in conn.execute("select * from book"): print(row)You can also pass iterator objects as a parameter to executemany, and the function will iterate over the each tuple of values that the iterator returns. The iterator must return a tuple of values.
import sqlite3 class IterChars: def __init__(self): self.count = ord('a') def __iter__(self): return self def __next__(self): # (use next(self) for Python 2) if self.count > ord('z'): raise StopIteration self.count += 1 return (chr(self.count - 1),) conn = sqlite3.connect("abc.db") cur = conn.cursor() cur.execute("create table characters(c)") theIter = IterChars() cur.executemany("insert into characters(c) values (?)", theIter) rows = cur.execute("select c from characters") for row in rows: print(row[0]), -
executescript(sql_script)This is a nonstandard convenience method for executing multiple SQL statements at once. It issues a
COMMITstatement first, then executes the SQL script it gets as a parameter.sql_scriptcan be an instance ofstrorbytes.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
SELECTstatements in SQL. To retrieve data after executing aSELECTstatement, you can either treat the cursor as an iterator, call the cursor’sfetchone()method to retrieve a single matching row, or callfetchall()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 a single row. 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