SQLite 检查一行是否存在

问题描述:

我正在尝试检查我的 sqlite 数据库中名为Products"的表中是否存在特定 ID.

I'm trying to check if a specific ID exists in a table named "Products" in my sqlite database.

def existsCheck( db, id )
    temp = db.execute( "select exists(
        select 1
        from Products
        where promoID = ?
    ) ", [id] )
end

这是我当前的代码,但它返回一个数组,这意味着我必须先处理转换,然后才能将其用作布尔值.任何想法如何更改它以使其返回值为 1 的 int?

that's my current code but that returns an array which means I have to deal with conversion before I can use it as a boolean. Any ideas how I can change it so that it returns an int with the value 1?

无需使用子查询:

def existsCheck( db, id )
    db.execute( "select 1
                 from Products
                 where promoID = ?",
                [id] ).length > 0
end

这将返回一个布尔结果.

This returns a boolean result.