import sqlite3
import os.path
db_filename = 'comparison.db'
if os.path.isfile(db_filename):
os.remove(db_filename)
db = sqlite3.connect(db_filename)
cur = db.cursor()
cur.execute('create table t (a, b)')
cur.execute("insert into t values ( 1 , 1 )")
cur.execute("insert into t values ( 1 , 2 )")
cur.execute("insert into t values ( null , 3 )")
cur.execute("insert into t values ( 4 , null )")
cur.execute("insert into t values ( null , null )")
print ("a = b")
for r in cur.execute('select * from t where a = b'):
print (" %d = %d" % (r[0], r[1]))
# 1 = 1
print ("nullif(a, b)")
for r in cur.execute('select nullif(a, b) from t'):
print (r[0])
# None
# 1
# None
# 4
# None
import sqlite3
import os.path
db_file = 'points.db'
if os.path.isfile(db_file):
os.remove(db_file)
db = sqlite3.connect(db_file)
db.text_factory = str
cur = db.cursor()
cur.execute('create table points (id, x , y )')
cur.execute('create table points_rtree(id, x1, x2, y1, y2)')
cur.execute('insert into points values (?,?,?)', (1, 0.5 , 0.5 ))
cur.execute('insert into points values (?,?,?)', (2, 9.99999, 8.88888))
cur.execute('insert into points values (?,?,?)', (3, 42.42 , 0 ))
cur.execute('insert into points values (?,?,?)', (4, 42.42 , 8.888 ))
cur.execute('insert into points_rtree select id, x, x, y, y from points')
db.commit()
for r in cur.execute('select id from points_rtree where x1=42.42'):
print "{:5d}".format(*r)
for r in cur.execute('select id from points_rtree where y2 between 8.8888 and 8.888888'):
print "{:5d}".format(*r)
--
-- sqlite does not have a union operator, it has
-- an except operator instead.
--
create table tq84_except_1 (col_1, col_2, col_3);
create table tq84_except_2 (col_1, col_2, col_3);
insert into tq84_except_1 values ('foo', 'bar', 'baz' );
insert into tq84_except_1 values ('one', 'two', 'three');
insert into tq84_except_1 values ('bla', 'bla', 'bla' );
insert into tq84_except_2 values ('foo', 'bar', 'baz' );
insert into tq84_except_2 values ('abc', 'def', 'ghi' );
.mode column
.width 5 5
select col_1, col_2 from tq84_except_1 EXCEPT
select col_1, col_2 from tq84_except_2;
-- bla bla
-- one two
drop table tq84_except_1;
drop table tq84_except_2;
import sqlite3
import os.path
db_file = 'aggregate_function_without_group_by.db'
if os.path.isfile(db_file):
os.remove(db_file)
db = sqlite3.connect(db_file)
db.text_factory = str
cur = db.cursor()
cur.execute('create table t (a text, b integer)')
cur.execute('insert into t values (?, ?)', ('foo', 7))
cur.execute('insert into t values (?, ?)', ('foo', 2))
cur.execute('insert into t values (?, ?)', ('foo', 5))
cur.execute('insert into t values (?, ?)', ('bar', 3))
cur.execute('insert into t values (?, ?)', ('bar', 8))
cur.execute('insert into t values (?, ?)', ('bar', 5))
cur.execute('insert into t values (?, ?)', ('bar', 4))
cur.execute('insert into t values (?, ?)', ('baz', 4))
cur.execute('insert into t values (?, ?)', ('baz', 6))
for r in cur.execute('select a, min(b), max(b) from t'):
print "{:4s} {:2d} {:2d}".format(*r)
# baz 2 8
create table tq84_table (
id integer primary key
);
with num_generator(n) as (
select 1 n union all
select 1+n from num_generator
limit 1000
)
insert into tq84_table
select n from num_generator;
analyze tq84_table;
select
idx,
stat
from
sqlite_stat1
where
tbl = 'tq84_table';
--
-- Table sqlite_stat3 only available if
-- compiled with SQLITE_ENABLE_STAT3 and
-- without SQLITE_ENABLE_STAT4
--
select * from sqlite_stat3;
--
-- Table sqlite_stat4 only available if
-- compiled with SQLITE_ENABLE_STAT4 and
select * from sqlite_stat4;
drop table tq84_table;
create table tq84_eqp_A (
id integer primary key,
col1 text,
col2 text
);
create table tq84_eqp_B (
id integer primary key,
col1 text,
col2 text
);
create table tq84_eqp_A2B (
id_A integer references tq84_eqp_A,
id_B integer references tq84_eqp_B,
col1 text,
col2 text
);
create index tq84_eqp_A_ix_col2 on tq84_eqp_A (col2);
create index tq84_eqp_A2B_ix_col1 on tq84_eqp_A2B(col1);
.headers on
.mode column
.width 8 5 4 80
explain query plan
select
A.col1,
A.col2,
B.col2
from
tq84_eqp_A A join
tq84_eqp_A2B A2B on A.id = A2B.id_A join
tq84_eqp_B B on B.id = A2B.id_B
where
A.col2 > 'foo' and
B.col1 = 'bar';
drop table tq84_eqp_A2B;
drop table tq84_eqp_A;
drop table tq84_eqp_B;
attach database 'xyz.db' as xyz;
create table xyz.tab (a, b, c);
--
-- Note: in SQLite, when creating an index,
-- the schema name xyz is in front of the index name rather
-- than the table name:
--
create index xyz.ix on tab(a);
detach xyz;