Forums

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

Home Forums JavaScript Storing and Saving a Variable Inside an Embedded Function

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #28657
    BaylorRae
    Member

    I’m working with HTML5 databases, and I’m trying to add an auto increment function to a class I’m making. I can get the highest id from my table with SQL and get the next highest id after that, but I can’t save the value.

    I’ve tried to return the new id, but the main function won’t return the value from the function inside of the main function.

    Here’s the function by itself, and it is slightly out of context, but you should be able to understand how it works.

    Code:
    function next_id(table_name, column) {
    if( db ) {
    db.transaction(function(tx) {
    message(‘Getting next ‘ + column + ‘ from table ‘ + table_name + ‘‘);
    tx.executeSql(“SELECT ” + column + ” FROM ” + table_name + ” ORDER BY ” + column + ” DESC LIMIT 1″, [], function(tx, results) {
    highest_id = ++results.rows.item(0).id;
    return highest_id;
    });
    });
    }else {
    message(‘Please initialize a database before inserting a row’);
    }
    }

    The variable "highest_id" has been created at the beginning of the file before the main object/class was created. And I can set the value and run a console.log inside the function after I store it and get the new value, but if I try to run the function var new_id = database.next_id(‘data’, ‘id’); and save the returned value, it doesn’t set an id.

    If you need to see more of the code, or if I haven’t explained something clearly, just let me know and I’ll try to clarify.

    – Baylor Rae’

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