android中的rawquery动态where子句条件?

时间:2014-12-19 11:26:04

标签: android sqlite

我是android的新手,并且第一次使用sqlite rawquery dynamic where子句条件,并且不知道如何使用它。我想给where子句提供动态值,以根据特定的" mid"获取listview。如何从SubjectActivty提供中间值

这是我的代码:

TestTable的:

    public long insert(String id, String time, int mid, String cmarks, String nmarks,
        String questions, String testType, String test,String marks) {

    log("insert test : " + test);

    ContentValues values = new ContentValues();
    values.put(KEY_TESTID, id);
    values.put(KEY_MID,mid);
    values.put(KEY_TEST, test);
    values.put(KEY_TIME, time);
    values.put(KEY_CMARK, cmarks);
    values.put(KEY_NMARK, nmarks);
    values.put(KEY_TESTTYPE, testType);
    values.put(KEY_QUESTION, questions);
    values.put(KEY_Total_Marks, marks);
    return db.insert(TABLE_NAME2, null, values);
}

    public ArrayList<NotificationListItem> getAllList(
        ArrayList<NotificationListItem> privateArrayList) {

    openToRead();
    privateArrayList.clear();

    Cursor cursor = null;
    String sql ="SELECT * FROm test_list WHERE mid=?"; 
     cursor= db.rawQuery(sql, null); 
    log("getAlllist() cursor : " + cursor.getCount());

    if (cursor != null) {
        log("getAlllist() cursor not null ");

        int index = 0;
        cursor.moveToFirst();

        while (index < cursor.getCount()) {

            NotificationListItem item = new NotificationListItem();

            int idIndex = cursor.getColumnIndex(TestTable.KEY_TESTID);
            int subid= cursor.getColumnIndex(TestTable.KEY_MID);
            int nameIndex = cursor.getColumnIndex(TestTable.KEY_TEST);
            int idTime = cursor.getColumnIndex(TestTable.KEY_TIME);
            int cMarks = cursor.getColumnIndex(TestTable.KEY_CMARK);
            int nMarks = cursor.getColumnIndex(TestTable.KEY_NMARK);
            int testTypeIndex = cursor.getColumnIndex(TestTable.KEY_TESTTYPE);
            int questions = cursor.getColumnIndex(TestTable.KEY_QUESTION);

            item.name = cursor.getString(nameIndex);
            item.testID = cursor.getString(idIndex);
            item.mid=cursor.getInt(subid);
            item.time = cursor.getString(idTime);
            item.cmark = cursor.getString(cMarks);
            item.nmark = cursor.getString(nMarks);
            item.testType = cursor.getString(testTypeIndex);
            item.questions = cursor.getString(questions);

            index++;
            privateArrayList.add(item);
            cursor.moveToNext();
        }
        log(" query(): cursor closing");
        cursor.close();
        db.close();
        db = null;
    }
    return privateArrayList;
}

SubjectActvity.class

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_subject);

    privateListLV = (ListView) findViewById(R.id.privateListLV);
    privatelistTable = new SubjectTable(SubjectActivity.this);
    testTableStatic = new StaticTestTable(this);
    testTable = new TestTable(SubjectActivity.this);

    privatelistTable.openToWrite();
    privatelistTable.deleteAll();
    privatelistTable.insert(10, "Biology");
    privatelistTable.insert(20, "Chemistry");
    privatelistTable.insert(30, "English");
    privatelistTable.insert(40, "Maths");
    privatelistTable.insert(50, "GK");

    testTable.openToWrite();
    testTable.deleteAll();
    testTable.insert("1", "10", 10, "5", "2", "2", "Both", "Anatomy", "10");
    testTable.insert("2", "10", 20, "5", "2", "2", "Both", "Paper1", "10");

    privateArrayList = new ArrayList<NotificationListItem>();

    listAdapter = new SubjectCustomListAdapter(this, privateArrayList,
            privatelistTable);

    privateListLV.setAdapter(listAdapter);

    privateListLV.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("unchecked")
        public void onItemClick(AdapterView<?> adapter, View arg1,
                int position, long arg3) {
            NotificationListItem selection = (NotificationListItem) adapter
                    .getItemAtPosition(position);
            String item = selection.getName();
            System.out.println("item" +item);

            if (!item.contentEquals(" ")) {
                subjectid = privatelistTable.getSinlgeEntry(item);
                Log.e("selected Value", " " + subjectid);


                Intent testact = new Intent(getApplicationContext(),
                        TestsActivity.class);
                testact.putExtra("subject", item);
                testact.putExtra("mid",subjectid);
                startActivity(testact);

            } else {
                return;
            }
        }
    });
}

@Override
protected void onResume() {

    super.onResume();
    updateList();

}
private void updateList() {
    privatelistTable.getAllList(privateArrayList);
    listAdapter.notifyDataSetChanged();
} 

1 个答案:

答案 0 :(得分:1)

以@Der Golem回答或其他方式

Cursor c =db.rawQuery("SELECT * FROM " + tableName + " where mid=" + mid , null);