0

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

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);

但这也给出了所有行。我在这里错过了什么吗?请帮忙

4

2 回答 2

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;
    }

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

于 2012-10-14T10:59:36.537 回答
-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
        }
}
于 2012-10-14T07:40:19.390 回答