外键问题android SQLite

时间:2012-12-27 21:28:50

标签: android sqlite


我想问一下我的代码中我的foregin密钥不起作用的问题是什么。主键运行良好。 我有3个表,用这种方式创建:(有些单词可以在波兰语中,因为我这样做是为了传递主题但是这些单词不是importnat(我猜))

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并稍后通过按钮应用于tabel来从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)));

你知道我怎么能让它有效?提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

java.lang.NumberFormatException: Invalid int: "null"

您的数据库中包含空值...您可以通过使用default+ KEY_ID1 + " INTEGER DEFAULT 0, "设置表来防止以后发生这种情况。要更正当前表中的数据,请使用UPDATE命令将所有空值更改为适当的default值。

另一种方法是每次检查每个

String value = cursor.getString(1);
if(value != null)
    zamow.setIDk(Integer.parseInt(value));

但这是解决问题的最慢方法 再次,如果您使用适当的方法,getInt()

zamow.setIDk(cursor.getInt(1));

你可能没有这个问题......