我正在创建一个应用程序,我在其中从不同的活动调用我的数据库,但我不断从数据库方面收到各种错误。起初,我得到一个
“SQLiteException 没有这样的数据”
当我删除我的应用程序并重新安装它时,现在
“android.database.sqlite.SQLiteConstraintException:错误代码 19:约束失败”
我不知道该怎么办了。在创建数据库方面经历了很多不同的例子,但我不知道我是否遗漏了什么?..这是我的数据库端代码:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
private static final String DATABASE_NAME = "MemoData.db";
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE = "Memo";
public static final String KEY_ID = "_id";
public static final String NAME = "Title";
public static final String START_DATE = "start_date";
public static final String END_DATE = "end_date";
public static final String NOTES = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_CREATE = "create table Memo (_id integer primary key autoincrement,"
+ "Title text not null, start_date text not null,"
+ "end_date text not null, "
+ "notes text not null);";
private final Context context;
private DBHelper dbhelper;
private SQLiteDatabase db;
public DBAdapter(Context context) {
this.context = context;
dbhelper = new DBHelper(context);
}
private static class DBHelper extends SQLiteOpenHelper {
DBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,"upgrading Database from version"+ oldVersion+ "to"
+newVersion+ "which oold data will be lost");
db.execSQL("DROP TABLE IF EXITS Memo");
onCreate(db);
}
}
public DBAdapter open()throws SQLException {
db = dbhelper.getWritableDatabase();
return this;
}
public void close(){
dbhelper.close();
}
public long createItem(String Title, String start_date,String end_date,String notes){
open();
ContentValues initialValues = new ContentValues();
initialValues.put(NAME, Title);
initialValues.put(START_DATE, start_date);
initialValues.put(END_DATE, end_date);
initialValues.put(NOTES, notes);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor retrieveItems(){
SQLiteDatabase db = dbhelper.getWritableDatabase();
String[] resultColumns = new String[] {NAME,START_DATE,END_DATE,NOTES };
Cursor cursor = db.query(DATABASE_TABLE, resultColumns, null, null, null, null, null);
Integer cindex = cursor.getColumnIndex("Title");
String result = "Result = ";
if(cursor.moveToFirst()){
do {
result += cursor.getString(cindex)+ "-";
}while(cursor.moveToNext());
}
return cursor;
}
}
这是主要的错误输出:
08-23 22:38:17.104: ERROR/Database(775): Error inserting notes=Notes: end_date=Due Date: Title=Name of Item: start_date=Start Date:
08-23 22:38:17.104:ERROR/Database(775): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
08-23 22:38:17.104: ERROR/Database(775):at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
08-23 22:38:17.104: ERROR/Database(775):at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:66)
08-23 22:38:17.104: ERROR/Database(775):at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1313)
08-23 22:38:17.104: ERROR/Database(775):at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1173)
08-23 22:38:17.104: ERROR/Database(775):at com.Memo.DBAdapter.createItem(DBAdapter.java:94)
08-23 22:38:17.104: ERROR/Database(775):at com.Memo.NewItem.save(NewItem.java:365)