Created
May 20, 2020 06:20
-
-
Save aminroosta/6239f103c8bdb80f435998fd77df1f86 to your computer and use it in GitHub Desktop.
deno sqlite example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { open, save, DB } from "https://deno.land/x/sqlite/mod.ts"; | |
class Database { | |
private constructor(private db: DB) { } | |
static async connect(path : string) { | |
return new Database(await open(path)); | |
} | |
execute(query: string, values : object = {}) { | |
this.db.query(query, values).done(); | |
} | |
query(query: string, values: object = { }) { | |
return this.db.query(query, values); | |
} | |
select<T>(query: string, values: object = {}) { | |
} | |
} | |
Database.connect("test.db"); | |
const db = await open("test.db"); | |
db.query(` | |
CREATE TABLE IF NOT EXISTS people ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
name TEXT | |
)`, {}); | |
db.query("delete from people", {}); | |
const names = ["Peter Parker", "Clark Kent", "Bruce Wayne"]; | |
// Run a simple query | |
for (const name of names) | |
db.query("INSERT INTO people (name) VALUES (:name)", {name}); | |
let res = db.query('select name,id from people', {}); | |
let r = res.columns(); | |
res.done(); | |
console.warn(r); | |
// Print out data in table | |
for (const [name] of db.query("SELECT name FROM people", {})) | |
console.log(name); | |
// Save and close connection | |
await save(db); | |
db.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment