Phonegap InsertID indefined

Go To StackoverFlow.com

0

I'm running into a little trouble and I am hoping some one out there can help.

I need to get the ID of a db record just inserted so that i can do a FK join with some other rows in other tables.

CREATE TABLE IF NOT EXISTS [Jobs] ( "ID" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "SourceID" guid,"Version" integer DEFAULT 0, "IsActive" bit DEFAULT 1, "LastUpdated" datetime DEFAULT (CURRENT_TIMESTAMP), "JobGPSLocation" nvarchar(100) COLLATE NOCASE)


    Job.prototype.Insert = function () {
        "use strict";
        try {
            console.log('Started: Job.prototype.Insert');
            console.log('ID of item been inserted ' + this.SourceID);
            var sqlcmd = 'INSERT INTO Jobs(ID,';
            sqlcmd += 'SourceID,';
sqlcmd += 'JobGPSLocation,';
            sqlcmd += 'IsActive , LastUpdated, Version) VALUES ';
            sqlcmd += '(NULL,';
            sqlcmd += '"' + this.SourceID + '",';
            sqlcmd += '"' + MakeDBSafe(this.JobGPSLocation) + '",';
            sqlcmd += this.IsActive + ',';
            sqlcmd += '"' + DateToSqlite(this.LastUpdated) + '",';
            sqlcmd += this.Version;
            sqlcmd += ')';
            console.log('B4 ClientDB.Transaction ' + sqlcmd);
            if ((ClientDB !== null) && (loggeduser !== null)) {
                var tempobj = this;
                ClientDB.transaction(function (tx) { JobInsert(tx, sqlcmd, tempobj); }, JobprototypeInsertErrorHandler);
            }
            else {
                console.log('Error: Job.prototype.Insert -- ClientDB And/Or loggeduser is null');
            }
        }
        catch (e) {
            console.log('Error: Engineer.prototype.Insert' + e);
        }
        function JobInsert(tx, sqlcmd, objref) {
            console.log('Started: JobInsert');
            console.log(sqlcmd);
            tx.executeSql(sqlcmd, [],
            function (transaction, results) {
                objref.ID = results.insertId;
                console.log('JobInsert insertId ' + results.insertId);
                console.log('JobInsert rowAffected ' + results.rowAffected);
                console.log('JobInsert rows ' + results.rows);
                console.log('JobInsert rows count ' + results.rows.length);

                for (var i = 0; i < results.rows.length; i++) {
                    for (prop in results.rows) {
                        str += prop + " value :" + obj[prop] + "\n"; //Concate prop and its value from object
                    }
                }

            }, JobprototypeInsertErrorHandler);
            console.log('Finished: JobInsert');
            console.log('JobInsert ID after insert ' + objref.ID);
        }
        var JobprototypeInsertErrorHandler = function (transaction, error) {
            console.log('JobprototypeInsertErrorHandler -- ' + error);
        };
    };

But the above code always returns

JobInsert insertId undefined

Any Help would be appreciated

Thanks in advance

Lmac

2012-04-04 07:58
by lmac34
The Table is been created and the records are been inserted just cannot get the id of the record inserted - lmac34 2012-04-04 08:01
Ok if this cannot be done can anyone recommend a different method? I need to inset a record and then use the id of this record in another table as the foreign key to this jo - lmac34 2012-04-04 11:38
Has this been resolved in a more elegant way I wonder - Jakob 2014-10-08 14:54


0

Called another sql statement to get the max id straight after the insert, not the most desired way but only way i could get it to work.

2012-04-12 07:32
by lmac34
Ads