我正在尝试使用自定义cursorAdapter提供listview。
我收到此错误: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;
}
}
这必须简单,并且来自我已经使用其他名称字段创建基础并且不升级的事实。
感谢。
答案 0 :(得分:3)
这必须简单,并且来自我已经使用其他名称字段创建基础并且不升级的事实。
我猜你是对的。您应该在更改数据库时增加版本号。然后,您可以将代码置于可以处理转换的onUpgrade
中。
private static final int DATABASE_VERSION = 2;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
onUpgrade
的架构通常就是这样
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// it all started this way
if (oldVersion < 1) {
db.execSQL("CREATE TABLE supertable...");
}
// in version 2 I decided that I need another table
if (oldVersion < 2) {
db.execSQL("CREATE TABLE cooltable...");
}
// in version 3 I decided that the table is not required anymore
if (oldVersion < 3) {
db.execSQL("DROP TABLE cooltable...");
}
// remember to increase DATABASE_VERSION if you add steps here.
}
该升级架构还允许安装了您的应用的用户将应用升级到新版本,而不会损坏数据库。
答案 1 :(得分:1)