我想用自定义cursorAdapter提供listview。android没有这样的列:_id
我得到这个错误: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
在该行values.put(DataBaseHelper.DATABASEBASE_CONTENT_NOTE, note.getContent());
这里是我的2连接类
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;
}
}
这必须是简单而来自我已经创造了其他名字段基地的情况并不升级。
谢谢。
谢谢这是我没有得到的东西。 – 2012-03-15 21:27:28