I am trying to implement a simple example using Cordova and sqlite Database
here is my js code:
var dbName = "PresentationDB";
var dbVersion = "1.0";
var bdSize = 100000;
var displayName = "TTPresentationDB";
var db = null;
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
db = createdb();
alert("DB Created");
db.transaction(populatedb,errorCallBack,successCallBack);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
//var parentElement = document.getElementById(id);
//var listeningElement = parentElement.querySelector('.listening');
//var receivedElement = parentElement.querySelector('.received');
//listeningElement.setAttribute('style', 'display:none;');
//receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
function createdb(){
if (!window.openDatabase)
{
alert("Your browser does not support openDatabase");
}
alert("creating db..");
return openDatabase(dbName, dbVersion, displayName, bdSize);
}
function populatedb(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS PAGE (id_page INT AUTO_INCREMENT
PRIMARY KEY,title VARCHAR(255), description TEXT, num_visits DATE,
last_visited DATE)');
tx.executeSql('CREATE TABLE IF NOT EXISTS CONTENT (id_content INT
AUTO_INCREMENT PRIMARY KEY, FOREIGN KEY(id_page) REFERENCES PAGE(id_page))');
tx.executeSql('CREATE TABLE IF NOT EXISTS ARTICLE (id_article INT
AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), text TEXT, description TEXT,
FOREIGN KEY(id_page) REFERENCES PAGE(id_page))');
tx.executeSql('INSERT INTO PAGE (title, description, num_visits, last_visited)
VALUES ("First Page", "Test Page", CURRENT_TIME, CURRENT_TIME)');
tx.executeSql('INSERT INTO CONTENT (id_page) VALUES (1)');
tx.executeSql('INSERT INTO ARTICLE (title, text, description, id_page) VALUES
("Article 1 Page 1", "Test Article", 1)');
tx.executeSql('INSERT INTO ARTICLE (title, text, description, id_page) VALUES
("Article 2 Page 1", "Test Article", 1)');
}
function successCallBack()
{
alert("Sucess");
//alert("Returned rows = " + results.rows.length);
}
function errorCallBack(tx, err)
{
alert("Error : " + err );
}
function queryDB(tx)
{
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
The lines when there is AUTO_INCREMENT and THOSE where i try to insert Data into the Data Tables are causing this undefined error
Any Help ?
Aucun commentaire:
Enregistrer un commentaire