【问题标题】:How to pass dynamic values to a Query using SQLITE in Android ?如何在 Android 中使用 SQLITE 将动态值传递给查询?
【发布时间】:2014-02-05 19:03:18
【问题描述】:

我试图传递动态 id 来查询,每次传递的 id 都不会改变,有时它可能是 3 或 2 等等。我的问题是我如何传递这个 id 并从查询中选择值。

String[] splits = clickedTopicIdString.split("-");
    Log.i("splits",""+splits.length); // length 2 bcoz clickedTopicIdString = 0-1 it may be 0-1-2 etc
    if(splits.length > 0)
    {
        for(int i=0;i<splits.length;i++)
        {clickedTopicIdInt = Integer.parseInt(splits[i]);// i want to save values in an array and pass it GetQuestionData(); this method after converting it in integer.....
         Log.i("clickedTopicIdInt",""+clickedTopicIdInt);
        }

    }

    clickedTopicIdInt = Integer.parseInt(clickedTopicIdString);
    dbhelper = new JamiaBinoriaDBHelper(context);
    dbhelper.open();
    GetQuestionData();

此函数将以数组的形式动态接收 id 并将其应用到 topic_question.topic_id="+topic_id1+" 或 topic_question.topic_id="+topic_id2+" 等等我该怎么做?

public List<String> GetClickedIdImages(int topic_id)
    { List<String> questionImageNameList = new ArrayList<String>();
    String query = "SELECT topic_question.question_image_name FROM topic_question,topic WHERE topic_question.topic_id="+topic_id+" AND topic.id=topic_question.topic_id";   
   // Cursor cursor = db.rawQuery("SELECT topic_question.question_image_name FROM topic_question,topic WHERE topic_question.topic_id=1 AND topic.id=topic_question.topic_id",null);
    Cursor cursor = db.rawQuery(query,null);
    Log.i("Cursor Query Print:",""+query);
        if(cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
         Log.d("cursor",""+cursor);
                questionImageNameList.add(cursor.getString(0));
                Log.i("cursor.getString(2)",""+cursor.getString(0));
                cursor.moveToNext();
            }
        } else {
            Log.d("", "Cursor is Null");
            Log.d("retrieving all parameters", "count < 0");
        }

        cursor.close();
        return questionImageNameList;
    }

有人可以帮我完成这项任务吗?

【问题讨论】:

    标签: java android sqlite split


    【解决方案1】:

    如果您想根据 ID 列表动态创建选择,您应该使用以下 SQL 语法:

    SELECT * FROM TABLE WHERE ID IN (id1, id2, ..., idn) 
    

    id1,id2 = 你的身份证

    http://www.tutorialspoint.com/sqlite/sqlite_where_clause.htm

    String numbers = "1-2-3-4-5";
            String [] splits = numbers.split("-");
            Integer [] realNumbers = new Integer [splits.length];
            String sqlSelect = "SELECT * FROM TABLE WHERE ID IN ";
            String whereStatement = "(";
            for (int i = 0; i<splits.length; i++)
            {
                realNumbers[i] = Integer.parseInt(splits[i]);
                whereStatement += realNumbers[i]+((i == splits.length -1) ? "" :",");
    
            }
            whereStatement += ");";
    
            sqlSelect += whereStatement; //your select SELECT * FROM TABLE WHERE ID IN (1,2,3,4,5);
    

    【讨论】:

    猜你喜欢
    • 2020-07-13
    • 2018-08-15
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多