我想问一下我的代码中我的外键不起作用的问题是什么。主键运行良好。我有 3 个表,以这种方式创建:(有些词可以是波兰语,因为我这样做是为了传递主题,但这些词不重要(我猜))
private static final String TABLE_CONTACTS = "Kontrahenci";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "imie";
private static final String KEY_SURNAME = "nazwisko";
private static final String KEY_STREET = "ulica";
private static final String KEY_NRB = "numerBudynku";
private static final String KEY_NRL = "numerLokalu";
private static final String KEY_KOD = "KodPocztowy";
private static final String KEY_CITY = "Miejscowosc";
private static final String KEY_NIP = "NIP";
private static final String KEY_TEL = "Telefon";
private static final String KEY_EMAIL = "Email";
private static final String</b> DATABASE_CREATE = "CREATE TABLE "
+ TABLE_CONTACTS + " ( "
+ KEY_ID +" INTEGER PRIMARY KEY, "
+ KEY_NAME +" TEXT, "
+ KEY_SURNAME+" TEXT, "
+KEY_STREET+" TEXT, "
+KEY_NRB+" INT(4), "
+KEY_NRL+" INTEGER(4), "
+KEY_KOD+" INTEGER(5), "
+KEY_CITY+" TEXT, "
+KEY_NIP + " TEXT, "
+ KEY_TEL+" TEXT, "
+KEY_EMAIL+" TEXT "+" ); ";
private static final String TABLE_PRODUCTS= "Produkty";
private static final String KEY_IDPROD = "id";
private static final String KEY_NAMEPROD = "nazwa";
private static final String KEY_IMG = "obraz";
private static final String KEY_PRICE = "CENA";
private static final String DATABASE_PRODUCTS_CREATE = "CREATE TABLE "
+ TABLE_PRODUCTS + " ( "
+ KEY_IDPROD +" INTEGER PRIMARY KEY, "
+ KEY_NAMEPROD +" TEXT, "
+ KEY_IMG+" NUMBER, "
+KEY_PRICE+" REAL" + " ); ";
private static final String TABLE_ORDER = "Zamowienia";
private static final String KEY_IDORD = "id";
private static final String KEY_ID1 = "id_kon";
private static final String KEY_ID2 = "id_prod";
private static final String KEY_ILOSC = "ilosc";
private static final String KEY_STATUS = "Status";
private static final String DATABASE_ORDERS_CREATE = "CREATE TABLE " + TABLE_ORDER + " ( "
+ KEY_IDORD + " INTEGER PRIMARY KEY, "
+ KEY_ID1 + " INTEGER, "
+ KEY_ID2 +" INTEGER, "
+ KEY_ILOSC + " INTEGER, "
+ KEY_STATUS + " TEXT, "
+"FOREIGN KEY (id_kon) REFERENCES "+ TABLE_CONTACTS +"(id), "
+"FOREIGN KEY (id_prod) REFERENCES " + TABLE_PRODUCTS +"(id));" ;
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_PRODUCTS_CREATE);
db.execSQL(DATABASE_ORDERS_CREATE);
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
后来我以这种方式向所有人插入数据:
public void addProdukt (Produkt produkt){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAMEPROD, produkt.getnazwa());
values.put(KEY_IMG, produkt.getZdj());
values.put(KEY_PRICE, produkt.getCena());
db.insert(TABLE_PRODUCTS, null, values);
db.close();}
public void addZamow (Zamowienie zamowienie){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID1, zamowienie.getIDk());
values.put(KEY_ID2, zamowienie.getIDp());
values.put(KEY_ILOSC, zamowienie.getIlosc());
values.put(KEY_STATUS, zamowienie.getStatus());
db.insert(TABLE_ORDER, null, values);
db.close();
}
所有没有的表FOREIGN KEY
都工作正常,但是当我想获取所有数据时,这个表显示“找不到源”。我通过更新Integer values
并稍后通过按钮应用到表来从 Spinners 添加此密钥,但即使我手动输入
db.addZamow(new Zamowienie(1,1,x,tstatus));
我仍然有这个错误。要获取这些元素的所有列表,我正在使用此函数:
public List<Zamowienie> getAllZamowienie(){
SQLiteDatabase db = this.getWritableDatabase();
List<Zamowienie> zamowlist = new ArrayList<Zamowienie>();
String selectQuery = "SELECT * FROM " + TABLE_ORDER;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Zamowienie zamow = new Zamowienie();
zamow.setID(Integer.parseInt(cursor.getString(0)));
zamow.setIDk(Integer.parseInt(cursor.getString(1)));
zamow.setIDp(Integer.parseInt(cursor.getString(2)));
zamow.setIlosc(Integer.parseInt(cursor.getString(3)));
zamow.setStatus(cursor.getString(4));
zamowlist.add(zamow);
}while(cursor.moveToNext());
}
return zamowlist;
}
此行之后出现错误:zamow.setIDk(Integer.parseInt(cursor.getString(1)));
你知道我怎样才能让它工作吗?提前感谢您的回答。