当前位置 : 主页 > 手机开发 > cordova >

Cordova / Sqlite外键

来源:互联网 收集:自由互联 发布时间:2021-06-10
我试图使用Cordova和sqlite运行一个简单的例子 这是我的javascript代码` var dbName = "PresentationDB";var dbVersion = "1.0";var bdSize = 100000;var displayName = "TTPresentationDB";var db = null;var app = {initialize: funct
我试图使用Cordova和sqlite运行一个简单的例子

这是我的javascript代码`

var dbName = "PresentationDB";
var dbVersion = "1.0";
var bdSize = 100000;
var displayName = "TTPresentationDB";
var db = null;

var app = {


initialize: function() {
    this.bindEvents();
},
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    db = createdb();
    alert(db);
    db.transaction(populatedb,errorCallBack,successCallBack);

 },
// Update DOM on a Received Event
receivedEvent: function(id) {

    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, FOREIGN KEY(id_page) REFERENCES PAGE (id_page))');
tx.executeSql('CREATE TABLE IF NOT EXISTS ARTICLE (id_article INTEGER PRIMARY 
   KEY AUTOINCREMENT, 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);

}

`

添加外键时的行导致错误,有没有办法为SQLITE和Cordova创建关系数据库?

外键也应该定义为属性.

CREATE TABLE IF NOT EXISTS CONTENT (id_content INTEGER PRIMARY
   KEY AUTOINCREMENT,id_page INTEGER, FOREIGN KEY(id_page) REFERENCES PAGE (id_page))'

适用于Web SQL和Sqlite插件.

网友评论