Skip to content

Instantly share code, notes, and snippets.

@fahmiegerton
Last active February 22, 2021 13:07
Show Gist options
  • Save fahmiegerton/f9660b72805796703f989a79f75656f5 to your computer and use it in GitHub Desktop.
Save fahmiegerton/f9660b72805796703f989a79f75656f5 to your computer and use it in GitHub Desktop.
const sqlite3 = require('sqlite3').verbose();
var db;
var dberror = false;
const path = require('path');
async function buatDb() {
console.log("Sedang membuat db");
db = await new sqlite3.Database(path.resolve(__dirname, '../bin/dbsesi.db'), (err) => {
if (err) {
dberror = true;
console.error(path.resolve(__dirname, '../bin/dbsesi.db'));
console.error("Error opening database "+err.message);
} else {
console.log('buat tabel');
createTable();
}
});
}
function createTable() {
console.log("Sedang membuat tabel clientcode");
db.serialize(function() {
var stmt = (`CREATE TABLE clientcode(
id INTEGER PRIMARY KEY AUTOINCREMENT,
origin VARCHAR(70) NOT NULL,
token TEXT NOT NULL,
authorized TINYINT(1) DEFAULT 0 NOT NULL,
created_at TEXT
)`);
db.run(stmt);
insertRows();
});
}
function insertRows() {
console.log("insert data 1");
var stmt = db.prepare("INSERT INTO clientcode (origin, token, authorized, created_at) VALUES (?,?,?,?)");
stmt.run("http://api.coba.test", "token", 1, "datetime('now')");
stmt.finalize();
}
function runChain() {
buatDb();
}
runChain();
module.exports = db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment