I am trying to run a simple example using Cordova and sqlite
here is my javascript 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);
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 window.openDatabase(dbName, dbVersion, displayName, bdSize);
}
function populatedb(tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS PAGE (id_page INTEGER PRIMARY KEY
AUTOINCREMENT , title VARCHAR(255), description TEXT, num_visits DATE,
last_visited DATE)');
tx.executeSql('CREATE TABLE IF NOT EXISTS CONTENT (id_content INTEGER PRIMARY
KEY AUTOINCREMENT, id_page )');
tx.executeSql('CREATE TABLE IF NOT EXISTS ARTICLE (id_article INTEGER PRIMARY
KEY AUTOINCREMENT, title VARCHAR(255), text TEXT, description TEXT)');
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 i add a foreign key are causing the error, is there any way to create relational database for SQLITE and Cordova ?
Aucun commentaire:
Enregistrer un commentaire