0

在下面给定的程序中,我在表中插入了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());
            } 
        }



    }       
4

2 回答 2

0

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

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

为了这:

IMEI_number = c.getString(0);
于 2012-12-06T11:12:25.107 回答
0

你也可以试试下面的代码。

IMEI_number = c.getString(c.getColumnIndexOrThrow("IMEI_number_db"));
于 2012-12-06T11:19:42.663 回答