在SQLite中选择查询?

时间:2012-10-14 07:12:42

标签: android sqlite select-query

我正在使用该函数从表中检索一个值,

public ArrayList selectValue(SQLiteDatabase sqliteDB, String contactEmail){
        Cursor c = sqliteDB.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ContactEmail='"+contactEmail+"'",null);
        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    double contactId = c.getDouble(c.getColumnIndex("ContactId"));
                    results.add("ContactEmail: " +contactEmail+ ",ContactId: " + contactId);
                }while (c.moveToNext());
            } 
        }
        return results;
    }

但上面的函数检索表中的所有值。不确定查询有什么问题..

我也尝试过硬编码这样的值,

Cursor c = sqliteDB.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ContactEmail='peter@peter.com'",null);

但这也给出了所有行。我在这里遗漏了什么?请帮忙

2 个答案:

答案 0 :(得分:1)

感谢您的回复人员..我解决了问题..

这就是我改变代码并使其工作的方式,

public String selectValue(SQLiteDatabase sqliteDB, String contactEmail){

        String contactId="Nothing";

        Cursor c = sqliteDB.rawQuery("SELECT * FROM " + TABLE_NAME + " where ContactEmail = '"+contactEmail+"'", null);
        if (c != null ) {
            if  (c.moveToFirst()) {
                contactId = c.getString(c.getColumnIndex("ContactId"));
            }   
        }
        return contactId;
    }

再次感谢!!祝你有美好的一天!

答案 1 :(得分:-1)

尝试这样做:

    SQLiteDatabase mDb;
    DataBaseHelper mDbHelper;
    mDbHelper = new DataBaseHelper(context);
    mDb = mDbHelper.getWritableDatabase();
    Cursor cursor = mDb.query(TABLE_NAME, null, ContactEmail + " =? ", new String[] { "peter@peter.com" }, null, null, null);
    //Continue with your code


private static class DataBaseHelper extends SQLiteOpenHelper {

        /* Create a helper object to create, open, and/or manage a database */
        DataBaseHelper(Context context) {
            super(context, TABLE_NAME, null, DATABASE_VERSION);
        }

        /* Called when the database is created for the first time */
        @Override
        public void onCreate(SQLiteDatabase db) {
                     //Your code
        }

            /* Called when the database needs to be upgraded*/ 
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                      //Your code
        }
}