前言
在开发一个点餐软件时,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 }