Android. SQLite Exception: no such column _id

前端 未结 6 1267
鱼传尺愫
鱼传尺愫 2021-01-13 09:56

Im having some troubles trying to get the info stored in my database and showing it in a ListView.

This is the ListActivity where i want to show the rows:

         


        
6条回答
  •  不知归路
    2021-01-13 10:35

    Right, you should create the database table before querying on it.

    Here is an example:

     private static final String DATABASE_CREATE_DRIVERS =
            "create table " + DATABASE_TABLE_DRIVERS + "(" + KEY_ROWID +" integer primary key autoincrement, "
            + KEY_DRIVERID + " text not null,"
            + KEY_FIRST_NAME + " text not null,"
            + KEY_LAST_NAME + " text not null,"
            + KEY_SPEED_LIMIT + " int not null,"
            + KEY_TIMESTAMP + " int"
            + ");";
    

    Then query on it here:

     public Cursor fetchAllDrivers(){
         Cursor mCursor =
             mDb.query(true, DATABASE_TABLE_DRIVERS, new String[] {
                     KEY_ROWID, 
                     KEY_DRIVERID,
                     KEY_FIRST_NAME,
                     KEY_LAST_NAME,
                     KEY_SPEED_LIMIT,
                     KEY_TIMESTAMP},"", null,null, null, null, null);
         if (mCursor != null) {
             mCursor.moveToFirst();
         }
         return mCursor;
     }
    

    It also appears that you are missing a sixth argument as to what you are querying for. See where I put "" in method parameter string.

提交回复
热议问题