用于从列中查找特定数据的sqlite搜索查询?

时间:2012-10-11 08:52:48

标签: android

我正在制作寻找印度车牌的应用程序。 我的数据库包含两个列“代码”和“城市”代码包含MH1,MH2等数据。 和城市包含像浦那,孟买这样的数据。

App包含一个edittext框和listview。

Listview包含来自GJ3 Rajkot,GJ10 Jamnagar等数据库的全部数据。

如果我在编辑文本框中写GJ,则必须在listview中显示GJ的唯一数据。

3 个答案:

答案 0 :(得分:2)

以下是查询:  选择 * 来自table_name 在哪里code喜欢'%GJ%' 限制0,30

答案 1 :(得分:2)

试试这个:

Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?",
            new String[] { "search" });

编辑:

按照上面给定链接的教程,将以下方法添加到TestAdapter

public Cursor get_tag_Data()
 {
     try
     {
         String sql ="select * from table_name where column_name = ?", new String[] { edittext.getText().toString().trim()}";

         Cursor mCur = mDb.rawQuery(sql, null);
         if (mCur!=null)
         {
            mCur.moveToNext();
         }
         return mCur;
     }
     catch (SQLException mSQLException) 
     {
         Log.e(TAG, "getTestData >>"+ mSQLException.toString());
         throw mSQLException;
     }
 }

并从您的班级调用此方法,如:

 TestAdapter mDbHelper = new TestAdapter(urContext);        
 mDbHelper.createDatabase();      
mDbHelper.open();

Cursor testdata = mDbHelper.get_tag_Data();

mDbHelper.close();

编辑:

将以下方法声明到数据库类中,

 public List<String> getQuestions(String difficulty) {

   public static List<String> question_Set;
    question_Set = new ArrayList<String>();


    Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?", new String[] { difficulty });

    while (c.moveToNext()) {

        question_Set.add(c.getString(1).trim());


    }

    return question_Set;
}

现在调用

DB.getQuestions(edittext.getText().toString().trim()); // DB is your database class name

答案 2 :(得分:0)

使用此link

希望它会对你有所帮助。