SQLite as storage format? maybe not so crazy
- we can write directly from pandas (with help of
sqlalchemy
)
import pandas as pd
from sqlalchemy import create_engine
df = pd.DataFrame({'a':[1,2,3], 'b':['x','y','z']})
sqlite = create_engine('sqlite:///test.sqlite')
df.to_sql('hello', sqlite)
or we can create db files from the CLI (and thus pipe into),
» sqlite3
create table hello (a int, b char);
insert into hello values(1, 'hello');
insert into hello values(2, 'world');
.save test.db
.quit
or debug using the same tool sqlite3
,
» sqlite3 test.db
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE hello (a int, b char);
INSERT INTO "hello" VALUES(0,'hello');
INSERT INTO "hello" VALUES(1,'world');
COMMIT;
there is also a large nr of other tools to work with the format.
Eg. once stored, we can read it with node, or in the browser with https://github.com/kripken/sql.js/
» node index.js
{ columns: [ 'index', 'a', 'b' ],
values: [ [ 0, 1, 'x' ], [ 1, 2, 'y' ], [ 2, 3, 'z' ] ] }
here for node, browser is similar
var fs = require('fs')
var SQL = require('sql.js')
var filebuffer = fs.readFileSync('test.sqlite')
var db = new SQL.Database(filebuffer)
var res = db.exec("SELECT * FROM hello")
res.forEach(function (row){console.log(row)})