Skip to content

Instantly share code, notes, and snippets.

@wires
Created July 2, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wires/76adc11d8264543639f888bd598ba102 to your computer and use it in GitHub Desktop.
Save wires/76adc11d8264543639f888bd598ba102 to your computer and use it in GitHub Desktop.

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)})
var fs = require('fs')
var SQL = require('sql.js')
//var filebuffer = fs.readFileSync('test.sqlite')
var filebuffer = fs.readFileSync('test.db')
var db = new SQL.Database(filebuffer)
var res = db.exec("SELECT * FROM hello")
res.forEach(function (row) {
console.log(row)
})
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.db')
df.to_sql('hello', sqlite)
{
"name": "dotsqlite",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"sql.js": "^0.3.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment