-1

I have created a database, a table, and pre-populated it with initial values in one method:

function Setup(cb)
{
    _db = openDatabase("MoneyMan", "1.0", "Money Manager", 8 * 1024 * 1024);

    // Wallets
    _db.transaction(function(tx) {

        // Create wallet table
        tx.executeSql('CREATE TABLE Wallets (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, balance FLOAT, target FLOAT, transactions INTEGER)', [],
            function(tx, res) {

                // Add initial wallets
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Main", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Bills", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Savings", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Business", 0, 0, NULL)');

            });


        cb();
    });
}

This works perfectly fine.

I have a button to add a new wallet to the table, and it doesn't work. Even this code breaks:

_db.transaction(function(tx) {
        tx.executeSql('INSERT INTO Wallets VALUES (NULL, "Testing", 0, 0, NULL)');
});

EDIT: I have also tried listing the fields as such:

_db.transaction(function(tx) {
        tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES (?, 0, 0, NULL)', [name]);
    });

On Chrome, the whole page breaks and I get "Aww, Snap". On Android nothing happens at all. However, when I refresh the page, it seems the wallet was successfully added.

I've searched all over the internet for a solution to this and it's driving me mad. Where am I going wrong?

Thanks

EDIT:

When I set a breakpoint on executeSQL(), it runs fine. It's only when it tries to leave the transaction callback that it breaks.

EDIT:

I should note that this is Javascript compiled from another language. The variable "_db" is accessible from the second method. There are no issues if I do a SELECT statement when I try to add a new wallet. Only INSERT INTO breaks.

MBehtemam
  • 7,865
  • 15
  • 66
  • 108
Zotoaster
  • 393
  • 2
  • 4
  • 15
  • just a thought, might work, try putting the _db.transaction(...); inside a function and assign it to the onclick() of that button. Because you said item was successfully added after the crash, there seems to be no error with the SQL query – Rejo Chandran Jun 10 '16 at 13:50

2 Answers2

0

try

tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Testing", 0, 0, NULL)');

you are passing NULL as a primary key which is already on auto-increment

Rejo Chandran
  • 599
  • 4
  • 22
  • I have tried this too. This is the original code: _db.transaction(function(tx) { tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES (?, 0, 0, NULL)', [name], function(tx, res) { cb(res); } ); }); – Zotoaster Jun 10 '16 at 13:22
  • just check the parameters you are passing. you are passing 5 parameters (NULL, "Testing", 0, 0, NULL) whereas the scheme has only 4 – Rejo Chandran Jun 10 '16 at 13:23
  • do you have any stack trace of Exception info ? – Rejo Chandran Jun 10 '16 at 13:32
  • and are you sure you can use a "prepared statement" rather than a solid SQL query ? try using a definite SQL query without "?" (prepared statement) – Rejo Chandran Jun 10 '16 at 13:36
  • Stack trace just says anonymous function. My original code uses a prepared statement. – Zotoaster Jun 10 '16 at 13:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114366/discussion-between-rejo-chandran-and-zotoaster). – Rejo Chandran Jun 10 '16 at 13:52
0

You are not inserting the correct number of values. You want to provide a field list as you do in your first inserts. (name, balance, target, transactions) VALUES

Joe C
  • 3,925
  • 2
  • 11
  • 31