我正在创建一个应用程序,我从一个不同的活动调用我的数据库但我不断从数据库方面获得各种错误。起初我得到一个“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):插入注释时出错=注意:end_date =截止日期:标题=项目名称:start_date =开始日期: 08-23 22:38:17.104:ERROR / Database(775):android.database.sqlite.SQLiteConstraintException:错误代码19:约束失败
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):在android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:66)
08-23 22:38:17.104:错误/数据库(775):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1313)
08-23 22:38:17.104:ERROR / Database(775):在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)
答案 0 :(得分:0)
在你的onUpgrade事件中,你正在删除表DATABASE_NAME,但不应该是DATABASE_TABLE吗?
没有(除此之外)向我跳出来,在实例化DBAdapter对象时是否收到此错误?是在onCreate事件中还是在其他地方发生错误?