2014-03-03 71 views
0

如何保存ArrayList < HashMap < String >>在數據庫中?將arrayList散列表保存到數據庫中

獲取NULL值在數據庫

這個方法我試過:Saving records into the database from the hashmap in android 但並沒有幫助

public void addQuote(Quote q, ArrayList<HashMap<String, String>> put_q_list) 
{ 

    try{ 
    myDataBase=this.getWritableDatabase(); 
    ContentValues values=new ContentValues(); 

    values.put("q_customer_name", q.getCustomer_name()); 
    values.put("q_customer_email", q.getCustomer_email()); 
    values.put("q_b_street", q.getBilling_street()); 
    values.put("q_b_city", q.getBilling_city()); 
    values.put("q_b_state", q.getBilling_state()); 
    values.put("q_b_zipcode", q.getBilling_zipcode()); 
    values.put("q_b_country", q.getBilling_country()); 
    values.put("q_s_street", q.getShipping_street()); 
    values.put("q_s_city", q.getShipping_city()); 
    values.put("q_s_state", q.getShipping_state()); 
    values.put("q_s_zipcode", q.getShipping_zipcode()); 
    values.put("q_s_country", q.getShipping_country()); 
    values.put("q_day", q.getDay()); 
    values.put("q_month", q.getMonth()); 
    values.put("q_year", q.getYear()); 
    values.put("q_total", q.getTotal()); 
    values.put("q_discount", q.getTotal_discount()); 

    myDataBase.insert("quote",null,values); 

    }catch(Exception e) 
    { 
     Log.e("bderror",e.toString()); 
     e.printStackTrace(); 
    } 

    String selectQuery = "SELECT quote_id FROM quote WHERE q_customer_email=?"; 
    Cursor cursor = myDataBase.rawQuery(selectQuery, new String[] {q.getCustomer_email()}); 
    int i=0; 
    while(cursor.moveToNext()) 
    { 
     i=cursor.getInt(0); 
    } 
    cursor.close(); 
    myDataBase.beginTransaction(); 
    for (int j = 0; j < put_q_list.size(); j++) { 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map = put_q_list.get(j); 

     //Each value by using this method 
     String item_code = map.get("item_code_final"); 

     ContentValues cv = new ContentValues(); 
     cv.put("quote_id", i); 
     cv.put("q_item_code", item_code); 
     myDataBase.insert("q_item", null, cv); 
    } 
    myDataBase.setTransactionSuccessful(); 
    myDataBase.endTransaction(); 
    myDataBase.close(); 
} 

只有的值 「i」 被插入!不是產品代碼 請幫忙!!!

+0

什麼是你的代碼,你用過的?? – MDMalik

+0

編輯問題 – DICER45

+0

現在試試,我想你的'i'變量是問題\ – MDMalik

回答

0

試試這個。它應該工作。

for (int i = 0; i < mapList.size(); i++) { 
    HashMap<String, String> map = new HashMap<String, String>(); 
    map = mapList.get(i); 

    ContentValues cv = new ContentValues(); 
    cv.put("quote_id", i); 
    cv.put("q_item_code", map.get("item_code_final")); 
    cv.put("q_item_desc", map.get("desc_final")); 
    cv.put("q_item_price", map.get("item_price_final")); 
    cv.put("q_item_qty", map.get("item_qty_final")); 
    cv.put("q_item_tax", map.get("item_tax_final")); 
    cv.put("q_item_discount", map.get("item_discount_final")); 
    db.insert("tablename", null, cv); 
} 
db.close(); 
+0

仍然值爲空 – DICER45

+0

@ user3319217做的事情,而不是使用map.get(「...」);直接到數據庫,首先把它放在一個字符串Log.i上,這樣你就知道你是否得到了這個值。然後插入它。它更像是知道錯誤究竟在哪裏 – MDMalik

0

您應該首先聲明一個數據庫實例來處理和開始事務。如下

SQLiteDatabase db = mDbHelper.getDatabase(); 
db.beginTransaction(); 

然後執行循環

for(HashMap<String, String> map : mylist){ 
ContentValues cv = new ContentValues(); 
cv.put(FILE_NAME, map.get(FILE_NAME)); 
cv.put(DESC, map.get(DESC)); 
cv.put(UPLOADED_BY, map.get(DATE_UPLOADED)); 
cv.put(ACTION, map.get(FILE_NAME)); 
cv.put(ID, map.get(ID)); 
cv.put(FILE_URI, map.get(FILE_URI)); 
db.insert("tablename", null, cv); 
} 

然後關閉cursor

db.setTransactionSuccessful(); 
db.endTransaction(); 
db.close(); 

那是你的鏈接清楚公認的答案

+0

我已經把代碼 沒有得到一個錯誤,但獲得數據庫中的空值 – DICER45