0

Am trying to create a alert condition if the duplicate value is inserted to DB on duplicate entry of data on unique state Example on my code below as test table's all columns are unique and insert query has a repeated entry of data on that i need to show sql error

Create Query

    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)");
        });

Insert Query

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

1 Answer 1

1

You can use a error callback function that is being called when an sql error occurs.

tx.executeSql(query, values, successHandler, errorHandler);

Note that there is room for both an error and a success function.

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

Also use a success function like this

function successHandler  (transaction, resultSet) {
            if (!resultSet.rowsAffected ) {
                // Previous insert failed. Bail.
                alert('No rows affected!');
                return false;
            }else if (resultSet.rowsAffected == 1){
                 alert("ONLY one row inserted");
            }
}

Because your insert query does not really have an error, it executes fine but does not insert both queries.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank s for the reply but it is not working database is opened and a table is also created but the value are not inserted can you help he hear jsfiddle.net/chsgf50t/11
Edited my answer. Check the success callback
my question was hear if i add the errorHandler as suggested next to value query that doesn't works tx.executeSql("INSERT INTO test (column1, column2 ,column3, column4,column5) VALUES ('insert1','insert2','insert3','insert4','insert5');",errorHandler);
the place where i have placed the errorHandler is it correct i think am placing the errorHandler in a wrong place or ma be the placement code is wrong need a conformation on that
It is in the wrong place indeed, if you use no success you need to put null, before the errorhandler. I updated your fiddle: jsfiddle.net/chsgf50t/12 though I am not able to test it on my browser for some reason.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.