sqlite检查提供的数据是否存在然后警报存在,否则警报不存在

时间:2015-09-28 13:09:05

标签: javascript jquery sqlite

我试图检查select查询是否存在where cause中提供的数据然后警告数据存在其他警报数据不存在但是没有得到如何实现。

Demo Fiddle

var db = openDatabase("demo", "1.0", "demo", 200000);

            db.transaction(function(tx) {
            tx.executeSql( "CREATE TABLE IF NOT EXISTS test (Slno INTEGER PRIMARY KEY AUTOINCREMENT, column1 TEXT UNIQUE, column2 TEXT UNIQUE, column3 TEXT UNIQUE, column4 TEXT UNIQUE, column5 TEXT UNIQUE)");
        },errorHandler);

db.transaction(function(tx) {
            tx.executeSql("INSERT INTO test (column1, column2 ,column3, column4,column5) VALUES ('insert1','insert2','insert3','insert4','insert5');");
        },errorHandler);

选择查询

db.transaction(function(tx) {
            tx.executeSql("SELECT * FROM test where column1='insert1'");
        },errorHandler,successHandler);


function errorHandler(error) {
    alert("Error : " + error.message);
}

function successHandler  (transaction, resultSet) {
            alert(resultSet.length);
}

1 个答案:

答案 0 :(得分:0)

您正在接收: -

Error : could not execute statement due to a constaint failure (19 UNIQUE constraint failed: test.column5)

因为浏览器正在记住上一个插入,而且列是唯一的。

您的选择交易周围存在轻微的格式问题,请尝试将其更改为: -

db.transaction(function(tx) {
    tx.executeSql("SELECT * FROM test where column1='insert1'", [], successHandler, errorHandler);
},errorHandler);

完整答案: -

var db = openDatabase("demo", "1.0", "demo", 200000);

db.transaction(function(tx) {
    tx.executeSql( "CREATE TABLE IF NOT EXISTS test (Slno INTEGER PRIMARY KEY AUTOINCREMENT, column1 TEXT UNIQUE, column2 TEXT UNIQUE, column3 TEXT UNIQUE, column4 TEXT UNIQUE, column5 TEXT UNIQUE)");
},errorHandler);

db.transaction(function(tx) {
    tx.executeSql("SELECT * FROM test where column1='insert1'", [], successHandler, errorHandler);
},errorHandler);

function errorHandler(error) {
    alert("Error : " + error.message);
}

function successHandler  (transaction, resultSet) {
    alert(resultSet.rows.length);
}

Fiddle