我创建了一个与数据库相关的应用程序。
在此我创建简单的数据库并在 sql 查询中使用in从数据库中获取值。
我的问题是我得到了像“2,6,8,9,10”这样的字符串。首先我拆分这个字符串并存储在一个数组列表中。没有逗号。在这个数组列表之后,我用逗号合并,比如 2,6,8,9,10 这个。
这一切我都做得很好。我在查询中使用此字符串从数据库中获取值。我的查询是
SELECT tm.icon,tm.topic_no,td.topic_id,td.name FROM topicmaster tm ,topicmaster_description td WHERE td.topic_id =tm.topic_id AND td.langid='3' AND tm.oraganisationid ='1' AND td.topic_id in(2,6,8,9,10);
这。
但是当我传递合并字符串时,它看起来像
SELECT tm.icon,tm.topic_no,td.topic_id,td.name FROM topicmaster tm ,topicmaster_description td WHERE td.topic_id =tm.topic_id AND td.langid='3' AND tm.oraganisationid ='1' AND td.topic_id in('2,6,8,9,10');
所以我无法运行查询,因为原因中的传递值不同in
。在上面的查询中工作正常,因为它s like a
in(2,6,8,9,10)
in second it look like a
in('2,6,8,9,10')
so what to do.
how to remove this
'` from string?
首先我拆分字符串
//getTopicFile ArrayList with
public ArrayList<String> getAssignTopicArrayList(String passString)
{
ArrayList<String> rtnArrayList=new ArrayList<String>();
try
{
StringTokenizer strTokens = new StringTokenizer(passString,",");
while (strTokens.hasMoreElements())
{
rtnArrayList.add(String.valueOf(strTokens.nextToken()));
}
} catch (Exception ex)
{
ex.printStackTrace();
System.err.println("Error in Fetch ArrayList-->"+ex.toString());
}
return rtnArrayList;
}
//getTopicFile ArrayList with
在我合并新字符串之后
genHelper.showErrorLog("Assign Topic-->"+chapterAssignTopicName);
ArrayList<String> getTopicList=genHelper.getAssignTopicArrayList(chapterAssignTopicName);
String passConcat = "";
for (int i = 0; i < getTopicList.size(); i++)
{
System.out.println("Topic List--->"+getTopicList.get(i));
if(getTopicList.size()==1)
{
passConcat=passConcat.concat(String.valueOf(getTopicList.get(i)));
}
else
{
if(getTopicList.size()-1 == i)
{
System.err.println("value if --> " + i);
passConcat=passConcat.concat(String.valueOf(getTopicList.get(i)));
}else {
System.err.println("value else--> " + i);
passConcat=passConcat.concat(String.valueOf(getTopicList.get(i)).concat(","));
}
}
}
genHelper.showErrorLog("PassConcat String-->"+passConcat);
然后我得到了新字符串并在下面传入查询
String topicQuery="SELECT tm.icon,tm.topic_no,td.topic_id,td.name FROM topicmaster tm ,topicmaster_description td WHERE td.topic_id =tm.topic_id AND td.langid='"+LanguageActivity.languageId+"' AND tm.oraganisationid ='"+genHelper.loadPreferences(String.valueOf(R.string.sharePrefin_Login_organizationid))+"' AND td.topic_id in('"+passConcat+"')";