SQLite 检查一行是否存在 [英] SQLite check if a row exists

查看:47
本文介绍了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.

这篇关于SQLite 检查一行是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆