Reputation: 1081
I looked at other topics, but can't find my error in the code. This is my code for creating the DB
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);
}
and for checking im trying yo execute these function on the activity
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;
}
the application crashes before the activity is loaded with the error in the topic this is the 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)
Upvotes: 5
Views: 22129
Reputation: 2066
I think like cricket_007 said you are trying to duplicate the id of the record in the database, a quick solve for this which I have used more than once is to go to Settings -> apps -> your application -> delete the storage, then the app will work just fine, this is not a good solution though, it will just inform you about the problem. A real solution to this problem is to make sure that the id is unique by adding for example the size of the table each time u add a new record, or if there many accounts the user can use them make a directory for each username, try to figure out the best for your case.
Upvotes: 0
Reputation: 191701
You have a primary key on your table, therefore it needs to be a unique value.
public static final String CREATE_APARTMENT_TABLE = "CREATE TABLE "
+ APARTMENT_TABLE + "(" + APARTMENT_NUMBER_COLUMN + " INTEGER PRIMARY KEY"
+ ")";
Yet, you always are inserting a value of 1 into that column.
values.put(APARTMENT_NUMBER_COLUMN, 1);
// Inserting Row
db.insert(APARTMENT_TABLE, null, values);
There are several ways to fix that, but you should start with using distinct numbers instead of always a 1
Upvotes: 3