Phonegap SQLite错误23未授权

时间:2013-07-09 14:01:58

标签: javascript android jquery sqlite cordova

资源:

http://docs.phonegap.com/en/2.9.0/cordova_storage_storage.md.html#database_version

设备/框架信息:

  • Nexus 4 - Android 4.2.2
  • Phonegap 2.9.0
  • 还使用UI的引导库

我已经关注创建和管理数据库的PhoneGap教程2.9版。到目前为止,在尝试写入数据库时​​,我发现很少有人在讨论特定的错误23。

在insertBtn的click函数执行insertTemplate函数后尝试写入数据库后,我收到此错误。

当触发deleteBtn事件时尝试删除行时会发生同样的事情,这会执行deleteTemplate函数。

有什么建议吗?

控制器文件:

var databaseName = "blackbriar";
var version = 1;
var displayName = "lctv";
var size = 2097152; // two megabytes
var db = null;

$(document).ready(function(event){
    // Gets shell for database
    db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    db.transaction(populateDb, errorCb, successCb);
    db.transaction(loadTemplates, errorCb, successCb);

    $('#insertBtn').click(function(event){
        db.transaction(insertTemplate, errorCb, successCb);
    });

    $('#back').click(function(event){
        $('#templates').show();
        $('#templateEdit').hide();
        $('#back').hide();
    });
    $('#deleteBtn').click(function(event){
        db.transaction(deleteTemplate, errorCb, successCb);
        $('#back').click();
    });
});

$(document).on('click', '.pill', function(event){
    // If window width is greater than 480px, isMobile variable is false
    var isMobile = $(window).width() > 480 ? false : true;

    $('#currentId').text($(this).attr('tempid'));
    db.transaction(getTemplateById, errorCb, successCb);

    if($('#currentId').text() == "-1"){
        $('#insertBtn').show();
        $('#saveBtn').hide();
        $('#deleteBtn').hide();
        $('#clearBtn').show();
    } else {
        $('#insertBtn').hide();
        $('#saveBtn').show();
        $('#deleteBtn').show();
        $('#clearBtn').hide();
    }

    // Checks if pill is active for UI changes
    if($(this).hasClass('active')){
        $(this).removeClass('active');
        if(!isMobile){$(this).find('.temp').attr('style', 'color: black;');}
    } else {
        $('.pill').removeClass('active');
        $('.pill').find('.temp').attr('style', 'color: black;');
        $(this).addClass('active');
        if(!isMobile){$(this).find('.temp').attr('style', 'color: white;');}
    }
    if(isMobile) {
        $('#templates').hide();
        $('#templateEdit').show();
        $('#back').show();
    }
});

我的功能文件:

function getShell(databaseName, version, displayName, size) {
    return window.openDatabase(databaseName, version, displayName, size);
}

function populateDb(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS templates (id INTEGER PRIMARY KEY AUTOINCREMENT, templateName, description, campus, account, department, programName, projectId, taskId)');
}

function errorCb(error) {
    alert("Error processing SQL: "+error.code);
}

function successCb() {
    alert("Success!");
}

function insertTemplate(tx) {

    var templateName = $('#templateName').val();
    var description = $('#descriptionField').val();
    var campus = $('#campusField').val();
    var account = $('#accountField').val();
    var department = $('#departmentField').val();
    var programName = $('#programNameField').val();
    var projectId = $('#projectIdField').val();
    var taskId = $('#taskIdField').val();

    tx.executeSql('INSERT INTO templates (templateName, description, campus, account, department, programName, projectId, taskId) VALUES ('
            +'"'+templateName+'"'+', '
            +'"'+description+'"'+', '
            +'"'+campus+'"'+', '
            +'"'+account+'"'+', '
            +'"'+department+'"'+', '
            +'"'+programName+'"'+', '
            +'"'+projectId+'"'+', '
            +'"'+taskId+'"'
            +')');
}

function loadTemplates(tx) {
    tx.executeSql('SELECT * from templates', [], templatesSuccess, errorCb);
}

function templatesSuccess(tx, results) {
    var length = results.rows.length;
    $('#templateUl').html('');
    $('#templateUl').append('<li class="templi"><a class="pill" tempid="-1"><text class="temp"><i style="color: green;" class="icon-plus"></i> Create</text></a></li>');
    for(var i = 0; i < length; i++) {
        $('#templateUl').append('<li class="templi"><a class="pill" tempid="'+results.rows.item(i).id+'"><text class="temp">'+results.rows.item(i).templateName+'</text></a></li>');
    }
}

function getTemplateById(tx) {
    var currentId = $('#currentId').text();
    tx.executeSql('SELECT * FROM templates WHERE id = '+currentId, [], loadTemplateSuccess, errorCb);
}

function loadTemplateSuccess(tx, results) {
    if(results.rows.length > 0){
        var template = results.rows.item(0);
        var templateName = $('#templateName').val(template.templateName);
        var description = $('#descriptionField').val(template.description);
        var campus = $('#campusField').val(template.campus);
        var account = $('#accountField').val(template.account);
        var department = $('#departmentField').val(template.department);
        var programName = $('#programNameField').val(template.programName);
        var projectId = $('#projectIdField').val(template.projectId);
        var taskId = $('#taskIdField').val(template.taskId);
    } else {
        var templateName = $('#templateName').val('');
        var description = $('#descriptionField').val('');
        var campus = $('#campusField').val('');
        var account = $('#accountField').val('');
        var department = $('#departmentField').val('');
        var programName = $('#programNameField').val('');
        var projectId = $('#projectIdField').val('');
        var taskId = $('#taskIdField').val('');
    }
}

function deleteTemplate(tx) {
    var currentId = $('#currentId').text();
    alert(currentId);
    tx.executeSql('DELETE FROM templates', [], templatesSuccess, errorCb);
    alert("Authorize!!!");
}

1 个答案:

答案 0 :(得分:0)

刚刚进入这个,我不得不卸载应用程序并重新制作它(也改变了版本,但我不认为这样做)。 跟着this所以我每次调试都卸载应用程序。如果您每次都必须插入项目,但这至少解决了它,这可能很烦人。