0

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.

7
  • 3
    What abomination of a javascript library lets you perform datbase inserts? Commented Jul 26, 2011 at 14:43
  • This is within HP WebOs Enyo framework, Inserting into a local SQLite DB Commented Jul 26, 2011 at 14:46
  • Is this websql? If not this is the biggest security risk I have ever seen on stackoverflow. Commented Jul 26, 2011 at 14:46
  • inserts look like this: INSERT INTO tasks ( task, task_date, remark ) VALUES ( ?,?,? ) Commented Jul 26, 2011 at 14:46
  • @Jamiec, JavaScript is used for more than just client-side web development. Node.js, for example, is a server-side usage of JavaScript. As a language it's very powerful, but you do have to remember to notify people as to what context it's being called in. Commented Jul 26, 2011 at 14:51

3 Answers 3

2

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.

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

Comments

2

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

Comments

1

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.

Comments

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.