有人可以帮助我解决我得到的错误吗?我真的无法弄清楚为什么addCategory()方法被调用两次然后导致“UNIQUE约束失败”问题。非常感谢。代码如下:
public class MainActivity extends ActionBarActivity {
MyDatabase mdb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mdb=new MyDatabase(this);
mdb.addCategory("hello");
setContentView(R.layout.activity_main);
}
和在Mydatabase中
public class MyDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Event24";
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORY_TABLE = "CREATE TABLE if not exists test2
( " +
" _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" categorys TEXT UNIQUE)" ;
db.execSQL(CREATE_CATEGORY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS events");
// create fresh books table
this.onCreate(db);
}
public void addCategory(String name){
Log.e("mydatabase","addCategory");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("categorys", name);
db.insert("test2", // table
null, //nullColumnHack
cv); // key/value -> keys = column names/ values =
column values
db.close();
}
}
,错误如下:
05-22 09:50:34.672 1884-1884/com.example.julia.testactivitydatabase E/mydatabase﹕ addCategory
05-22 09:50:53.135 1884-1884/com.example.julia.testactivitydatabase E/mydatabase﹕ addCategory
05-22 09:50:53.137 1884-1884/com.example.julia.testactivitydatabase E/SQLiteLog﹕ (2067) abort at 12 in [INSERT INTO test2(categorys) VALUES (?)]: UNIQUE constraint failed: test2.categorys
05-22 09:50:53.137 1884-1884/com.example.julia.testactivitydatabase E/SQLiteDatabase﹕ Error inserting categorys=hello
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: test2.categorys (code 2067)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.julia.testactivitydatabase.MyDatabase.addCategory(MyDatabase.java:54)
at com.example.julia.testactivitydatabase.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
at android.app.ActivityThread.access$900(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:1)
每次启动应用程序或旋转屏幕时都会执行Activity.onCreate。此调用用于创建用户界面,而不是处理数据库初始化。这意味着它将被召唤两次但不会多次。
如果您需要设置一些预先存在的类别ONCE,请在MyDatabase.onCreate方法中执行此操作。
至于"为什么叫两次" - 在那里放一个日志。在单个应用程序启动中,没有必要连续两次调用它。如果您启动应用程序一次,将创建数据库行。如果你第二次启动它,即使onCreate在第二次启动期间被调用一次(应该如此),DB也会从之前的启动中获得这个固定的唯一记录。毕竟,DB的目的是在发布之间持久化信息。