soft.sqlite - jgrey4296/jgrey4296.github.io GitHub Wiki

Sqlite

Reference

Syntax

SQLite is case insensitive apart from GLOB and glob. Statement end is ‘;’

Main commands

CREATE

sqlite3 my_new_database.db

ATTACH | DETACH

For databases in separate files.

attach database 'test.db' as 'TEST';
detach database 'TEST';

CREATE | DROP table

-- create table $dbname.$tablename ( columnN, datatypeN constraints, );
create table main.test (id int primary key not null, name text not null);
drop table main.test;
Column Constraints
not null
default $X
unique
primary key
check($CONDITIONS)
autoincrement only on integers, must be last

INSERT | DELETE

-- insert into $tablename (columns) values (values);
-- insert into $tablename values (values) (values) (values)...;
insert into test (id, name) values (1, "bob"), (2, "bill");

-- delete from $tablename where $conditions;
delete from test where id = 1;

-- DELETE ALL RECORDS:
-- delete from $tablename;

-- where primary key is autoincrement:
insert into test (name) values ("bob"), ("bill"), ("jill");

SELECT

-- select * from test where condition;
-- select * from test where limit $no_of_rows offset $row_num;
-- select * from test order by column asc/desc;

UPDATE

-- update $tablename set $column = $value, $column2 = $value2 where $conditions;
update test set name = 'taweg' where id = 2;

Data Types

NULL
INTEGER
REAL
TEXT
BLOB

Operators

-- Arithmetic: + 0 * / %

-- Comparison: == !=, <, <=, >, >=
-- <> : equality test
-- !<, >! : not greater/lesser

-- and, between exists, in, not in, not, or, is null, is, is not
-- unique
-- || : string concat
-- like, GLOB : compare values using wildcards

-- avg(), sum(), count()
-- select count(*) as "value name" from test;

JOINS

Useful commands:

.show List settings
.mode Set output formatting mode csv, column, html, insert, line, list, tabs, tcl
.nullvalue $STRING set a default string in place of null values
.schema $TABLE show the setup of a table
.tables list all tables in the file
.dump $TABLE output the table in SQL format
.headers on/off display headers on output
.backup main $FILE backup db main to a file

Python wrappers

sqlalchemy

pony

Org-Babel header args:

db a string with the name of the file that
holds the SQLite database. Babel requires this header argument.
header if present, turn on headers in the output format.
Headers are also output with the header argument :colnames yes.
echo if present, set the SQLite dot command .echo to ON.
bail if present, set the SQLite dot command .bail to ON.
csv the default SQLite output format for Babel SQLite source code blocks.
column an SQLite output format that outputs a table-like form with
whitespace between columns.
html an SQLite output format that outputs query results as simple HTML tables.
line an SQLite output format that outputs query results with one value per line.
list an SQLite output format that outputs query results with the separator
character between fields.
separator a string that specifies the separator character used by the SQLite
`list’ output mode and by the SQLite dot command .import.
nullvalue a string to use in place of NULL values.

Best Practices

Sandbox

create table test (id int primary key not null, name text not null);
.schema test

http://www.sqlite.org/docs.html

http://www.sqlite.org/sqlite.html

http://www.sqlite.org/whentouse.html

http://www.thegeekstuff.com/2012/09/sqlite-command-examples/

http://zetcode.com/db/sqliteperltutorial/connect/

http://zetcode.com/db/sqliteperltutorial/dbi/

http://zetcode.com/db/sqlitepythontutorial/

https://docs.python.org/3/library/sqlite3.html

https://github.com/AlecKazakova/sqlite-bnf

https://github.com/dumblob/mysql2sqlite

https://rednafi.com/

https://rednafi.github.io/reflections/recipes-from-python-sqlite-docs.html

https://sqlite.land/sqlite-browser

https://sqlite.org/docs.html

https://sqlite.org/lang.html

https://stackoverflow.com/questions/82875/how-to-list-the-tables-in-an-sqlite-database-file-that-was-opened-with-attach#82899 http://pythoncentral.io/introductory-tutorial-python-sqlalchemy/

https://docs.sqlalchemy.org/en/20/intro.html https://docs.ponyorm.org/firststeps.html

https://github.com/ponyorm/pony/

⚠️ **GitHub.com Fallback** ⚠️