Javascript SQL INSERT 帮助

发布于 2024-11-26 02:57:55 字数 270 浏览 1 评论 0原文

我有以下代码,它将“任务”插入到具有“任务”值的表中。我正在尝试修改此语句以将多个值插入到多个列中,但由于我不熟悉该格式,因此我似乎无法使其工作。

有人可以告诉我如何修改以插入多个值吗?

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

多谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-12-03 02:57:55

我假设你并不是故意标记这个 MySQL...

但是 - 一个帮助 Enyo DB 查询的有用类是:

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

一种蜡笔的实现。非常适合 Enyo。

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.

梦晓ヶ微光ヅ倾城 2024-12-03 02:57:55

我不确定您是否使用某种库,但如果您使用 SQLite 的标准 Web SQL 接口,则第二个参数是值的数组,而不是像您这样的对象有。所以代码看起来更像

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

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
颜漓半夏 2024-12-03 02:57:55

不知道这个 javascript 实际在做什么使得很难回答这个问题,但足以说明将值插入到 sql 查询中的多个字段中的语法如下所示

INSERT INTO tableName ( col1col2coln)值(val1val2valn em>)

作为有根据的猜测,我想说你正在寻找

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

每个“?”在您的 sql 查询中,您将需要 values 数组中的另一个参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文