无法从sqlite数据库中获取数据

时间:2012-12-06 11:00:05

标签: android sqlite android-sqlite

在下面给出的程序中,我在表中插入imei_number。但是我必须检索插入的数据并以不同的方法显示它。请事先指导。谢谢

的onCreate() {

try {
     imeiDB =  this.openOrCreateDatabase(IMEI_DB_NAME, MODE_PRIVATE, null);

     imeiDB.execSQL("CREATE TABLE IF NOT EXISTS " +
     IMEI_TABLE_NAME +" (IMEI_number_db VARCHAR);");

     imeiDB.execSQL("INSERT INTO " + IMEI_TABLE_NAME +" Values ("+IMEI_number_db+");");

     Cursor c = imeiDB.rawQuery("SELECT IMEI_number_db FROM " + IMEI_TABLE_NAME , null);

     if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                     IMEI_number = c.getString(c.getColumnIndex("IMEI_number_db"));

                    }while (c.moveToNext());
            } 
     }



} catch (SQLiteException se ) {
     Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
     if (imeiDB != null) 
        imeiDB.execSQL("DELETE FROM " + IMEI_TABLE_NAME);
        imeiDB.close();
}

    }

/ * ** * ** * ** * ** * *** /

fetch the data in another method

private collectdata()
    {
         String IMEI_number ="";
        Cursor c = imeiDB.rawQuery("SELECT IMEI_number_db FROM " + IMEI_TABLE_NAME , null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                     IMEI_number = c.getString(c.getColumnIndexOrThrow("IMEI_number_db"));

                }while (c.moveToNext());
            } 
        }



    }       

2 个答案:

答案 0 :(得分:0)

您在查询中搜索的唯一一列是IMEI_number_db,因此游标只有一列。你应该改变这个:

IMEI_number = c.getString(c.getColumnIndex("IMEI_number_db"));

为此:

IMEI_number = c.getString(0);

答案 1 :(得分:0)

您也可以尝试以下代码。

IMEI_number = c.getString(c.getColumnIndexOrThrow("IMEI_number_db"));