0

this code has to alerting the things i add to the table. He is not alerting the value of the variable "hallo". Do you know what's wrong in this code?

<html>
<body>
    <script>
        var db = openDatabase('neueDb', '1.0', "Test DB", 2 * 1024 * 1024);
        var hallo = "hallo1234";

            db.transaction(function (tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (log)');
                tx.executeSql('INSERT INTO LOGS (log) VALUES ("foobar")');
                tx.executeSql('INSERT INTO LOGS (log) VALUES ("logmsg")');
                tx.executeSql('INSERT INTO LOGS (log) VALUES (?)', hallo);
            });

            db.transaction(function (tx) {
                tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
                        var len = results.rows.length, i;

                        for (i = 0; i < len; i++) {
                            alert(results.rows.item(i).log);
                        }
                }, null);
        });
    </script>
</body>

Thank you for all answers! In Love, Dexter

2 Answers 2

1

For me, in Chrome I get an error when executing tx.executeSql('INSERT INTO LOGS (log) VALUES (?)', hallo);

VM109:1 Uncaught TypeError: Failed to execute 'executeSql' on 'SQLTransaction': The 2nd argument is neither an array, nor does it have indexed properties.(…)

I don't know why, I never had this error before. But if I do db.transaction(function (tx) { tx.executeSql('INSERT INTO LOGS (log) VALUES (?)', [hallo]); }) it works correctly, and then the alert is showing hallo value OK.

I think you have an error inserting the value and that's why it doesn't get alerted.

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

1 Comment

No problem!! For me it's kind of a weird behaviour... I will try to find out why.
1

You miss the idea that db.transaction is asynchronous.

You expect that first it will insert all your rows, then will select them. That's not what happens actually.

This is roughtly what you need:

   b.transaction(function (tx) {
      tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (log)');
      tx.executeSql('INSERT INTO LOGS (log) VALUES ("foobar")');
      tx.executeSql('INSERT INTO LOGS (log) VALUES ("logmsg")');
      tx.executeSql('INSERT INTO LOGS (log) VALUES (?)', [hallo]);
      tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
                    var len = results.rows.length, i;

                    for (i = 0; i < len; i++) {
                        alert(results.rows.item(i).log);
                    }
            }, null);
   });

Of course tx.executeSql('SELECT * FROM LOGS' ideally should be wrapped in another function.

Also note @Jorge comment regarding the syntax you use.

1 Comment

For me your code is also not working. Giving the error I stated in my answer when inserting hallo value.

Your Answer

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