开发者

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( 'I开发者_开发知识库NSERT INTO tasks ( task ) VALUES ( ? )', { values: [ item ] } ); 

Thanks alot.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜