Resources:
http://docs.phonegap.com/en/2.9.0/cordova_storage_storage.md.html#database_version
Device/Framework Info:
Nexus 4 - Android 4.2.2
Phonegap 2.9.0
Also using the bootstrap library for UI
I have followed the PhoneGap tutorial for version 2.9 on creating, and managing a database. So far I have found little to no places where people are talking about the specific error 23 when trying to write to a database.
I get this error after trying to write to a database after the insertBtn's click function, which executes the insertTemplate function.
The same thing happens when trying to delete a row when the deleteBtn event is fired, which executes the deleteTemplate function.
Any recommendations?
Controller File:
var databaseName = "blackbriar";
var version = 1;
var displayName = "lctv";
var size = 2097152; // two megabytes
var db = null;
$(document).ready(function(event){
// Gets shell for database
db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDb, errorCb, successCb);
db.transaction(loadTemplates, errorCb, successCb);
$('#insertBtn').click(function(event){
db.transaction(insertTemplate, errorCb, successCb);
});
$('#back').click(function(event){
$('#templates').show();
$('#templateEdit').hide();
$('#back').hide();
});
$('#deleteBtn').click(function(event){
db.transaction(deleteTemplate, errorCb, successCb);
$('#back').click();
});
});
$(document).on('click', '.pill', function(event){
// If window width is greater than 480px, isMobile variable is false
var isMobile = $(window).width() > 480 ? false : true;
$('#currentId').text($(this).attr('tempid'));
db.transaction(getTemplateById, errorCb, successCb);
if($('#currentId').text() == "-1"){
$('#insertBtn').show();
$('#saveBtn').hide();
$('#deleteBtn').hide();
$('#clearBtn').show();
} else {
$('#insertBtn').hide();
$('#saveBtn').show();
$('#deleteBtn').show();
$('#clearBtn').hide();
}
// Checks if pill is active for UI changes
if($(this).hasClass('active')){
$(this).removeClass('active');
if(!isMobile){$(this).find('.temp').attr('style', 'color: black;');}
} else {
$('.pill').removeClass('active');
$('.pill').find('.temp').attr('style', 'color: black;');
$(this).addClass('active');
if(!isMobile){$(this).find('.temp').attr('style', 'color: white;');}
}
if(isMobile) {
$('#templates').hide();
$('#templateEdit').show();
$('#back').show();
}
});
My function file:
function getShell(databaseName, version, displayName, size) {
return window.openDatabase(databaseName, version, displayName, size);
}
function populateDb(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS templates (id INTEGER PRIMARY KEY AUTOINCREMENT, templateName, description, campus, account, department, programName, projectId, taskId)');
}
function errorCb(error) {
alert("Error processing SQL: "+error.code);
}
function successCb() {
alert("Success!");
}
function insertTemplate(tx) {
var templateName = $('#templateName').val();
var description = $('#descriptionField').val();
var campus = $('#campusField').val();
var account = $('#accountField').val();
var department = $('#departmentField').val();
var programName = $('#programNameField').val();
var projectId = $('#projectIdField').val();
var taskId = $('#taskIdField').val();
tx.executeSql('INSERT INTO templates (templateName, description, campus, account, department, programName, projectId, taskId) VALUES ('
+'"'+templateName+'"'+', '
+'"'+description+'"'+', '
+'"'+campus+'"'+', '
+'"'+account+'"'+', '
+'"'+department+'"'+', '
+'"'+programName+'"'+', '
+'"'+projectId+'"'+', '
+'"'+taskId+'"'
+')');
}
function loadTemplates(tx) {
tx.executeSql('SELECT * from templates', [], templatesSuccess, errorCb);
}
function templatesSuccess(tx, results) {
var length = results.rows.length;
$('#templateUl').html('');
$('#templateUl').append('<li class="templi"><a class="pill" tempid="-1"><text class="temp"><i style="color: green;" class="icon-plus"></i> Create</text></a></li>');
for(var i = 0; i < length; i++) {
$('#templateUl').append('<li class="templi"><a class="pill" tempid="'+results.rows.item(i).id+'"><text class="temp">'+results.rows.item(i).templateName+'</text></a></li>');
}
}
function getTemplateById(tx) {
var currentId = $('#currentId').text();
tx.executeSql('SELECT * FROM templates WHERE id = '+currentId, [], loadTemplateSuccess, errorCb);
}
function loadTemplateSuccess(tx, results) {
if(results.rows.length > 0){
var template = results.rows.item(0);
var templateName = $('#templateName').val(template.templateName);
var description = $('#descriptionField').val(template.description);
var campus = $('#campusField').val(template.campus);
var account = $('#accountField').val(template.account);
var department = $('#departmentField').val(template.department);
var programName = $('#programNameField').val(template.programName);
var projectId = $('#projectIdField').val(template.projectId);
var taskId = $('#taskIdField').val(template.taskId);
} else {
var templateName = $('#templateName').val('');
var description = $('#descriptionField').val('');
var campus = $('#campusField').val('');
var account = $('#accountField').val('');
var department = $('#departmentField').val('');
var programName = $('#programNameField').val('');
var projectId = $('#projectIdField').val('');
var taskId = $('#taskIdField').val('');
}
}
function deleteTemplate(tx) {
var currentId = $('#currentId').text();
alert(currentId);
tx.executeSql('DELETE FROM templates', [], templatesSuccess, errorCb);
alert("Authorize!!!");
}
Just came into this, i had to uninstall the app and re made it (Also changed the version but i dont think that did it).
Followed this so i uninstall the app everytime i debug. This can be annoying if you have to insert items each time, but atleast solved it.
Related
I'm making a game with socket.io and nodejs, and I'm making a module called rooms.js, this module require users.js module and fiveSocket.js module
but when I call Rooms.New from the main server file, it says that fiveSocket is undefined, same problem when Rooms.New calls a users.js function, I got TypeError: Cannot read property 'getSocketIDbyId' of undefined
rooms.js:
var mysql = require('../mysql/mysql.js');
var headers = require('./headers.js');
var users = require('./users.js');
var fiveSocket = require('./sockets.js');
var Rooms = {
Obj: {},
Room: function(data) {
var room = this;
this.name = data.name;
this.users = [];
this.floorCode = data.floor;
this.description = data.desc;
this.maxUsers = data.maxUsers;
this.owner = data.owner;
this.setTime = new Date().getTime();
this.dbID = data.dbID;
this.doorx = data.doorx;
this.doory = data.doory;
this.doordir = data.doordir;
},
New: function(socketID, roomID) {
var keys = Object.keys(Rooms.Obj).length;
var id = keys + 1;
var callback = function(row) {
fiveSocket.emitClient(socketID, headers.roomData, {
title: row.title,
desc: row.description,
mapStr: row.floorCode,
doorx: row.doorx,
doory: row.doory,
doordir: row.doordir
});
var uid = users.getIdBySocketID(socketID);
users.Obj[uid].curRoom = roomID;
var rid = Rooms.getIdByDbID(roomID);
Rooms.Obj[rid].users.push(uid);
}
if(Rooms.getIdByDbID(roomID) != false) {
var room = Rooms.getIdByDbID(roomID);
var row = { title: room.name, description: room.description, floorCode: room.foorCode, doorx: room.doorx, doory: room.doory, doordir: room.doordir };
callback(row);
} else {
mysql.Query('SELECT * FROM rooms WHERE id = ? LIMIT 1', roomID, function(rows) {
if(rows.length > 0) {
var row = rows[0];
Rooms.Obj[id] = new Rooms.Room({name: row.title, floorCode: row.floorCode, desc: row.description, maxUsers: row.maxUsers, owner: row.owner, dbID: row.id, doorx: row.doorx, doory: row.doory, doordir: row.doordir});
callback(row);
}
});
}
},
removeUser: function(DBroomID, userID) {
var rid = Rooms.getIdByDbID(DBroomID);
var room = Rooms.Obj[rid];
var index = room.indexOf(userID);
if (index > -1) array.splice(index, 1);
},
Listener: function(users) {
setInterval(function(){
for(var roomID in Rooms.Obj) {
var room = Rooms.Obj[roomID];
// send users coordinates
room.users.forEach(function(uid) {
var socketID = users.getSocketIDbyId(uid);
var data = Rooms.getUsersInRoomData(roomID);
fiveSocket.emitClient(socketID, headers.roomUsers, data);
});
// unload inactive rooms (no users after 10 seconds)
var activeUsers = room.users.length;
var timestamp = room.setTime;
var t = new Date(); t.setSeconds(t.getSeconds() + 10);
var time2 = t.getTime();
if(activeUsers <= 0 && timestamp < time2) {
Rooms.Remove(roomID);
}
}
}, 1);
},
getUsersInRoomData: function(roomID) {
var room = Rooms.Obj[roomID];
var obj = {};
room.users.forEach(function(uid) {
var user = users.Obj[uid];
obj[uid] = {
username: user.username,
position: user.position,
figure: user.figure
};
});
return obj;
},
Remove: function(id) {
delete Rooms.Obj[id];
},
getIdByDbID: function(dbID) {
var result = null;
for(var room in Rooms.Obj) {
var u = Rooms.Obj[room];
if(u.dbID == dbID) var result = room;
}
if(result == null) return false;
else return result;
},
getDbIDbyId: function(id) {
return Rooms.Obj[id].dbID;
}
}
Rooms.Listener();
module.exports = Rooms;
EDIT: (if it can be helpful)
When I console.log fiveSocket on the main file
When I console.log fiveSocket on the rooms.js file
EDIT2: When I've removed var users = require('./users.js'); from fiveSocket, when I console.log it in rooms.js it works, why ?
EDIT3: I still have the problem
If you need the others modules sources:
Users.JS: http://pastebin.com/Ynq9Qvi7
sockets.JS http://pastebin.com/wpmbKeAA
"Rooms" requires "Users" and vice versa, so you are trying to perform "circular dependency".
Quick search for node.js require circular dependencies gives a lot of stuff, for example :
"Circular Dependencies in modules can be tricky, and hard to debug in
node.js. If module A requires('B') before it has finished setting up
it's exports, and then module B requires('A'), it will get back an
empty object instead what A may have intended to export. It makes
logical sense that if the export of A wasn't setup, requiring it in B
results in an empty export object. All the same, it can be a pain to
debug, and not inherently obvious to developers used to having those
circular dependencies handled automatically. Fortunately, there are
rather simple approaches to resolving the issue."
or
How to deal with cyclic dependencies in Node.js
I am trying to add a contact in a table. The logic is simple , if the contact already exist confirm modal will ask for confirmation for replacement. Otherwise the contact will be inserted as a new contact.
The addContact() method is called from a html page with onClick event .
The problem is newContact() is called but it's not doing anything , what is the problem ?
function addContact() {
var conName = document.getElementById("conName").value;
var conNumber = document.getElementById("conNumber").value;
newContact(conName, conNumber);
}
function newContact(conName, conNumber) {
var db = window.openDatabase("myDB", "1.0", "myDB", 200000);
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM contact", [], function(tx, rs) {
var contactAlreadyExist=false;
for (var i = 0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);
if(row['name']==conName){
if (confirm("Want to replace contact " + conName + "?")) {
var db = window.openDatabase("qpdio", "1.0", "QPDIO", 200000);
db.transaction(function(tx) {
var sql = "Update contact set number="+conNumber+" WHERE ID="+row['id'];
tx.executeSql(sql);
contactAlreadyExist=true;
return true;
});
}
}
}
});
if(!contactAlreadyExist){
tx.executeSql("INSERT INTO contact (name, number) VALUES ('" + conName + "','" + conNumber + "')");
console.log("Here");
}
},errorAddingContact, successAddingContact);
}
function errorAddingContact(err) {
navigator.notification.alert("Error Adding Contact " + err.code, null,
"Error", "Ok");
}
function successAddingContact() {
navigator.notification.alert("Contact Saved successfully!", null,
"Information", "Ok");
}
Can you try this? And let me know if there are errors? I'll edit this answer accordingly. Also if you can check the code alignment (tabs/spaces especially for lines with brackets), it would be good as I think it's a bit messy, maybe due to copy-paste
Note that
I am not a phonegap expert
The error and success call back now have alerts
executeSql now has 4 parameters (including error callback), yours seemed to have 5, but this might be because of the code alignment makes me misread the brackets
confirm doesn't need if, does it? Please check the API
note that the location of contactAlreadyExist check is now still within the successCallback of executeSQL
Code
function newContact(conName, conNumber) {
var db = window.openDatabase("myDB", "1.0", "myDB", 200000);
db.transaction(
function(tx) {
tx.executeSql
(
"SELECT * FROM contact",
[],
function(tx, rs) {
var contactAlreadyExist=false;
for (var i = 0; i < rs.rows.length; i++)
{
var row = rs.rows.item(i);
if(row['name']==conName)
{
confirm
(
"Want to replace contact " + conName + "?",
function()
{
var db = window.openDatabase("qpdio", "1.0", "QPDIO", 200000);
db.transaction(function(tx) {
var sql = "Update contact set number="+conNumber+" WHERE ID="+row['id'];
tx.executeSql(sql);
contactAlreadyExist=true;
return true;
});
}
);
}
}
if (!contactAlreadyExist)
{
tx.executeSql("INSERT INTO contact (name, number) VALUES ('" + conName + "','" + conNumber + "')");
console.log("Here");
}
},
errorAddingContact
);
}
,errorAddingContact, successAddingContact);
}
function errorAddingContact(err) {
alert(err.code);
navigator.notification.alert("Error Adding Contact " + err.code, null,
"Error", "Ok");
}
function successAddingContact() {
alert(err.code);
navigator.notification.alert("Contact Saved successfully!", null,
"Information", "Ok");
}
i'm trying to retrieve the data from the database for web application using webSQL ,but i'm unable to get the data from database. I'm very new to this. I tried like this
var DB_NAME = "database";
var DB_VERSION = "";
var DB_TITLE = "";
var DB_BYTES = 50 * 1024 * 1024;
var db = openDatabase(DB_NAME, DB_VERSION, DB_TITLE, DB_BYTES);
//Retrieve Rows from Table
db.transaction(
function(tx) {
tx.executeSql("SELECT * FROM Data;",
[],
function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
alert(results.rows.item(i).text);
}
});
});
Thanks in Advance.
This is how i have done.. and this is working for me.
// global variables
var db;
var shortName = 'Books';
var version = '1.0';
var displayName = 'BooksDB';
var maxSize = 200000;//65535;
function ListDBValues() {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
// this line tries to open the database base locally on the device if it does not exist, it will create it and return a database object stored in variable db
db = openDatabase(shortName, version, displayName,maxSize);
// this line clears out any content in the #lbUsers element on the page so that the next few lines will show updated content and not just keep repeating lines
$('#lbUsers').html('');
// this next section will select all the content from the User table and then go through it row by row appending the UserId FirstName LastName to the #lbUsers element on the page
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM books;', [], function(transaction, result) { if (result != null && result.rows != null) { for (var i = 0; i < result.rows.length; i++) { var row = result.rows.item(i); $('#lbUsers').append('<br>' + row.book_title + '. ' + row.book_isbn+ ' ' + row.book_price); } } },errorHandler); },errorHandler,nullHandler);
return;
alert('in list end');
}
// this is called when a successful transaction happens
function successCallBack() {
alert("DEBUGGING: success");
}
function nullHandler(){
alert('null handler');
};
I'm unable to find a definitive answer for this, asynchronous web sql is really tripping me up. How does one execute a SELECT query using data based on a parent SELECT query? Here's a snippet I thought was working but really isn't:
db.transaction(function(tx){
tx.executeSql('SELECT a, b FROM mytable WHERE c=5', [], function(tx,results) {
var i = results.rows.length;
while(i--){
var votes = results.rows.item(i).a;
var marker = results.rows.item(i).b;
tx.executeSql('SELECT a FROM mytable WHERE c=?', [marker], function(tx,aresults) {
if(!aresults){
//do something
} else {
myScore += (votes*100)/aresults.rows.item(0).a;
}
}(marker));
}
nextFunction();
});
}, function(){onError});
Try this:
function firstQuery(){
db.transaction(function(tx){
tx.executeSql('SELECT a, b FROM mytable WHERE c=5', [],
function(tx,results) {
var i = results.rows.length;
while(i--){
secondQuery(results.rows[i]);
}
});
}, function(){onError});
}
function secondQuery(responseQuery){
var votes = responseQuery.a;
var marker = responseQuery.b;
db.transaction(function(tx){
tx.executeSql('SELECT a FROM mytable WHERE c=?', [marker], function(tx,aresults) {
if(!aresults){
//do something
} else {
myScore += (votes*100)/aresults.rows.item(0).a;
}
}(marker));
}, function(){onError});
}
There's a lot of code here, but the question has to do with the for loop at the bottom.
var dbo = openDatabase('xxx','1.0','myDatabase', 1048576);
var DropTableDeferred = new $.Deferred();
var CreateTableDeferred = new $.Deferred();
var InsertDeferred = new $.Deferred();
var SelectDeferred = new $.Deferred();
dbo.transaction(function(myTrans) {
myTrans.executeSql(
'drop table myTable;'
,[]
,DropTableDeferred.resolve()
);
});
DropTableDeferred.done(function() {
dbo.transaction(function(myTrans) {
myTrans.executeSql(
'CREATE TABLE IF NOT EXISTS myTable'
+ '(xxxID Integer NOT NULL PRIMARY KEY'
+ ',xxxName Varchar(128)'
+ ');'
,[]
,CreateTableDeferred.resolve()
);
});
});
CreateTableDeferred.done(function() {
dbo.transaction(function(myTrans) {
myTrans.executeSql("INSERT INTO myTable(xxxID,xxxName) VALUES(1,'A')");
myTrans.executeSql("INSERT INTO myTable(xxxID,xxxName) VALUES(2,'B')");
myTrans.executeSql(
"INSERT INTO myTable(xxxID,xxxName) VALUES(3,'C')",
[],
InsertDeferred.resolve()
);
});
});
InsertDeferred.done(function() {
dbo.transaction(function(myTrans) {
myTrans.executeSql(
'SELECT * FROM myTable',
[],
function(tx, result) {
SelectDeferred.resolve(result);
}
);
});
});
SelectDeferred.done(function(result) {
var X = $('#result-template').html();
var template = Handlebars.compile(X);
var data = [];
for(var i=0;i < result.rows.length; i++) {
data.push(result.rows.item(i));
}
$('ul').append(template(data));
});
Q: Do I need to build a data array in order to call template(data), or can I pass the result variable directly?
And by 'result variable', what I mean is: result.rows, or result.rows.item, or some other combination.
Yes the value has passed directly in my html&javascript only project. In the code below, I get the results of the Sql and filling a dropdown box's options.
function fillLectureFromDB(tx) {
tx.executeSql('SELECT * FROM LECTURE', [], successFill, errorFill);
}
function successFill(tx, results) {
var len = results.rows.length;
for (var i=0; i<len; i++){
var elOptNew = document.createElement('option');
elOptNew.text = results.rows.item(i).code;
elOptNew.value = results.rows.item(i).code;
var elSel = document.getElementById('slExCode');
elSel.add(elOptNew, null);
}
}
IMHO you can, but to tell you the truth, it would've taken less time for you to try it out then to post the question. Just try:
$('ul').append(template(result.rows));