Web.py Cookbook 简体中文版 - 使用字典动态构造where子句

使用web.py创建动态where子句
本文介绍了如何使用web.py库中的web.db.sqlwhere函数将Python字典转换为适用于不同查询语句的where子句,并提供了实例演示及分组参数的使用方法。

问题

你希望创建一个字典来构造动态的where子句并且希望能够在查询语句中使用。

解决

>>> import web
>>> db = web.database(dbn='postgres', db='mydb', user='postgres')
>>> where_dict = {'col1': 1, col2: 'sometext'}
>>> db.delete('mytable', where=web.db.sqlwhere(where_dict), _test=True)
<sql: "DELETE FROM mytable WHERE col1 = 1 AND col2 = 'sometext'">

解释

web.db.sqlwhere takes a Python dictionary as an argument and converts it into a string useful for where clause in different queries. You can also use an optional grouping argument to define the exact gouping of the individual keys. For instance:

web.db.sqlwhere将Python的字典作为参数并且转换为适用于不同的查询语句的where子句的string类型数据。你也可以使用grouping参数来定义链接字典中的key的链接字符。例子如下。

>>> import web
>>> web.db.sqlwhere({'a': 1, 'b': 2}, grouping=' OR ')
'a = 1 OR b = 2'

grouping 的默认值为 ' AND '.

转载于:https://www.cnblogs.com/justjavac/archive/2012/11/23/webpy-cookbook-where-dict.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值