Android的SQLite的IN,NOT IN的语法 [英] Android Sqlite IN, NOT IN syntax

查看:308
本文介绍了Android的SQLite的IN,NOT IN的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行。在选择其中。在名单是动态的。类似于

I'm trying to run NOT IN select where NOT IN list is dynamic. Something like

SELECT id, type FROM CONTACTS where type NOT IN ('connect','answer')

在code我徒劳的尝试是:

In the code my futile attempts were:

db.query(TABLE, new String[] { "id", ""}, " type NOT IN (?)", "connect,answer", 
    null, null, null); // didn't work
db.query(TABLE, new String[] { "id", ""}, " type NOT IN (?)", "'connect','answer'", 
null, null, null); // didn't work

我拿上这是替换对待我的逗号列表作为一个参数。我没有找到一个解决方案,但它是相当难看,所以我不会张贴在这里,直到有人来着的东西更优雅

My take on this is that ? substitution treats my comma list as single argument. I did find a solution but it is rather ugly so I won't post it here until someone comes forward with something more elegant

推荐答案

您不能将只是一个?代替值的列表。因此,很少有从试图参数化列表来获得。人们可以,当然,创建2,4,16价值prepared语句 ...型NOT IN(?,?),新的String [] {连接,答案},... 但即使远程RDBMS服务器上有可疑的价值。 Istead,做

You cannot place just one '?' instead of a list of values. As such, there is little to gain from trying to parametrize lists. One can, of course, create 2,4,16-value prepared statements ..." type NOT IN (?,?)", new String[]{ "connect","answer" },... but even on a server with remote RDBMS it has questionable value. Istead, do

db.query(TABLE, new String[] { "id", ""}, " type NOT IN ('connect','answer')", 
     null, null, null, null);

如果该列表是动态的,你将不得不逃避串并把它们放到一个引用列表。

if the list is dynamic, you will have to escape the strings and put them into single quoted list.

这篇关于Android的SQLite的IN,NOT IN的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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