前言
在開發一個點餐軟件時,app的訂單數據是使用本地Sqlite數據庫,在提交訂單數據后,當訂單數據在后台(Mysql數據庫)發生變化時(如:已買單),本地數據如何改變呢?
思路
前台在查詢時,將后台訂單數據以數組方式發送到前台進行更新(update)
實現
服務端實現
定義一類訂單類
package yybwb;
public class orderdetail {
public Integer id; //,
public Integer orderID;
public Integer menuID;
public String num;
public Integer state;
public String Tno; // //
public String remark;
public orderdetail(int id,int orderID, int menuID,String num, int state,String Tno,String remark)
{
this.id = id;
this.orderID = orderID;
this.menuID = menuID;
this.num = num;
this.state = state;
this.Tno= Tno;
this.remark= remark;
}
}
通過一方法獲取訂單信息
1 public static ArrayList<orderdetail> getorderdetaillists(String Tabno){ 2 ArrayList< orderdetail> result = new ArrayList< orderdetail>(); 3 Connection con = null; //聲明Connection對象 4 PreparedStatement ps = null; 5 ResultSet rs = null; 6 /* 7 * tabletal的數據在新訂單生成時修改 8 */ 9 String sql = "select orderID,menuID,num,state,Tno,remark from orderdetailtal where state=1"; 10 11 try{ 12 con = getConnection(); //獲得連接 13 ps = con.prepareStatement(sql); //獲得預編譯語句 14 //ps.setInt(1, Integer.valueOf(r_id)); //設置參數 15 rs = ps.executeQuery(); //執行查詢 16 while(rs.next()){ 17 18 String remark = rs.getString(6); 19 String Tno = rs.getString(5); 20 // int orderID = rs.getInt(1); 21 int menuID = rs.getInt(2); 22 double num = rs.getDouble(3); 23 String.format("%.2f", num); //1.23 24 int state =rs.getInt(4); 25 26 //new String(rs.getString(2).getBytes("ISO-8859-1"),CHAR_ENCODING); 27 // new String(rs.getString(3).getBytes("ISO-8859-1"),CHAR_ENCODING); 28 orderdetail c = new orderdetail(1,1,menuID, String.format("%.2f", num),state,Tno,remark); 29 result.add(c); 30 31 //System.out.println(Tno+','+remark); 32 } 33 System.out.println("get orderdetail lists"); 34 }catch(Exception e){ 35 e.printStackTrace(); 36 } 37 finally{ 38 try{ 39 if(rs != null){ 40 rs.close(); 41 rs = null; 42 } 43 }catch(Exception e){ 44 e.printStackTrace(); 45 } 46 try{ 47 if(ps != null){ 48 ps.close(); 49 ps = null; 50 } 51 }catch(Exception e){ 52 e.printStackTrace(); 53 } 54 try{ 55 if(con != null){ 56 con.close(); 57 con = null; 58 } 59 }catch(Exception e){ 60 e.printStackTrace(); 61 } 62 } 63 return result; 64 } 65
將訂單信息傳遞到客戶端
1 else if(msg.startsWith("<#GET_ORDERDETAIL#>")){//消息為獲取台表2014-7-22 2 msg = msg.substring(19); //提取內容 3 ArrayList<orderdetail> cmList = DBUtil.getorderdetaillists(msg); //獲得台列表 4 int size = cmList.size(); //個數 5 dout.writeInt(size); //返回個數 6 String reply = din.readUTF(); //等待客戶端反饋 7 if(reply.equals("<#READY_TO_ORDERDETAIL#>")){ //如果客戶端已經准備好 8 for(orderdetail c:cmList){ 9 StringBuilder sb = new StringBuilder(); 10 11 sb.append(String.valueOf(c.id)); 12 sb.append("|"); 13 sb.append(String.valueOf(c.orderID)); 14 sb.append("|"); 15 sb.append(String.valueOf(c.menuID)); 16 sb.append("|"); 17 sb.append(c.num); 18 sb.append("|"); 19 sb.append(String.valueOf(c.state)); 20 sb.append("|"); 21 sb.append(c.Tno); 22 sb.append("|"); 23 sb.append(c.remark); 24 25 dout.writeUTF(sb.toString()); //發送消息到客戶端 26 } 27 } 28 } 29
客戶端實現
定義一個訂單類
1 package com.realhope.rmeal.bean; 2 3 /** 4 * 5 * @author Wuchunyuan 6 * 訂單明細類 7 */ 8 public class OrderDetail{ 9 private Integer _id; 10 private Integer orderID; 11 private Integer menuID; 12 private String num; // 數量 13 private Integer state; // 狀態 14 private String remark; // 備注 15 private String Tno; 16 17 public OrderDetail() { 18 super(); 19 } 20 public OrderDetail(Integer _id, Integer orderID, Integer menuID, 21 String num, Integer state, String Tno,String remark) { 22 super(); 23 this._id = _id; 24 this.orderID = orderID; 25 this.menuID = menuID; 26 this.num = num; 27 this.state = state; 28 this.remark = remark; 29 this.Tno = Tno; 30 } 31 public Integer get_id() { 32 return _id; 33 } 34 public void set_id(Integer _id) { 35 this._id = _id; 36 } 37 public Integer getOrderID() { 38 return orderID; 39 } 40 public void setOrderID(Integer orderID) { 41 this.orderID = orderID; 42 } 43 public Integer getMenuID() { 44 return menuID; 45 } 46 public void setMenuID(Integer menuID) { 47 this.menuID = menuID; 48 } 49 public String getNum() { 50 return num; 51 } 52 public void setNum(String num) { 53 this.num = num; 54 } 55 public Integer getState() { 56 return state; 57 } 58 public void setState(Integer state) { 59 this.state = state; 60 } 61 public String getRemark() { 62 return remark; 63 } 64 public void setRemark(String remark) { 65 this.remark = remark; 66 } 67 68 public String getTno() { 69 return Tno; 70 } 71 public void setTno(String Tno) { 72 this.Tno = Tno; 73 } 74 75 }
發送獲取訂單的消息到服務器,得到服務器發來數據
1 String msg = "<#GET_ORDERDETAIL#>"+TableN; 2 mc.dout.writeUTF(msg); //發出獲取新菜單表請求 3 mc.dout.flush(); 4 int size = mc.din.readInt(); //獲取個數 5 6 mc.dout.writeUTF("<#READY_TO_ORDERDETAIL#>"); 7 mc.dout.flush(); 8 9 for(int i=0;i<size;i++){ 10 msg = mc.din.readUTF(); //讀取每條信息 11 String [] sa = msg.split("\\|"); //切割字符串 12 13 OrderDetail OrderDetaillists = new OrderDetail(Integer.valueOf(sa[0]),Integer.valueOf(sa[1]), 14 Integer.valueOf(sa[2]),String.valueOf(sa[3]),Integer.valueOf(sa[4]),String.valueOf(sa[5]), 15 String.valueOf(sa[6])); 16 ((MenuActivity)mContext).lstDate_OrderDetailLists.add(OrderDetaillists); 17 } 18
執行SQl語句更新本地Sqlite數據庫
1 / * 2014-10-14 2 * @param lstDate_OrderDetailLists 3 */ 4 5 public void updateOrderDetail(List<OrderDetail> lstDate_OrderDetailLists){ 6 DBHelper dbM = new DBHelper(this.context); 7 SQLiteDatabase db = dbM.getReadableDatabase(); 8 db.execSQL("DROP TABLE IF EXISTS OrderDetailTal"); 9 //db.execSQL("delete from OrderDetailTal"); 10 int Count=lstDate_OrderDetailLists.size(); 11 //創建表 12 db.execSQL("CREATE TABLE " + OrderDetails.TABLE+ " (" 13 + OrderDetails._ID + " INTEGER PRIMARY KEY," 14 + OrderDetails.ORDERID+ " INTEGER," 15 + OrderDetails.MENUID+ " INTEGER," 16 + OrderDetails.NUM + " REAL," 17 + OrderDetails.STATE + " INTEGER," 18 + OrderDetails.Tno + " TEXT," 19 + OrderDetails.REMARK + " TEXT" 20 21 + ");"); 22 //逐一更新本地數據 23 for(int i=0 ; i<Count ;){ 24 db.execSQL("insert into "+OrderDetails.TABLE+" ("+OrderDetails.ORDERID+","+ OrderDetails.MENUID+","+ 25 OrderDetails.NUM +","+ 26 OrderDetails.STATE+","+OrderDetails.Tno +","+OrderDetails.REMARK +") " + 27 "values ('"+String.valueOf(lstDate_OrderDetailLists.get(i).getOrderID())+"','"+ 28 String.valueOf(lstDate_OrderDetailLists.get(i).getMenuID()) +"','"+ 29 lstDate_OrderDetailLists.get(i).getNum() +"','"+ 30 String.valueOf(lstDate_OrderDetailLists.get(i).getState()) + 31 "','"+String.valueOf(lstDate_OrderDetailLists.get(i).getTno()) + 32 "','"+String.valueOf(lstDate_OrderDetailLists.get(i).getRemark())+"')"); 33 i++; 34 } 35 36 db.close(); 37 return; 38 }
