Sqlite not working in hybrid app

时间:2016-04-04 18:39:15

标签: javascript html angularjs sqlite monaca

I'm developing a hybrid app using Monaca IDE, the thing is the database I'm using is made in Sqlite, and it works great in the preview, I even had another project with the same DB scheme and it worked great too, but when I try to make the .apk, the application works but the db is never created, I don't know if I should be doing something else that I'm not aware of, or if you guys have any idea of what might be going on here!

Note: It doesn't work either if I open the Monaca preview in firefox

this is my db code:

$(document).ready(function(){
     onDeviceReady();
});


function onDeviceReady() {
db = window.openDatabase('antel', '1.0', 'primera', 2 * 1024 * 1024);
db.transaction(crearBase);
};


function crearBase(tx) {
tx.executeSql('Create table if not exists PARADA (nombre TEXT PRIMARY KEY)', [], function (tx) {
    tx.executeSql('Create table if not exists REGISTRO (id INTEGER PRIMARY KEY autoincrement, nomParada TEXT REFERENCES PARADA (nombre), tipo TEXT ,km TEXT,fecha TEXT,hora TEXT)', [], function (tx) {
         tx.executeSql('Create table if not exists EMAIL (id INTEGER PRIMARY KEY autoincrement,nombre TEXT)', [], function (tx) {
            tx.executeSql('Select nombre FROM PARADA',[],meFijoSiEsVacia);});
    });
});

}

2 个答案:

答案 0 :(得分:1)

我不确定你是否需要像这样创建一个回调地狱。您应该考虑到您依赖于每个语句都很好并且没有错误处理。我没有进入Monaca - 所以不确定他们正在使用哪个cordova SQLite插件,但我查看了their documentation regarding SQLite

他们正在执行关于打开sql事务的所有语句:

function onDeviceReady() {
    var db = window.openDatabase('antel', '1.0', 'primera', 2 * 1024 * 1024);
    db.transaction(crearBase, errorCB, successCB);
};


function crearBase(tx) {
    tx.executeSql('Create table if not exists PARADA (nombre TEXT PRIMARY KEY)');
    tx.executeSql('Create table if not exists REGISTRO (id INTEGER PRIMARY KEY autoincrement, nomParada TEXT REFERENCES PARADA (nombre), tipo TEXT ,km TEXT,fecha TEXT,hora TEXT)');
    tx.executeSql('Create table if not exists EMAIL (id INTEGER PRIMARY KEY autoincrement,nombre TEXT)');
    tx.executeSql('Select nombre FROM PARADA');
}

function errorCB(err) {
    console.log("Error occured while executing SQL: "+err.code);
}

function successCB() {
    console.log("all fine");
}

您可能有理由将所有这些成功函数嵌套,但您还应该处理错误回调。只是陈述"预览工作,设备不"是不足够的。浏览器环境当然与Cordova插件不同。

答案 1 :(得分:0)

虽然Kerosene对于如何编写这些语句是对的,但我遇到的问题是我的Jquery输入,因为我有:

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

那样(我不知道为什么)它可以在预览中使用但不在设备中

当我将其更改为:

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>

工作得很好!,

谢谢Kerosene,你是唯一一个试图帮助的人!