nishant52 0 Newbie Poster

I'm trying to perform multiple inserts into a table present in SQLite database using JavaScript.
For this purpose I'm using Transactions.

Here is my code (HTML + JavaScript):

<html>
  <head>
    <script language="javascript">
        var shortName = 'Test';
        var version = '1.0';
        var displayName = 'Test';
        var maxSize = 65536;
        db = openDatabase(shortName, version, displayName, maxSize);

        function onBodyLoad()
        {

            // begin transaction
            query = "BEGIN;";

            // creating a simple table with one integer valued column.
            query += "CREATE TABLE IF NOT EXISTS dir (phone INTEGER);"

            // inserting simple values
            query += "INSERT INTO dir VALUES(10);";
            query += "INSERT INTO dir VALUES(20);";
            query += "INSERT INTO dir VALUES(30);";

            // committing the transaction
            query += "COMMIT;";

            // executing the above formed query.
            db.transaction(
                    function(transaction)
                    {
                        transaction.executeSql(query,[],successHandler,errorHandler);
                    }
                   );
        }
        function successHandler()
        {
            alert("Rows Successfully inserted.");
        }
        function errorHandler(transaction, error)
        {
            alert('Oops. Error was \''+error.message+'\' (Code '+error.code+')');
            return true;
        }
    </script>

  </head>
  <body onload="onBodyLoad();">
    SQLite Multiple Insert Testing....
  </body>
</html>

On running the above file in Chrome and as well as Safari,
I'm getting the following error:

Oops. Error was 'not authorized' (Code 1)

When I'm running the CREATE TABLE and multiple INSERT INTO queries separately in WebInspector (in both Chrome and Safari), they are executing with out any errors.

But when I execute BEGIN or BEGIN TRANSACTION , the same error is shown:

not authorized

I'm unable to resolve this error.

I want to implement Transactions in JavaScript because executing insert statement one after the other is time consuming compared to multiple insert statements using transactions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.