QueryDb
package com.activity.db; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.activity.bean.Dish; import com.activity.bean.Table; import com.activity.myclientserver.R; import com.acyivity.Utils.Utils; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class QueryDb { /** * 数据库 */ public SQLiteDatabase database; public MyDataBase mdb; Context context; public QueryDb(Context context) { this.context = context; } /** * * * @return */ public List<Dish> getDish() { mdb = new MyDataBase(context); database = mdb.getReadableDatabase(); Cursor cur = database.query(Utils.CAI_DAN, null, null, null, null, null, null); List<Dish> list = null; if (cur != null) { list = new ArrayList<Dish>(); while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex("dish_id")); String dish_name = cur.getString(cur .getColumnIndex("dish_name")); int dish_num = cur.getInt(cur.getColumnIndex("dish_num")); String dish_class = cur.getString(cur .getColumnIndex("dish_class")); String imgPath = cur.getString(cur.getColumnIndex("image")); String describe = cur.getString(cur.getColumnIndex("itrInfo")); int price = cur.getInt(cur.getColumnIndex("price")); Dish d = new Dish(); d.setDish_id(id); d.setDiah_name(dish_name); d.setDish_class(dish_class); d.setDish_num(dish_num); d.setImage(imgPath); d.setItrInfo(describe); d.setPrice(price); list.add(d); } database.close(); cur.close(); mdb.close(); } return list; } /** * * * @return */ public List<Table> getTableList() { mdb = new MyDataBase(context); database = mdb.getReadableDatabase(); Cursor cur = database.query(Utils.TABLE, null, null, null, null, null, null); List<Table> list = null; if (cur != null) { list = new ArrayList<Table>(); while (cur.moveToNext()) { String name = cur.getString(cur.getColumnIndex("table_num")); String address = cur.getString(cur .getColumnIndex("table_address")); String num = cur.getString(cur.getColumnIndex("waiter_num")); String type = cur.getString(cur.getColumnIndex("table_name")); Table t = new Table(); t.setAddress(address); t.setTable_name(type); t.setTable_num(name); t.setSize(num); list.add(t); } database.close(); cur.close(); mdb.close(); } return list; } /** * * @return */ public List<Map<String, Object>> getTableInfo() { mdb = new MyDataBase(context); database = mdb.getReadableDatabase(); List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); String sql = "select table_num,waiter_num from " + Utils.TABLE; Cursor cur = database.rawQuery(sql, null); if (cur != null) { cur.moveToFirst(); for (int x = 0; x < cur.getCount(); x++) { Map<String, Object> map = new HashMap<String, Object>(); String str = cur.getString(1); switch (Integer.parseInt(str)) { case 4: map.put("ImageItem", R.drawable.icon1); break; case 6: map.put("ImageItem", R.drawable.icon6); break; case 8: map.put("ImageItem", R.drawable.icon2); break; case 10: map.put("ImageItem", R.drawable.icon2); break; case 12: map.put("ImageItem", R.drawable.icon3); break; case 20: map.put("ImageItem", R.drawable.icon7); break; } map.put("textTable", "楼层+桌号" + cur.getString(0)); list.add(map); cur.moveToNext(); } } database.close(); cur.close(); mdb.close(); return list; } /** * * * @return */ public List<Dish> getOrders() { mdb = new MyDataBase(context); database = mdb.getReadableDatabase(); Cursor cur = database.query(Utils.ORDER, null, null, null, null, null, null); List<Dish> list = null; if (cur != null) { list = new ArrayList<Dish>(); while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex("dish_id")); String dish_name = cur.getString(cur .getColumnIndex("dish_name")); int dish_num = cur.getInt(cur.getColumnIndex("dish_num")); String dish_class = cur.getString(cur .getColumnIndex("dish_class")); int price = cur.getInt(cur.getColumnIndex("price")); String remark=cur.getString(cur.getColumnIndex("remark")); Dish d = new Dish(); d.setDish_id(id); d.setDiah_name(dish_name); d.setDish_class(dish_class); d.setDish_num(dish_num); d.setPrice(price); d.setRemark(remark); list.add(d); } database.close(); cur.close(); mdb.close(); } return list; } /** * * @return */ public List<Dish> getLiuShuiHao() { mdb = new MyDataBase(context); database = mdb.getReadableDatabase(); Cursor cur = database.query(Utils.LIU_SHUI_HAO, null, null, null, null, null, null); List<Dish> list = null; if (cur != null) { list = new ArrayList<Dish>(); while (cur.moveToNext()) { String liushuihao = cur.getString(cur.getColumnIndex("liushuihao")); String remark=cur.getString(cur.getColumnIndex("remark")); Dish d = new Dish(); d.setLiushuihao(liushuihao); d.setRemark(remark); list.add(d); } database.close(); cur.close(); mdb.close(); } return list; } /** * * * @param tableNum * @return */ public List<Dish> getXiaDan() { mdb = new MyDataBase(context); List<Dish> list = null; database = mdb.getReadableDatabase(); String sql = "select dish_name,price,dish_num from xiadan,liushuibiao where xiadan.liushuihao=liushuibiao.liushuihao"; Cursor cur = database.rawQuery(sql,null); if (cur != null) { list = new ArrayList<Dish>(); while (cur.moveToNext()) { Dish d = new Dish(); d.setDiah_name(cur.getString(0)); d.setPrice(cur.getInt(1)); d.setDish_num(cur.getInt(2)); list.add(d); } } database.close(); cur.close(); mdb.close(); return list; } // 清空表数据 public void Truncate(String tablename) { mdb = new MyDataBase(context); database = mdb.getWritableDatabase(); String sql = "delete from " + tablename; database.execSQL(sql); database.close(); mdb.close(); } }