Android. SQLite Exception: no such column _id
Asked Answered
D

6

5

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:

public class Prueba extends ListActivity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.i_prueba);

        DataBaseAccess db = new DataBaseAccess(this);
        db.open();

        Cursor result = db.getAllContact();
        startManagingCursor(result);


        // the desired columns to be bound
        String[] columns = new String[] {"_id", "nombre", "telefono", "mail"};
        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.textid, R.id.textNombre, R.id.textTelefono, R.id.textMail };

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.i_listadb, result, columns, to);


        this.setListAdapter(mAdapter);

    }

The DB class. Here is the method getAllContact where i get the error:

public class DataBaseAccess {

public static final String NOMBRE = "nombre";
public static final String TELEFONO = "telefono";
public static final String MAIL = "mail";
public static final String ID = "_id";


    public DataBaseAccess(Context context) {
        this.androidContext = context;
//DataBaseHelper is another class where the Database is created. In the "onCreate" //method of that class is where i make the CREATE TABLE and add some values to that //table.
        dbHelper = new DataBaseHelper(androidContext);  
    }



    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
        }



[...MORE METHODS...]

    public Cursor getAllContact(){
        String[] columnas = new String[] {ID, NOMBRE, TELEFONO, MAIL};
        Cursor mCursor = dbHelper.query("t_contactos", columnas, null, null, null, null, null);
    if (mCursor != null){
        mCursor.moveToFirst();
    }
    return mCursor;
}

}

And here is the error messages from the LogCat:

E/AndroidRuntime(  636): Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(  636): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pfc.android/com.pfc.android.Prueba}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, nombre, telefono, mail FROM t_contactos

E/AndroidRuntime(  636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)

E/AndroidRuntime(  636):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)

E/AndroidRuntime(  636):    at android.app.ActivityThread.access$2100(ActivityThread.java:116)

E/AndroidRuntime(  636):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)

E/AndroidRuntime(  636):    at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(  636):    at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(  636):    at android.app.ActivityThread.main(ActivityThread.java:4203)

E/AndroidRuntime(  636):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(  636):    at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(  636):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

E/AndroidRuntime(  636):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

E/AndroidRuntime(  636):    at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  636): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, nombre, telefono, mail FROM t_contactos

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.native_compile(Native Method)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1118)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1006)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:964)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1041)

E/AndroidRuntime(  636):    at com.pfc.android.DataBaseAccess.getAllContact(DataBaseAccess.java:97)

E/AndroidRuntime(  636):    at com.pfc.android.Prueba.onCreate(Prueba.java:21)

E/AndroidRuntime(  636):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

E/AndroidRuntime(  636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)

E/AndroidRuntime(  636):    ... 11 more

So, can anybody help to get through this? Thank you in advance !

Dancette answered 12/10, 2010 at 1:43 Comment(0)
C
6

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)

Cline answered 12/10, 2010 at 4:9 Comment(4)
This is the CREATE TABLE statement in my code: "create table t_contactos (_id integer primary key autoincrement, nombre varchar not null, telefono varchar not null, mail varchar not null);" But when i checked it with the SqliteBrowser the column name showed is just "id". Any idea of what is happening?Dancette
Try to correct it to _id manually, maybe some bug in the creation method, not sure.Cline
You mean to re-write in the SQLiteBrowser and get the file back to the phone, right? I'll try. Anyway, im detecting something weird. I have some traces in the "onCreate" method where i create the database and fill it, but i dont see these traces anywhere, so just to be sure... When i create a new object of a class, after executing the contructor method, it will execute the onCreate?Dancette
yes, push the file back to the phone, is what I meant. OnCreate: that's only automatically called for Activities, not for any objects (simple java beans) in general.Cline
S
7

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).

Sloop answered 13/7, 2011 at 8:51 Comment(0)
C
6

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)

Cline answered 12/10, 2010 at 4:9 Comment(4)
This is the CREATE TABLE statement in my code: "create table t_contactos (_id integer primary key autoincrement, nombre varchar not null, telefono varchar not null, mail varchar not null);" But when i checked it with the SqliteBrowser the column name showed is just "id". Any idea of what is happening?Dancette
Try to correct it to _id manually, maybe some bug in the creation method, not sure.Cline
You mean to re-write in the SQLiteBrowser and get the file back to the phone, right? I'll try. Anyway, im detecting something weird. I have some traces in the "onCreate" method where i create the database and fill it, but i dont see these traces anywhere, so just to be sure... When i create a new object of a class, after executing the contructor method, it will execute the onCreate?Dancette
yes, push the file back to the phone, is what I meant. OnCreate: that's only automatically called for Activities, not for any objects (simple java beans) in general.Cline
N
4

Try rowid instead of _id. It works for me.

Nasser answered 18/10, 2012 at 5:46 Comment(1)
Worked for me in python.Nazler
S
1

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.

Sergiosergipe answered 27/7, 2011 at 3:55 Comment(0)
H
0

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.

Haymes answered 12/8, 2016 at 9:59 Comment(0)
A
-2

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

Actium answered 18/7, 2020 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.