cat << EOF > ~/.sqliterc
.headers on
.mode table
EOF
sqlite3 path/to/mydb.sqlite3
.help
.help mode
.headers on
.mode list|csv|column|line|table
.mode table
.tables # list tables
.databases # List names and files of attached databases
.schema # show create table for all tables
.schema mytable # show create table for one table
select json_pretty(column_containing_json) from my_table
select rowid,* from person
# .once specifies that the next query will be sent to a file.
.once my_file1.txt
SELECT * FROM my_table;
# .output specifies that all further output will be sent to a file.
# my_file2.txt will contain the results of the 2 queries.
.output my_file2.txt
SELECT * FROM my_table;
SELECT * FROM my_table limit 10;
# to come back to normal display.
.output stdout
# or
.output
php
public static function getConnection($path): ?\PDO {
return new \PDO('sqlite:' . $path);
}
prepare / execute / fetch
$stmt = $sqlite->prepare("select rowid,* from person where bday=:bday");
$stmt->execute([':bday' => $bday]);
foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row){}
// or fetch one row
$myVar = $stmt->fetch(\PDO::FETCH_ASSOC);
query / fetch
$stmt = $sqlite->query("select slug from person where slug like 'gauquelin-%'");
foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row){}
// or fetch one row
$myVar = $stmt->fetch(\PDO::FETCH_ASSOC);
direct queries
$stmt = $sqlite->query("insert into myTable(myCol) values('myValue')");
$sqlite->exec('create table person(name varchar(255))');