我查看了其他主题,但是在代码中找不到我的错误.这是我创建数据库的代码
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;
}
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)
解决方法:
您的表上有一个主键,因此它必须是唯一的值.
public static final String CREATE_APARTMENT_TABLE = "CREATE TABLE "
+ APARTMENT_TABLE + "(" + APARTMENT_NUMBER_COLUMN + " INTEGER PRIMARY KEY"
+ ")";
但是,您总是在该列中插入值1.
values.put(APARTMENT_NUMBER_COLUMN, 1);
// Inserting Row
db.insert(APARTMENT_TABLE, null, values);