Android. SQLite Exception: no such column _id

前端 未结 6 1260
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2021-01-13 10:35

    This is can exists 2 possiblities:

    1. This problem can occurred from broken SQLite schema.

    Your question is not about SQL Statement problem. but many developer can think SQL Statement problem about your question.

    This case can check no demaged data in your database file. Although you can not use select fields and sql where clause by field name.

    As a result, you can not use database file in your android code.

    Exactly solution, I recommend recreate SQLite DB file, step by step.

    You must be backup before use SQLite modification tool. (SQLite Manager, Browser, others db manage tools)

    1. This problem occurred from your persistent data.

    If you use the same file name for assets or raw data when run with modified data,

    you can try uninstall previous installed app for refresh.

    0 讨论(0)
  • 2021-01-13 10:38

    i change my database name, all lower_caps. it worked for me.

    0 讨论(0)
  • 2021-01-13 10:39

    Try rowid instead of _id. It works for me.

    0 讨论(0)
  • 2021-01-13 10:46

    You probably don't have a column _id in your database table. Pull the database and open it with SqliteViewer to check if it actually exists. The database is usually under /data/data/com.yourapp.package/databases/

    You can use: http://sqlitebrowser.sourceforge.net/ or http://www.navicat.com/en/products/navicat_sqlite/sqlite_overview.html (they also have a free lite version)

    0 讨论(0)
  • 2021-01-13 10:49

    If you created database for example with fields 'a', 'b' and 'c' and used this db then if you add new field (for ex. 'd') you need to recreate database on phone (just delete it).

    0 讨论(0)
提交回复
热议问题