Last active
March 19, 2024 22:35
-
-
Save defndaines/58277fb975475ac2f522a2b1f1b849c8 to your computer and use it in GitHub Desktop.
Attempt to Use SQLite3 from ClojureScript
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
;; Attempting to access an SQLite DB from Loom. | |
;; SQLite3 is installed and npm installed. | |
(def sqlite (js/require "sqlite3")) | |
(defn on-error [err] | |
(when err | |
(js/console.error err.message))) | |
;; Existing DB with 8 test records. | |
(def db | |
(new sqlite.Database "./resources/public/db/database.db" sqlite.OPEN_READONLY on-error)) | |
(def id "901680220") | |
(defn query-handler [err, row] | |
(if err | |
(js/console.error err.message) | |
(js/console.error row))) | |
(.get db "SELECT last_name, first_name FROM voters WHERE id = ?" [id] query-handler) | |
;; => SQLITE_RANGE: bind or column index out of range | |
;; SOLUTION: | |
(.get db "SELECT last_name, first_name FROM voters WHERE id = ?" #js[id] query-handler) | |
=> { last_name: 'Heaton', first_name: 'Carl' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another useful post related to this (tip from Mike Fikes): Checked Array Access
This is important for getting data out of the results, using
((goog.object/get result "last_name")