levi
levi

Reputation: 25111

Help with Javascript SQL INSERT

I have the following code which inserts a "task" into a table with a value for the "task". I am trying to modify this statement to insert multiple values into multiple columns, but I cant seem to get it to work since I am unfamiliar with the format.

Can sombody show me how the can be modified to insert multiple values?

var item  = this.$.newItem.getValue();
this.$.db.query( 'INSERT INTO tasks ( task ) VALUES ( ? )', { values: [ item ] } ); 

Thanks alot.

Upvotes: 0

Views: 12938

Answers (3)

Doug Reeder
Doug Reeder

Reputation: 739

I'm not sure if you're using some kind of library, but if you're using the standard Web SQL interface to SQLite, the second argument is an array of values, not an object like you have. So code looks more like

db.transaction(
    function (transaction) {
        transaction.executeSql(
                "INSERT OR IGNORE INTO contact (contactId, nameG, nameF, orgContactId, accountId) VALUES (?, ?, ?, ?, ?)",
                [contact.contactId, contact.nameG, contact.nameF, contact.orgContactId, contact.accountId],
                function (transaction, resultSet) {   // SQL INSERT statement success
                    // do something now that the item has been saved
                },   // end statement success callback
                function (transaction, sqlError) {   // statement fail
                    // report failed operation
                    return true;   // abort transaction
                }
        );
    }   // end transaction function
    // no transaction callbacks
);   // end transaction call

Upvotes: 2

diagonalbatman
diagonalbatman

Reputation: 18002

I am assuming you didn't mean to tag this MySQL....

However - a useful class for helping out with Enyo DB queries is:

https://github.com/onecrayon/database-webos

One crayon's implementation. Works very nicely for Enyo.

Upvotes: 2

Jamiec
Jamiec

Reputation: 136104

Not knowing what this javascript is actually doing makes it hard to answer this question, but suffice it to say the syntax for inserting values into multiple fields in a sql query is as follows

INSERT INTO tableName (col1, col2, coln) VALUES (val1, val2, valn)

As a total educated guess, I would say you're looking for

this.$.db.query( 'INSERT INTO tasks ( task, some_other_field ) VALUES ( ?,? )', { values: [ item, some_other_input ] } ); 

for each "?" in your sql query, you will need another parameter in the values array.

Upvotes: 1

Related Questions