GithubHelp home page GithubHelp logo

Comments (5)

andpor avatar andpor commented on August 15, 2024

Hi Donald

avoid doing stuff on the db open status callback - wait for db instance to be returned and then invoke this.populateDatabase(db)

try removing this.setState(this.state); or recoding this stmt to that.setState(that.state); in populateDatabase.

var that = this is not there by accident

you can either do var that = this or use a bind(this)_ on the method.

from react-native-sqlite-storage.

andpor avatar andpor commented on August 15, 2024

otherwise the sample you sent is not complete..where is getService getting called from. What is the param to openDatabase for example?

from react-native-sqlite-storage.

andpor avatar andpor commented on August 15, 2024

This code "works" i.e. I see return values from getServices...

React Native is bad in terms of eating up bad references to this it seems...


openErrorCallback(message) {

},

populateDatabase(db){
    var that = this;

    db.executeSql('SELECT version_id FROM Version ORDER BY version_id DESC LIMIT 1', [], function(results) {
        console.log("Post version query (success)");
        var len = results.rows.length;
        for (let i = 0; i < len; i++) {
            let row = results.rows.item(i);
            database_current_version = row.version_id;
        }

        that.setState(that.state);

        db.transaction(that.upgradeTables, function() {
            console.log("upgradeTables failed");
        }, function() {
            console.log("upgradeTables success");
            that.getServices(() => {});
        });

    }, function() {
        console.log("Post version query (failed)");
        db.transaction(that.upgradeTables, function() {
            console.log("Upgrade failed");
        }, function() {
            console.log("Upgrade success");
        });
    });


},

upgradeTables(tx) {
    console.log("Database running version "+database_current_version);
    if(database_current_version < 1) {
        tx.executeSql("CREATE TABLE IF NOT EXISTS Version ( " +
            "version_id INTEGER PRIMARY KEY" +
            ");", []);
        tx.executeSql("CREATE TABLE IF NOT EXISTS Service (" +
            "id INTEGER PRIMARY KEY, " +
            "name TEXT" +
            ");", []);
        tx.executeSql("INSERT INTO Version (version_id) VALUES (?)", [1]);
    }
},

addService(callback, obj) {
    var that = this;
    db.transaction(tx).then(() => {
        callback(true);
    });
},

addAccountTransaction(tx, obj) {
    tx.executeSql("INSERT INTO Service (name) VALUES (?)", [obj.name]);
},

getServices(callback) {
    db.executeSql("SELECT id FROM Service", [], function(results) {
        console.log("Got service results "+JSON.stringify(results));
        var len = results.rows.length;
        console.log("len = "+len);
        for (var i = 0; i < len; i++) {
            let row = results.row.item(i);
            console.log("row "+i+" = "+row.id);
        }

        callback(results);
    });
},

executeQuery(query, params) {
    console.log("Executing query "+query);
    return db.executeSql(query, params);
},

executeQueryCallback(query, params, callbackSuccess, callbackFailure) {
    db.executeSql(query, params, callbackSuccess, callbackFailure);
},

executeTransaction(func, callback) {
    console.log("Executing transaction on AuthioDB");
    db.transaction(func).then((results) => {
        callback(true, results);
    }).catch((error) => {
        callback(false);
    });
},

getDbResource() {
    return db;
},


loadAndQueryDB(){
    var that = this;
    that.state.progress.push("Opening database ...");
    that.setState(that.state);
    //database_ready_callback = readyCallback;
    db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, function() {
        console.log("db opened");
    }, that.openErrorCallback);
    this.populateDatabase(db);
},

from react-native-sqlite-storage.

donald-jackson avatar donald-jackson commented on August 15, 2024

Hi Andrzej,

Thanks for taking the time to help. It appears I have misunderstood the purpose of the open callbacks. I thought that the entire nature of this module was asynchronous - ie: you can only use once the open callback has executed, which is why I was doing things in there.

I'll try it again now by just using the populate/etc methods outside of the callback - because my entire structure was basically executing in a chained callback.

Thanks again.

from react-native-sqlite-storage.

shanitiwari01 avatar shanitiwari01 commented on August 15, 2024

I follow the below blog to integrate SQLite storage on this blog provided ExecuteQuery function and how to use it because based on promise it handles multiple select queries.

`/**

  • Execute sql queries
  • @param sql
  • @param params
  • @returns {resolve} results
    */
    ExecuteQuery = (sql, params = []) => new Promise((resolve, reject) => {
    db.transaction((trans) => {
    trans.executeSql(sql, params, (trans, results) => {
    resolve(results);
    },
    (error) => {
    reject(error);
    });
    });
    });`

https://infinitbility.com/react-native-sqlite-storage-examples-of-query

from react-native-sqlite-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.