我有我的數據庫一對夫婦的列已被註釋爲uniqueCombo =真ORMLite處理uniqueCombo約束失敗例外
@DatabaseField(columnName = "COL1", uniqueCombo = true)
private Double col1;
@DatabaseField(columnName = "COL2", uniqueCombo = true)
private Double col2;
根據ormlite文件這兩個領域的結合應該是在表中是唯一的。爲了測試這個,我故意添加相同的字段。我確實得到了SQLException,但不知道如何處理這個異常並要求用戶進行更改。
try {
mydao.create(myInfo);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
/* Need to uniquely identify constraint failed error here
* and ask user to make suitable change */
}
任何想法如何實現。
- UPDATE ---
的SQLException getErrorCode用於和getSQLState都分別返回0和空。
logcat的堆棧跟蹤:
Caused by: android.database.sqlite.SQLiteConstraintException: column NAME is not unique (code 19)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
01-31 22:15:14.042: W/System.err(2586): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
01-31 22:15:14.042: W/System.err(2586): at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:122)
01-31 22:15:14.042: W/System.err(2586): ... 15 more
異常的確切內容是什麼,在logcat中看到的是什麼 –
@StefandeBruijn:它並不重要,它違反約束條件是SQLiteException。 – PravinCG