web2py 插入方法

发布于 01-01 16:08 字数 317 浏览 4 评论 0原文

Web2py 文档有两种插入数据库的方法

db.tbl[0] = newRow

db.tbl.insert(newRowAsDict)

文档暗示它们是同义词,但它们似乎不同。其一,如果 newRow 包含表中不存在的字段,则 insert 方法会引发异常。此外,.insert 方法返回所添加行的 id,而赋值则不会返回。

  • 这是预期的行为吗?
  • 如果我使用赋值方法,如何获取 id?
  • 赋值方法是否被废弃?

Web2py docs have two methods for inserting into a database

db.tbl[0] = newRow

and

db.tbl.insert(newRowAsDict)

The documentation implies that they are synonyms, but they appear to be different. For one, the insert method throws an exception if newRow contains fields that are not in the table. Also the .insert method returns the id of the added row, where the assignment doesn't.

  • Is this the intended behavior
  • How can I get the id if I use the assignment method?
  • Is the assignment method depricated?

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

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

发布评论

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

评论(3

白云不回头2025-01-08 16:08:18

还有

db.tbl.insert(**db.tbl._filter_fields(newRowAsDict))

一个会过滤 newRowAsDict 中的键,忽略未知字段。

There is also

db.tbl.insert(**db.tbl._filter_fields(newRowAsDict))

which will filter the keys in newRowAsDict ignoring unknown fields.

懷念過去2025-01-08 16:08:18
  • 这是预期的行为

根据代码,它似乎是。使用分配方法,字段被过滤,因此它只尝试插入属于表的字段。标准 insert() 方法不会发生这种情况。

  • 如果使用赋值方式,如何获取id?

如果您需要 id,那么最好使用 insert() 方法。

  • 分配方法是否已弃用?

我不这么认为。

  • Is this the intended behavior

Based on the code, it appears to be. Using the assignment method, the fields are filtered so it only attempts to insert the fields that belong to the table. This does not happen with the standard insert() method.

  • How can I get the id if I use the assignment method?

If you need the id, you're probably better off using the insert() method.

  • Is the assignment method depricated?

I don't think so.

寄风2025-01-08 16:08:18

一点点的探索显示了两者之间的区别:

对于:db.tbl[0] = dict(name='something')

File "/var/web2py/gluon/globals.py", line 172, in <lambda>
    self._caller = lambda f: f()
File "/var/web2py/applications/myapp/controllers/default.py", line 114, in test
    db.tbl[0] = dict(name='something')
File "/var/web2py/gluon/dal.py", line 5531, in __setitem__
    self.insert(**self._filter_fields(value))
File "/var/web2py/gluon/dal.py", line 5605, in insert
    return self._db._adapter.insert(self,self._listify(fields))

对于:db.tbl.insert(name=') some')

File "/var/web2py/gluon/globals.py", line 172, in <lambda>
    self._caller = lambda f: f()
File "/var/web2py/applications/myapp/controllers/default.py", line 115, in test
    db.tbl.insert(name='something')
File "/var/web2py/gluon/dal.py", line 5605, in insert
    return self._db._adapter.insert(self,self._listify(fields))

它们最终都会调用相同的代码来执行插入,因此您会看到它们运行相同的查询:

INSERT INTO tbl(name) VALUES ('something');

因为前者执行 _filter_fields 从跟踪中可以明显看出,当存在时它不会抛出异常是表中不存在的字段,而另一个是表中存在的字段。

显然,db.tbl[0] = newRow 无法返回值。您应该将其视为 insert 的简写,其表亲 db.tbl[x>0] 对于更新非常有用,并且具有完全相同的表示法,这有助于简化代码。

A little bit of probing shows the difference between the two:

For: db.tbl[0] = dict(name='something')

File "/var/web2py/gluon/globals.py", line 172, in <lambda>
    self._caller = lambda f: f()
File "/var/web2py/applications/myapp/controllers/default.py", line 114, in test
    db.tbl[0] = dict(name='something')
File "/var/web2py/gluon/dal.py", line 5531, in __setitem__
    self.insert(**self._filter_fields(value))
File "/var/web2py/gluon/dal.py", line 5605, in insert
    return self._db._adapter.insert(self,self._listify(fields))

For: db.tbl.insert(name='something')

File "/var/web2py/gluon/globals.py", line 172, in <lambda>
    self._caller = lambda f: f()
File "/var/web2py/applications/myapp/controllers/default.py", line 115, in test
    db.tbl.insert(name='something')
File "/var/web2py/gluon/dal.py", line 5605, in insert
    return self._db._adapter.insert(self,self._listify(fields))

Both of them end up calling the same code to do the insert, so you will see that they run the same query:

INSERT INTO tbl(name) VALUES ('something');

Since the former does _filter_fields as is apparent from the trace, it does not thrown an exception when there are fields that are not present in the table, while the other does.

Obviously, db.tbl[0] = newRow cannot return a value. You should just consider it a shorthand for insert and its cousin db.tbl[x>0] is extremely useful for updates and has the exact same notation and this helps simplify the code.

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