【问题标题】:webSQL AUTOINCREMENT not working (Different from the one in search)webSQL AUTOINCREMENT 不起作用(与搜索中的不同)
【发布时间】:2014-03-04 02:45:48
【问题描述】:

学习 phonegap 为 Android 编写一个基本的应用程序。出于这个原因使用 WebSQL,我知道它已被弃用,但它仅适用于 Android 手机并且无法扩展,因此目前不会打扰诸如 polyfill 之类的东西。

我什至在开始处理回调处理程序(代码的基础)之类的东西之前尝试让 auto_increment 工作:

var db = openDatabase('test', '1.0', 'Test', 10 * 1024 * 1024);
db.transaction(
    function (tx) {  
        tx.executeSql('CREATE TABLE IF NOT EXISTS test (id INTEGER AUTOINCREMENT, value VARCHAR)');

    }
);

$(function(){
    $('#test').click(function(){
        alert('click');
        db.transaction(
            function (tx) {
                tx.executeSql('INSERT INTO test (value) VALUES (?)', ["testValue"]);
            }
        );      
    });
});

尝试使用诸如 default 关键字之类的东西,但似乎没有任何效果。如果我为 ID 列添加一个值,它就可以正常工作。


编辑这是尝试的代码,没有插入任何内容。

var db = openDatabase('test', '1.0', 'Test', 10 * 1024 * 1024);
db.transaction(
    function (tx) {  
        tx.executeSql('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value VARCHAR)');

    }
);

$(function(){
    $('#test').click(function(){
        alert('click');
        db.transaction(
            function (tx) {
                tx.executeSql('INSERT INTO test (value) VALUES (?)', ["testValue"]);
            }
        );      
    });
});

【问题讨论】:

    标签: android cordova


    【解决方案1】:

    试试这个:

    tx.executeSql('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value VARCHAR)');
    

    在 webSql (sqlite) 中,PRIMARY KEY 会自动递增,除非您传递值。此处的其他信息:http://sqlite.org/autoinc.html

    【讨论】:

    • 不起作用,因为这里的目标是让 AUTOINCREMENT 在 INSERT 上工作。
    • 我在 phonegap 项目中以这种方式使用它,并在插入时自动递增。你试过了吗?
    • 你可以在这里阅读更多信息:stackoverflow.com/questions/16832401/…
    • 好的,它自己工作,只是没有插入点击事件。很有趣。
    • 尝试将alert 移动到事务之后,看看会发生什么。在 PG 中,警报可能会搞砸事情
    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2017-05-26
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2013-02-04
    相关资源
    最近更新 更多