未创建数据库表,也不可插入Cordova

时间:2018-10-05 13:34:36

标签: javascript sqlite cordova

我正在用Visual Studio构建一个Cordova应用程序,这需要数据库操作。我遇到的问题是我的脚本未创建我的数据库表,正在创建我的数据库,但未创建表,因此我的INSERT语句未执行。当我在数据库上运行adb pull时,它在数据库浏览器中打开时返回零字节,没有创建任何表或数据。我有一个外部js脚本,用于处理我的“ Index.html”中引用的数据库功能“ dbhandler.js”。感谢您的帮助,我不确定我缺少什么。我也安装了SQlite。

Index.js

(function () {
"use strict";
document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );
function onDeviceReady() {
    // Handle the Cordova pause and resume events
    document.addEventListener( 'pause', onPause.bind( this ), false );
    document.addEventListener('resume', onResume.bind(this), false);

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    var parentElement = document.getElementById('deviceready');
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

   $(document).on('pageshow', '#stocktaking', function () {
    var tbl = "<table cellspacing='0' cellspacing='0' class='table table-condensed table-striped' id='stocktakingtbl1'>" +
        "<thead>" +
        "<tr>" +
        "<th>Vessel</th>" +
        "<th>Ullage(INS)</th>" +
        "<th>Vol. One</th>" +
        "<th>Vol. Two</th>" +
        "<th>Brix</th>" +
        "<th>Tonnes Brix</th>" +
        "<th>Purity</th>" +
        "<th>Tonnes Brix</th>" +
        "<th>Tonnes Pol</th>" +
        "</tr>" +
        "</thead>" +
        "<tbody" +
        "<tr>" +
        "<td >CLARIFIER</td>" +  // CLARIFIER R1
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "<td><input type='number' id='data'></td>" +
        "</tr>" +
        "<tr>" +
        "<td>EVAPORATOR</td>" + // EVAPORATOR R1
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "<td><input type='number'></td>" +
        "</tr>" +
        "</tbody" +
        "</table>";
      $("#stocktakingtabdiv1").append(tbl);
        var array = [[],[]];
        $("#stocktakingtbl1 > tbody > tr").each(function (index) {
            array[index] = [];
            $('td:not(:first-child)', this).each(function (index2,element) {
                var value = $(this).find("input[type=number]").val();
                array[index][index2] = value;
            });                
        });
        for (var i = 0; i < array.length; i++) {
            StockTakingInsert(array[i][0], array[i][1], array[i][2], array[i][3], array[i][4], array[i][5], array[i][6], array[i][7]);
        }
});
} )();

DBHandler.js

var _db = window.sqlitePlugin.openDatabase({ name: 'FIS.db', location: 'default', androidDatabaseImplementation: 2 });
function CreateDbTables() {
_db.transaction(function (tx) {
    //tx.executeSql("DROP TABLE IF EXISTS StockTaking1", errorHandler);
    tx.executeSql("CREATE TABLE IF NOT EXISTS [StockTaking1] ([Id] integer PRIMARY KEY AUTOINCREMENT NOT NULL, [Ullage] integer, [Volume] integer, [VolumeInM] integer, [Brix] integer, [TonnesBrix] integer, [Purity] integer, [TonnesBrixInM] integer, [TonnesPol] integer)", [],
        function (tx, result) {
            alert("Table created successfully");
        },
        function (error) {
            alert("Error occurred while creating the table.");
        });
});
}

function StockTakingInsert(ullage, vol, volm, brix, tbrix, purity, tbrixm, tpols) {
_db.transaction(function (tx) {
    var executeQuery = "INSERT INTO StockTaking1 (Ullage,Volume,VolumeInM,Brix,TonnesBrix,Purity,TonnesBrixInM,TonnesPol) VALUES (?,?,?,?,?,?,?,?)";
    tx.executeSql(executeQuery, [ullage, vol, volm, brix, tbrix, purity, tbrixm, tpols]
        , function (tx, result) {
            alert('Inserted');
        },
        function (error) {
            alert('Error occurred');
        });
});
}

function errorHandler(transaction, error) {
alert('Oops. Error was ' + error.message + ' (Code ' + error.code + ')');
return false;
};

0 个答案:

没有答案