Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Using SQLite database with Javascript

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #175965
    Ilan Firsov
    Participant

    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…

    #176158
    Ilan Firsov
    Participant

    Trying 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(...);
                ... 
            }
        }
    #176162
    JacobPeters
    Participant

    I ran across this while researching asm.js for a project. I thought it might be useful to you.

    https://github.com/kripken/sql.js

    #176881
    Ilan Firsov
    Participant

    Thanks 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

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.