2014-12-06 116 views
0

我試圖讓列「_id」的數據(這是第一列),名和密碼欄(共3列)的指數第一列(_id)= -1

但我得到這個錯誤消息

無法讀取行0,列-1從CursorWindow ...

所以我運行一個測試,以獲得使用getColumnCount(),但它的列數返回

後,我開始把所有列的索引,我發現「_id」列返回看重(-1),這就是爲什麼我得到這個錯誤消息

我看着我的查詢語句但它沒有任何問題。

有人能告訴我我的代碼有什麼問題嗎?

public class SQLiteAdapter { 

     SQLiteHelper helper; 

     public SQLiteAdapter(Context context) { 
      helper = new SQLiteHelper(context); 
     } 

     public String getAllData() { 
      SQLiteDatabase db = helper.getWritableDatabase(); 

      // select _id, name, password from ahmadssbdb 
      String[] columns = { SQLiteHelper.COL_NAME, SQLiteHelper.COL_PASSWORD }; 

      Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, null, null, 
        null, null, null); 

      StringBuffer strBuffer = new StringBuffer(); 

      int indexUID = cursor.getColumnIndex(SQLiteHelper.COL_ID); 
      int indexColName = cursor.getColumnIndex(SQLiteHelper.COL_NAME); 
      int indexColPassword = cursor.getColumnIndex(SQLiteHelper.COL_PASSWORD); 
      String uid = " " + indexUID + " " + indexColName + " " + indexColPassword; 
      return uid; 


     /************************************* 
     * Retrieve data from each column 
     *************************************/ 

     /* 
     * while(cursor.moveToNext()){ 
     * 
     * long uid = cursor.getInt(indexUID); 
     * String name = cursor.getString(indexColName); 
     * String password = cursor.getString(indexColPassword); 
     * strBuffer.append(uid + " " + name + " " + password + "\n"); } 
     * 
     * return strBuffer.toString(); 
     */ 
     } 

    static class SQLiteHelper extends SQLiteOpenHelper { 
     private Context context; 

     // Defined Database Schema 
     private static final String DATABASE_NAME = "dbname"; 
     private static final String TABLE_NAME = "db_table"; 
     private static final int DATABASE_VERSION = 4; 
     private static final String COL_ID= "_id"; 
     private static final String COL_NAME = "name"; 
     private static final String COL_PASSWORD = "password"; 

     // List of Queries 
     private static final String QUERY_CREATE_TABLE = 
       "CREATE TABLE " + TABLE_NAME + " (" 
       + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + COL_NAME + " VARCHAR(250), " 
       + COL_PASSWORD + " VARCHAR(250)" + 
       ");"; 

     private static final String QUERY_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; 

     public SQLiteHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      this.context = context; 
      Message.message(context, "constructer was called"); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      try { 
       // CREATE TABLE ahamadssb_table (_id INTEGER PRIMARY KEY 
       // AUTOINCREMENT, name VARCHAR(255)); 
       db.execSQL(QUERY_CREATE_TABLE); 
       Message.message(context, "onCreate was called"); 
      } catch (SQLException e) { 
       Message.message(context, "" + e); 
      } 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      try { 
       db.execSQL(QUERY_DROP_TABLE); 
       onCreate(db); 
       Message.message(context, "onUpgrade was called"); 
      } catch (SQLException e) { 
       Message.message(context, "" + e); 
      } 

     } 

    } 
} 
+0

嘗試重新安裝後completley應用程序卸載一次.. – Lal 2014-12-06 17:46:11

回答

2

您的查詢不會詢問id列。將它添加到您的列變量中:

String[] columns = { SQLiteHelper.COL_ID, SQLiteHelper.COL_NAME, SQLiteHelper.COL_PASSWORD }; 
+0

感謝,我的錯誤,我是想不「_id」首先,它是確定的,但後來我包括「_id」,但我忘了將它添加到數組中 – 2014-12-06 18:02:54

+0

在此網站上,將接受的答案標記爲已接受(複選標記),不編輯您的問題以包括「(已解決)」 – tachyonflux 2014-12-06 18:27:37

+0

抱歉,再次感謝您 – 2014-12-06 18:29:29

相關問題