2017-05-25 179 views
-1

我有一個訂購應用程序,我正在使用SQLite來存儲用戶添加到購物車的內容。我的問題是,我將如何能夠將SQLite數據庫中的所有數據上傳到服務器。將SQLite數據插入MySql數據庫

所以說,當用戶按下ORDER按鈕時,它會將SQLite中的所有數據上傳到數據庫。

我這樣做是爲了讓將要交付的人可以檢索和查看用戶的訂單詳情,但我只是困惑不已,並且卡在如何能夠接近或執行此操作。

如果任何人有任何洞察力或鏈接,可以給我一個想法,或者可能提供給我一種替代方法,它會很好。在此先感謝大家! :d

嗯,我解決我的問題,這是我的人誰需要它的解決方案!

Cursor data = db.getCartItems(); 
     TotalOrderArray = new JSONArray(); 
     data.moveToFirst(); 
     while(data.isAfterLast() == false) 
     { 
      int totalColumn = data.getColumnCount(); 
      TotalOrderObject = new JSONObject(); 
      for (int i = 0; i < totalColumn; i++) 
      { 
       try { 
        TotalOrderObject.put(data.getColumnName(i), data.getString(i)); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      TotalOrderArray.put(TotalOrderObject); 
      data.moveToNext(); 
     } 
     data.close(); 

它獲得所有在SQLite數據庫作爲JSON數據,然後我只是把它上傳到服務器。感謝所有幫助過的人!

+0

您是否使用過任何HTTP庫,例如Volley,Retrofit,LoopJ將數據發佈到服務器? – tahsinRupam

+0

SQLite獲取數據並轉換JSON字符串並傳遞json解析。這麼簡單 –

+0

@tahsinRupam是的,先生我正在使用凌空。 –

回答

1

第1步:只需從SQLite database獲取數據。

步驟2:將此data編碼爲JSON格式。 如: -

ArrayList<Product_bean > all_product_list = db.getAllProductInCart(cart_id); // here get the products 

JSONArray jarray = new JSONArray(); 
JSONObject product; 

for(Product_bean bean : all_product_list){ 
product = new JSONObject(); 
product.put("product_id",bean.getP_id()); 
...... 
} 
jarray.put(product); 

jarray is your request jSON. 

步驟3:發送JSONHttp...網址。

+0

因此,如果我有數據庫中有多個數據,我會顯示它們在一個列表視圖,然後檢索每一行上傳到數據庫,然後檢查列表視圖是否爲空,然後它會停止上傳?方法正確或錯誤?啊啊。 –

+0

@GionneLapuz ...首先檢索來自特定CART的sqlite的所有數據..編碼爲json併發送。問題是什麼。爲什麼需要列表視圖?可能是,我沒有得到你的要求。 –

+0

好吧,先生明白了,謝謝你的例子先生! :D –