I want to update or insert data in my database. If user is exists, update data. If not exists insert data.
That function is Database.getUser()
if (results.rows.length > 0) {
this.updateData(results.rows[0].id, lastseen);
} else {
this.saveData(username, lastseen);
}
this code doesn't work. I get error like this.
Uncaught TypeError: this.saveData is not a function
Uncaught TypeError: this.updateData is not a function
How can i access another method in my Database.getUser() method?
var Database = function() {
var mydb = false;
}
Database.prototype.initDB = function() {
try {
if (!window.openDatabase) {
console.log('not supported');
} else {
var shortName = 'tbl_xyz';
var version = '1.0';
var displayName = 'X Y Z';
var maxSize = 2 * 1024 * 1024;
this.mydb = openDatabase(shortName, version, displayName, maxSize);
}
console.log('Init DB');
} catch (e) {
console.log(e);
}
}
Database.prototype.createTable = function() {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql("CREATE TABLE IF NOT EXISTS friends (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username VARCHAR(50), lastseen TIMESTAMP);", [], this.nullDataHandler, this.errorHandler);
});
console.log('Create Table');
} catch (e) {
console.log(e);
}
}
Database.prototype.errorHandler = function(transaction, error) {
alert("Error processing SQL: " + error);
return true;
}
Database.prototype.nullDataHandler = function(transaction, results) {}
Database.prototype.saveData = function(username, lastseen) {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql("INSERT INTO friends (username, lastseen) VALUES (?,?)", [username, lastseen], this.nullDataHandler, this.errorHandler);
});
console.log('Save Data');
} catch (e) {
alert("Error processing SQL: " + e.message);
return;
}
}
Database.prototype.updateData = function(id, lastseen) {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql("UPDATE friends SET lastseen = ? WHERE id = ?", [lastseen, id], this.nullDataHandler, this.errorHandler);
});
console.log('Update Data');
} catch (e) {
alert("Error processing SQL: " + e.message);
return;
}
}
Database.prototype.getUser = function(username, lastseen) {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql("SELECT * FROM friends WHERE username = ?", [username], function(transaction, results) {
if (results.rows.length > 0) {
this.updateData(results.rows[0].id, lastseen);
} else {
this.saveData(username, lastseen);
}
});
});
} catch (e) {
alert(e.message);
}
}
Database.prototype.loadData = function() {
try {
this.mydb.transaction(function(transaction) {
transaction.executeSql("SELECT * FROM friends ORDER BY lastseen DESC", [], function(transaction, results) {
console.log('load OK');
console.log(results);
for (var i in results.rows) {
var row = results.rows.item(i);
console.log('username : ' + row.username + " | lastseen : " + row.lastseen);
}
});
console.log('load Data');
});
} catch (e) {
alert(e.message);
}
}
EDIT
I change like this and it's work. Is this right?
Database.prototype.getUser = function(username, lastseen) {
try {
var my_this = this;
this.mydb.transaction(
function(transaction) {
transaction.executeSql("SELECT * FROM friends WHERE username = ?", [username], function(transaction, results) {
if (results.rows.length > 0){
my_this.updateData(results.rows[0].id, lastseen);
}else{
my_this.saveData(username, lastseen);
}
});
});
} catch(e) {
alert(e.message);
}
}