I’m trying to feed a listview with a custom cursorAdapter.
I get this error : 03-16 03:25:39.968: E/AndroidRuntime(14552): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, content_note FROM notes
at this line values.put(DataBaseHelper.DATABASEBASE_CONTENT_NOTE, note.getContent());
Here are my 2 Connections classes
package com.android.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "db";
public static final String DATABASE_TABLE_NOTE = "notes";
public static final String DATABASE_ID_NOTE = "_id";
public static final String DATABASE_CONTENT_NOTE = "content_note";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE_NOTE + " (" + DATABASE_ID_NOTE + " INTEGER PRIMARY KEY, " + DATABASE_CONTENT_NOTE + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("Constants", "Maj de la base, suppression de toutes les anciennes donnees");
db.execSQL("DROP TABLE IF EXISTS Notes");
onCreate(db);
}
}
package com.android.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class NoteDataSource{
private SQLiteDatabase database;
private DataBaseHelper dbHelper;
private String[] allColumns = { DataBaseHelper.DATABASE_ID_NOTE,
DataBaseHelper.DATABASE_CONTENT_NOTE };
public NoteDataSource(Context context) {
dbHelper = new DataBaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor createNoteTop(String note) {
//recuperer le bon id
// int idNote = getFirstId().getId();
int idNote = 1 ;
ContentValues values = new ContentValues();
values.put(DataBaseHelper.DATABASE_ID_NOTE, idNote);
values.put(DataBaseHelper.DATABASE_CONTENT_NOTE, note);
long insertId = database.insert(DataBaseHelper.DATABASE_TABLE_NOTE, null,
values);
Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
allColumns, DataBaseHelper.DATABASE_ID_NOTE + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
return cursor;
}
public Cursor createNoteBottom(String note) {
//recuperer le bon id
int idNote = getLastId().getId();
ContentValues values = new ContentValues();
values.put(DataBaseHelper.DATABASE_ID_NOTE, idNote);
values.put(DataBaseHelper.DATABASE_CONTENT_NOTE, note);
long insertId = database.insert(DataBaseHelper.DATABASE_TABLE_NOTE, null,
values);
// To show how to query
Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
allColumns, DataBaseHelper.DATABASE_ID_NOTE + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
return cursor;
}
public void deleteNote(Note note) {
long id = note.getId();
System.out.println("Note deleted with id: " + id);
database.delete(DataBaseHelper.DATABASE_TABLE_NOTE, DataBaseHelper.DATABASE_ID_NOTE
+ " = " + id, null);
}
public Cursor getAllNotes() {
Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
allColumns, null, null, null, null, null);
return cursor;
}
private Note getFirstId(){
Note notes = new Note();
Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
notes = cursorToNote(cursor);
return notes;
}
private Note getLastId(){
Note notes = new Note();
Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
allColumns, null, null, null, null, null);
cursor.moveToLast();
notes = cursorToNote(cursor);
return notes;
}
private Note cursorToNote(Cursor cursor) {
Note note = new Note();
note.setId(cursor.getInt(0));
note.setContent(cursor.getString(1));
return note;
}
}
This must be simple and come from the fact that I already created base with other name fields and it doesn’t upgrade.
Thanks.
I guess you are right. You should increase the version number whenever you change the database. Then you can put code in
onUpgradethat can handle the transition.The schema for
onUpgradeis usually like thisThat upgrade schema will also allow users that have your app installed to upgrade the app to new versions without getting corrupted databases.