我正在尝试检查我的 sqlite 数据库中名为“Products”的表中是否存在特定 ID。
def existsCheck( db, id )
temp = db.execute( "select exists(
select 1
from Products
where promoID = ?
) ", [id] )
end
这是我当前的代码,但它返回一个数组,这意味着我必须先处理转换,然后才能将其用作 bool 值。有什么想法可以改变它,以便它返回一个值为 1 的 int 吗?
最佳答案
不需要使用子查询:
def existsCheck( db, id )
db.execute( "select 1
from Products
where promoID = ?",
[id] ).length > 0
end
这将返回一个 bool 结果。
关于ruby - SQLite 检查行是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13488593/