Search code examples
androidsqlitesimplecursoradapter

android sqlite error


I'm trying to put in sqldatabase 2 columns. When I insert coment all is good, but when I try to read listView I have error. column '_id' does not exist There is logcat:

    01-25 12:30:55.787: E/AndroidRuntime(4256): FATAL EXCEPTION: main
01-25 12:30:55.787: E/AndroidRuntime(4256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spacebrowser/com.example.spacebrowser.books}: java.lang.IllegalArgumentException: column '_id' does not exist

There is Sqliteadapter

    public class SQLiteAbdapter {
public static final String MYDATABASE_NAME = "MY_DATABASES";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";


private static final String SCRIPT_CREATE_DATABASE =
 "create table " + MYDATABASE_TABLE + " ("
 + KEY_ID + " integer primary key autoincrement, "
 + KEY_CONTENT1 + " text not null, "
 + KEY_CONTENT2 + " text not null);"; 

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAbdapter(Context c){

 context = c;

} 

public SQLiteAbdapter openToRead() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
 sqLiteDatabase = sqLiteHelper.getReadableDatabase();
 return this; 

} 

public SQLiteAbdapter openToWrite() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
 sqLiteDatabase = sqLiteHelper.getWritableDatabase();
 return this; 

}

public void close(){
sqLiteHelper.close();
} 

public long insert(String content1, String content2){

 ContentValues contentValues = new ContentValues();
 contentValues.put(KEY_CONTENT1, content1);
 contentValues.put(KEY_CONTENT2, content2);  


 return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
} 

public int deleteAll(){
 return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
 String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2};
 Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
 null, null, null, null, null, null);
   return cursor;

} 

 public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);

}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 // TODO Auto-generated method stub   
    if (oldVersion >= newVersion)
        return;
    db.execSQL("DROP TABLE IF EXISTS " + MYDATABASE_TABLE);
    onCreate(db);
}
    }

  } 

and there is books.java simplecursoradapter:

ListView listContent;   
  private SQLiteAbdapter mySQLiteAdapter;
  SimpleCursorAdapter cursorAdapter;
  Cursor cursor;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.books);

    listContent = (ListView)findViewById(R.id.list);
    mySQLiteAdapter = new SQLiteAbdapter(this);
    mySQLiteAdapter.openToRead();        

    cursor = mySQLiteAdapter.queueAll();
    String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAbdapter.KEY_CONTENT1, SQLiteAbdapter.KEY_CONTENT2};
    int[] to = new int[]{R.id.id, R.id.title, R.id.urlss};
    cursorAdapter =
     new SimpleCursorAdapter(this, R.layout.bookmark, cursor, from, to);
    listContent.setAdapter(cursorAdapter);    

What I am do so bad, that I have this error?

column '_id' does not exist

Solution

  • you missed to insert the id in your insert method. So you will add that

    public long insert(String id,String content1, String content2){
    
      ContentValues contentValues = new ContentValues();
      contentValues.put(KEY_ID, id);
      contentValues.put(KEY_CONTENT1, content1);
       contentValues.put(KEY_CONTENT2, content2);  
    
    
     return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    
     }