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
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.