简体   繁体   中英

Android database simplecursoradapter error

I'm programming an android app.

This is my BDHelper class:

public class BD_Helper extends SQLiteOpenHelper{

private static final String BD_NOMBRE = "BD_PAED.db";
private static final int BD_VERSION = 1;
private static final String TABLA_AUDIT_NOMBRE = "audit";
private static final String TABLA_CONTROLES_NOMBRE = "controles";

private static final String crearAudit = 
                                "CREATE TABLE " + TABLA_AUDIT_NOMBRE + " (" +
                                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                "usuario TEXT, " +
                                "clave TEXT, " +
                                "userid INTEGER, " +
                                "pin TEXT, " +
                                "tema TEXT, " +
                                "colorFondo TEXT, " +
                                "color TEXT, " +
                                "colorFuente TEXT)";

private static final String crearControles = 
                                "CREATE TABLE " + TABLA_CONTROLES_NOMBRE + " (" +
                                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                "entryTypeId TEXT, " +
                                "statusTypeId TEXT, " +
                                "momentTypeId TEXT, " +
                                "activityTypeId TEXT, " +
                                "activityDuration TEXT, " +
                                "activityTypeOthers TEXT, " +
                                "intensityTypeId TEXT, " +
                                "measurementTime TEXT, " +
                                "glucosePre TEXT, " +
                                "CHRations TEXT, " +
                                "rapidInsulin TEXT, " +
                                "slowInsulin TEXT, " +
                                "glucosePost TEXT);";

public BD_Helper(Context context){
    super(context, BD_NOMBRE, null, BD_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {       
    db.execSQL(crearAudit);
    db.execSQL(crearControles);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists "+ TABLA_AUDIT_NOMBRE);
    db.execSQL("drop table if exists "+ TABLA_CONTROLES_NOMBRE);
    onCreate(db);
}

}

In other class I'm trying to use a simplecursoradapter to show the info of the controles database:

private void cargaDatos(){
    String[] from;

    cursor = bdHelper.obtenerControles(entryTypeId);
    startManagingCursor(cursor);

        from = new String[] {BD_ControlesAdapter.CAMPO_MEASUREMENTTIME,BD_ControlesAdapter.CAMPO_ACTIVITYTYPEID};

    int[] to = new int[]{R.id.fila_fecha,R.id.fila_tipo};


    SimpleCursorAdapter controles = new SimpleCursorAdapter(this,R.layout.fila_controles,cursor,from,to,0);
    setListAdapter(controles);

}

But this doesn't work, the new SimpleCursorAdapter throws an:

IllegalArgumentException: column '_id' does not exist.

If I check my sqlite database this column is perfectly created in this database.

Any idea?

I afraid,your cursor gets the resultset from obtenerControles() method,not having _id field into that.That is,you might not have fetched _id from your table while querying.

Post your code for obtenerControles(entryTypeId) or check by yourself to make sure,it fetches _id into resultset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM