他のトピックを見ましたが、コードでエラーが見つかりません。これは、DBSQLiteConstraintException:UNIQUE制約が失敗しました
public class DBHelper extends SQLiteOpenHelper {
//data base name
private static final String DATABASE_NAME = "roomatesDB";
//data base version
private static final int DATABASE_VERSION = 1;
//tables name
public static final String APARTMENT_TABLE = "apartment";
public static final String ROOMATE_TABLE = "roomate";
public static final String SHOPCART_TABLE = "shopcart";
public static final String ITEMS_TABLE = "items";
//common column
public static final String APARTMENT_NUMBER_COLUMN = "apartmentNum";
//roomates table columns
public static final String FIRST_NAME_COLUMN = "firstName";
public static final String LAST_NAME_COLUMN = "lastName";
public static final String PHONE_NUMBER_COLUMN = "phoneNumber";
//shop cart table columns
public static final String NUMBER_COLUMN = "number";
public static final String LIST_NAME_COLUMN = "name";
//item table
public static final String PRICE_COLUMN = "price";
public static final String ITEM_NAME_COLUMN = "name";
//query for creating roomate table
public static final String CREATE_ROOMATE_TABLE = "CREATE TABLE "
+ ROOMATE_TABLE + "(" + FIRST_NAME_COLUMN + " TEXT, "
+ LAST_NAME_COLUMN + " TEXT, " + PHONE_NUMBER_COLUMN + " TEXT, "
+ APARTMENT_NUMBER_COLUMN + " INTEGER, "
+ "FOREIGN KEY(" + APARTMENT_NUMBER_COLUMN + ") REFERENCES "
+ APARTMENT_TABLE + "(apartmentNum) " + ")";
//query for crating shop cart table
public static final String CREATE_SHOPLIST_TABLE = "CREATE TABLE "
+ SHOPCART_TABLE + "(" + NUMBER_COLUMN + " INTEGER PRIMARY KEY,"
+ LIST_NAME_COLUMN + " TEXT, "
+ APARTMENT_NUMBER_COLUMN + " INTEGER, "
+ "FOREIGN KEY(" + APARTMENT_NUMBER_COLUMN + ") REFERENCES "
+ APARTMENT_TABLE + "(apartmentNum) " + ")";
//query for creating shop item table
public static final String CREATE_SHOPITEM_TABLE = "CREATE TABLE "
+ ITEMS_TABLE + "(" + ITEM_NAME_COLUMN + " TEXT,"
+ PRICE_COLUMN + " DOUBLE, "
+ NUMBER_COLUMN + " INT, "
+ "FOREIGN KEY(" + NUMBER_COLUMN + ") REFERENCES "
+ SHOPCART_TABLE + "(number) " + ")";
//query for creating apartment table
public static final String CREATE_APARTMENT_TABLE = "CREATE TABLE "
+ APARTMENT_TABLE + "(" + APARTMENT_NUMBER_COLUMN + " INTEGER PRIMARY KEY"
+ ")";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_APARTMENT_TABLE);
db.execSQL(CREATE_ROOMATE_TABLE);
db.execSQL(CREATE_SHOPLIST_TABLE);
db.execSQL(CREATE_SHOPITEM_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + APARTMENT_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ROOMATE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + SHOPCART_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ITEMS_TABLE);
onCreate(db);
}
と活動が話題 にエラーで がロードされる前に、ヨーヨーが活動
public void createApartment() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(APARTMENT_NUMBER_COLUMN, 1);
// Inserting Row
db.insert(APARTMENT_TABLE, null, values);
db.close(); // Closing database connection
}
public void addRoomate(RoomateModel r) {
SQLiteDatabase db = this.getWritableDatabase();
int i = 1;
ContentValues values = new ContentValues();
values.put(FIRST_NAME_COLUMN, r.getName()); // Contact Name
values.put(LAST_NAME_COLUMN, r.getLastName());
values.put(PHONE_NUMBER_COLUMN, r.getPhoneNumber()); // Contact Phone Number
values.put(APARTMENT_NUMBER_COLUMN, i);
// Inserting Row
db.insert(ROOMATE_TABLE, null, values);
db.close(); // Closing database connection
}
public RoomateModel getRoomates() {
RoomateModel r = null;
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + ROOMATE_TABLE + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()) {
String name = c.getString(0);
String lastName = c.getString(1);
String phoneNumber = c.getString(2);
r = new RoomateModel(name, lastName, phoneNumber);
}
if (r == null) {
r = new RoomateModel("bla", "bla", "bla");
}
return r;
}
アプリケーションがクラッシュした上でこれらの機能を実行しようとイムをチェックするために作成するための私のコードですこれはlogcat:
Error Code : 1555 (SQLITE_CONSTRAINT_PRIMARYKEY)
Caused By : Abort due to constraint violation.
(UNIQUE constraint failed: apartment.apartmentNum (code 1555))
#################################################################
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:952)
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:1609)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at atoa.roomates.SupportOperations.DBHelper.createApartment(DBHelper.java:106)
at atoa.roomates.QA.getRoomate(QA.java:49)
at atoa.roomates.QA.onCreate(QA.java:42)
それはあなたがコードを含めましたことを素敵だが、logcatせず、それは知ることは困難です問題が発生した場所 –
ありがとう、私はそれを編集しました – ATT
あなたのcreateApartmentメソッドはどこですか? – USKMobility