Home › Forums › JavaScript › Using SQLite database with Javascript
- This topic is empty.
-
AuthorPosts
-
July 21, 2014 at 12:05 am #175965Ilan FirsovParticipant
Is it possible to use an existing SQLite databse with Javascript?
I have a database ready prefilled with data that I need to load with Javascript, I tried using JSON but it’s super slow processing the data (around 2K records for one table and 200-ish for a second table) and it requires me to do some weird loops if I want to query the data
I couldn’t find anything about that, found only articles about IndexedDB and WebSQL but that requires me to create the DB on load, not load existing one from a file.*This is for a Chrome extension and Chrome auto updates to latest version, so I don’t really care for browser support…
July 22, 2014 at 6:29 am #176158Ilan FirsovParticipantTrying my luck with IndexedDB.
I can create a database and a table (object store) in it, get data from a json file (exported from the SQLite file), and load said data to a table. Rather simple and not really that slow (and it’s going to happen only on first install or specific upgrades so it’s not that bad :) )
But I just can’t figure out how to create multiple tables/object stores.
Only one of the object stores gets populated. It’s either the last one or the one which I don’t comment out.
Does anyone here have any experience with IndexedDB and can tell what am I doing wrong?var request = window.indexedDB.open("myDB", 1); request.onupgradeneeded = function (event) { var db = event.target.result; var alertAreasObjectStore, citiesDataObjectStore; //object store 1 alertAreasObjectStore = db.createObjectStore("alertareas", { keyPath : "cityAlertId", unique: true }); alertAreasObjectStore.createIndex("alertAreasAlertAreaIndex", "alertArea", { }); alertAreasObjectStore.transaction.oncomplete = function(event) { var obs = db.transaction("alertareas", "readwrite").objectStore("alertareas"); //loop - adding data obs.put(...); ... } //object store 2 citiesDataObjectStore = db.createObjectStore("cities", { autoIncrement: true }); citiesDataObjectStore.createIndex("citiesAlertAreaIndex", "alertArea", { }); citiesDataObjectStore.transaction.oncomplete = function(event) { var obs = db.transaction("cities", "readwrite").objectStore("cities"); //loop - adding data obs.put(...); ... } }
July 22, 2014 at 8:10 am #176162JacobPetersParticipantI ran across this while researching asm.js for a project. I thought it might be useful to you.
July 30, 2014 at 12:48 am #176881Ilan FirsovParticipantThanks for the link I’ll try that the next time I need to use a database in JS.
For this project I ended up using db.js. It makes using IndexedDB rather simple
And sorry for the really late response -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.