mardi 15 septembre 2015

Android SQLite No such column '_id' exists

I am having trouble with displaying the results for the search activity. I am getting the error "column '_id' does not exist". I don't know why this error occurs. Shall I rename a column for an "_id"? I already searched for a few answers such as changing the database version, and adding a column '_id' but I think that is not the error because the database is working when accessed by another sql statements. Only in this activity I get this error.

Here is my code for the SearchActivity.java displaying the results

private void handleIntent(Intent intent) {

    //GET ALL THE LIST OF ALL THE DATABASE
    File dir = new File("data/data/com.example.fieldbookv2/databases");
    File[] filelist = dir.listFiles();
    String[] namesOfFiles = new String[filelist.length];
    dbName = namesOfFiles[0];
    DatabaseHelper db1 = new DatabaseHelper(getApplicationContext(),dbName);
    SQLiteDatabase sqldb = db1.getWritableDatabase();
    String query = intent.getStringExtra(SearchManager.QUERY);
    String querysearch = "SELECT ObjectID FROM (SELECT * FROM Data WHERE ObjectID = ?)";
    cursor = sqldb.rawQuery(querysearch,  new String[]{query});
    final ListView listView = (ListView) findViewById(R.id.results_listview);

    SearchAdapter adapter = new SearchAdapter(this, R.layout.results_column, cursor,0 );
    listView.setAdapter(adapter);}

This is my DatabaseHelper.java for the Data table

// DATA Table - column names
private static final String KEY_OBJECTID= "ObjectID";
private static final String KEY_DATETIME = "DateTime";
private static final String KEY_LONGITUDE = "Longitude";
private static final String KEY_LATITUDE = "Latitude";
private static final String KEY_IMAGE = "image";
private static final String KEY_AUDIO = "audio";
public static final String[] DATA_COLUMNS = {
            KEY_OBJECTID, KEY_PMAID, KEY_VALUE, KEY_USERIDNO,
            KEY_DATETIME, KEY_DEVICEID, KEY_LONGITUDE, KEY_LATITUDE, KEY_ACTIVE,
            KEY_BLOCKED, KEY_SEQID, KEY_IMAGE, KEY_AUDIO};

// DATA create statement
public static final String CREATE_TABLE_DATA = "CREATE TABLE if not exists " + TABLE_DATA + "("
        + KEY_OBJECTID + " TEXT,"
        + KEY_PMAID + " TEXT,"
        + KEY_VALUE + " TEXT,"
        + KEY_USERIDNO + " TEXT,"
        + KEY_DATETIME + " TEXT,"
        + KEY_DEVICEID + " TEXT,"
        + KEY_LONGITUDE + " TEXT,"
        + KEY_LATITUDE + " TEXT,"
        + KEY_ACTIVE + " TEXT,"
        + KEY_BLOCKED + " TEXT,"
        + KEY_SEQID + " INTEGER PRIMARY KEY,"
        + KEY_IMAGE + " TEXT,"
        + KEY_AUDIO + " TEXT"
        + ")";

Can you help me with this? Any help would be greatly appreciated! Thanks! :)

Aucun commentaire:

Enregistrer un commentaire