Android(安卓)订餐APP(Sqlite数据库,完整的增删改查)

 Android(安卓)订餐APP(Sqlite数据库,完整的增删改查)

一.背景

    首先说说项目的背景,基于地铁口鸡蛋饼的一个小项目,APP中固定设置的鸡蛋饼搭配,顾客也可以自己搭配

二.开发环境

  • AndroidStudio3.0.1
  • JDK1.8
  • 语言:JAVA
  • 数据存储:Sqlite

三.功能

  • 注册、登录、管理员、普通用户
  • 首页:鸡蛋饼食材搭配
  • 订单:购买订单展示
  • 个人/管理:个人信息、用户信息、机器信息、订单管理、食材管理、退出登录等
  • 技术知识:Sqlite增删改查、glide图片加载、Spinner、EventBus、butterknife等等

四.功能图

                    

                    

                 

五.Sqlite数据库存储(增删改查)

字段值数据类型说明
idtext主键值
nickNametext用户名
passwordtext密码
pathtext头像
qianmingtext签名
phonetext手机号
birthdaytext生日
sextext性别
typetext用户类型

 

 

 

 

 

 

 

 

 

 

                                                                                    用户表

代码如下:

public class DbSqliteHelper extends SQLiteOpenHelper {

    public DbSqliteHelper(Context ctx) {
        super(ctx, "JianBin", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        //用户数据表
        db.execSQL("CREATE TABLE if not exists user(id integer PRIMARY KEY autoincrement,"
                + " nickName text, password text, path text, qianming text, phone text, birthday text, sex text, beizhu text,type text)");

        //鸡蛋饼订单信息数据表
        db.execSQL("CREATE TABLE if not exists jiqidingdan(id integer PRIMARY KEY autoincrement,"
                + "dingdanbianhao  text,userNmae text, binName text, lajiao text, salajiang text, fanqie text, zuoliao text, xiangcong text, bianhao text, price text, shuliang text, time text,zhuangtai text,huitui text,wanzi text,jidan text)");


        db.execSQL("CREATE TABLE if not exists jiqibin(id integer PRIMARY KEY autoincrement,"
                + "jiqiname text, jiqiweizhi text)");
        //机器存储数据表
        db.execSQL("CREATE TABLE if not exists jiqi(id integer PRIMARY KEY autoincrement,"
                + " title text,jiqitupian text, jiqiname text, jiqiweizhi text,lajiao text,salajiang text,fanqie text,xiangcong text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private static DbSqliteHelper dbManager;


    public static DbSqliteHelper getInstance(Context ctx) {
        if (dbManager == null) {
            synchronized (DbSqliteHelper.class) {
                if (dbManager == null) {
                    dbManager = new DbSqliteHelper(ctx);
                }
            }
        }
        return dbManager;
    }

    public boolean saveUser(UserBean bean) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            //注册之前先查询是否重复注册
            Cursor cursor = db.rawQuery("SELECT * FROM user WHERE nickName = ?", new String[]{bean.getNickName()});
            boolean hasUser = false;
            if (cursor.moveToNext()) {
                hasUser = true;
            }
            cursor.close();
            if (hasUser) {
                return true;
            }
            //如果不重复则注册
            db.execSQL("INSERT INTO user(nickName , password , path , qianming , phone , birthday , sex , beizhu,type) " +
                    "VALUES ('" + bean.getNickName()
                    + "', '" + bean.getPassword()
                    + "', '" + bean.getPath()
                    + "', '" + bean.getQianming()
                    + "', '" + bean.getPhone()
                    + "', '" + bean.getBirthday()
                    + "', '" + bean.getSex()
                    + "', '" + bean.getBeizhu()
                    + "', '" + bean.getType()
                    + "')");
        }
        return false;
    }


    /**
     * 查找用户(登录操作)
     *
     * @return 用户
     */
    public UserBean findUser(String[] args) {
        UserBean bean = new UserBean();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            Cursor cursor = db.query("user", null, "nickName = ?", args, null, null, null);
            if (cursor.moveToNext()) {
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setNickName(cursor.getString(cursor.getColumnIndex("nickName")));

                bean.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                bean.setPath(cursor.getString(cursor.getColumnIndex("path")));

                bean.setQianming(cursor.getString(cursor.getColumnIndex("qianming")));

                bean.setPhone(cursor.getString(cursor.getColumnIndex("phone")));

                bean.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));

                bean.setSex(cursor.getString(cursor.getColumnIndex("sex")));

                bean.setBeizhu(cursor.getString(cursor.getColumnIndex("beizhu")));


                bean.setType(cursor.getString(cursor.getColumnIndex("type")));

            }
            cursor.close();

        }
        return bean;
    }

    /**
     * 更新用户信息
     *
     * @param record
     */
    public void updateUser(UserBean record) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", record.getId());
        contentValues.put("nickName", record.getNickName());
        contentValues.put("password", record.getPassword());
        contentValues.put("path", record.getPath());
        contentValues.put("qianming", record.getQianming());
        contentValues.put("phone", record.getPhone());
        contentValues.put("birthday", record.getBirthday());
        contentValues.put("sex", record.getSex());
        contentValues.put("beizhu", record.getBeizhu());
        contentValues.put("type", record.getType());
        if (db != null) {
            db.update("user", contentValues, "id = ?", new String[]{record.getId()+""});
        }
    }
    /**
     * 查询用户数据
     *
     * @return
     */
    public List<UserBean> getuser(String name) {
        List<UserBean> userBeanList = new ArrayList<UserBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            //查询记录
            String[] selectioinArgs = {"%"+name+"%"};//注意:这里没有单引号
            String sql = "SELECT * FROM user" + " where nickName like ? ";
            Cursor cursor = db.rawQuery(sql, selectioinArgs);
            while (cursor.moveToNext()) {
                UserBean bean = new UserBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setNickName(cursor.getString(cursor.getColumnIndex("nickName")));

                bean.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                bean.setPath(cursor.getString(cursor.getColumnIndex("path")));

                bean.setQianming(cursor.getString(cursor.getColumnIndex("qianming")));

                bean.setPhone(cursor.getString(cursor.getColumnIndex("phone")));

                bean.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));

                bean.setSex(cursor.getString(cursor.getColumnIndex("sex")));

                bean.setBeizhu(cursor.getString(cursor.getColumnIndex("beizhu")));


                bean.setType(cursor.getString(cursor.getColumnIndex("type")));

                userBeanList.add(bean);
            }
            cursor.close();
        }
        return userBeanList;
    }
    /**
     * 获取所有用户
     * @return
     */
    public List<UserBean> getAllUser() {
        List<UserBean> userBeanList = new ArrayList<UserBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            Cursor cursor = db.rawQuery("SELECT * FROM user ", null);
            while (cursor.moveToNext()) {
                UserBean bean = new UserBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setNickName(cursor.getString(cursor.getColumnIndex("nickName")));

                bean.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                bean.setPath(cursor.getString(cursor.getColumnIndex("path")));

                bean.setQianming(cursor.getString(cursor.getColumnIndex("qianming")));

                bean.setPhone(cursor.getString(cursor.getColumnIndex("phone")));

                bean.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));

                bean.setSex(cursor.getString(cursor.getColumnIndex("sex")));

                bean.setBeizhu(cursor.getString(cursor.getColumnIndex("beizhu")));


                bean.setType(cursor.getString(cursor.getColumnIndex("type")));

                userBeanList.add(bean);
            }
            cursor.close();
        }
        return userBeanList;
    }

    /**
     * 删除用户
     *
     * @param id
     */
    public void deleteUser(int id) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            db.execSQL("DELETE FROM user WHERE id = " + id);
        }
    }
    //存储机器数据
    public void saveJiQi(JiQiBean bean) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("INSERT INTO jiqi(title,jiqitupian , jiqiname , jiqiweizhi,lajiao ,salajiang ,fanqie ,xiangcong ) " +
                "VALUES ('" + bean.getTitle()
                + "', '" + bean.getJiqitupian()
                + "', '" + bean.getJiqiname()
                + "', '" + bean.getJiqiweizhi()
                + "', '" + bean.getLajiao()
                + "', '" + bean.getSalajiang()
                + "', '" + bean.getFanqie()
                + "', '" + bean.getXiangcong()
                + "')");


    }
    /**
     * 获取所有jiq数据
     *
     * @return
     */
    public List<JiQiBean> getAllJiQi() {
        List<JiQiBean> records = new ArrayList<JiQiBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            Cursor cursor = db.rawQuery("SELECT * FROM jiqi ", null);
            while (cursor.moveToNext()) {
                JiQiBean bean = new JiQiBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));

                bean.setJiqitupian(cursor.getInt(cursor.getColumnIndex("jiqitupian")));
                bean.setJiqiname(cursor.getString(cursor.getColumnIndex("jiqiname")));
                bean.setJiqiweizhi(cursor.getString(cursor.getColumnIndex("jiqiweizhi")));
                bean.setLajiao(cursor.getString(cursor.getColumnIndex("lajiao")));

                bean.setSalajiang(cursor.getString(cursor.getColumnIndex("salajiang")));

                bean.setFanqie(cursor.getString(cursor.getColumnIndex("fanqie")));

                bean.setXiangcong(cursor.getString(cursor.getColumnIndex("xiangcong")));

                records.add(bean);
            }
            cursor.close();
        }
        return records;
    }

    /**
     * 查询鸡蛋饼数据
     *
     * @return
     */
    public List<JiQiBean> getJiQi(String name) {
        List<JiQiBean> jiQiBeanList = new ArrayList<JiQiBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            //查询记录
            String[] selectioinArgs = {"%"+name+"%"};//注意:这里没有单引号
            String sql = "SELECT * FROM jiqi" + " where jiqiname like ? ";
            Cursor cursor = db.rawQuery(sql, selectioinArgs);
            while (cursor.moveToNext()) {
                JiQiBean bean = new JiQiBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                bean.setJiqitupian(cursor.getInt(cursor.getColumnIndex("jiqitupian")));
                bean.setJiqiname(cursor.getString(cursor.getColumnIndex("jiqiname")));
                bean.setJiqiweizhi(cursor.getString(cursor.getColumnIndex("jiqiweizhi")));
                bean.setLajiao(cursor.getString(cursor.getColumnIndex("lajiao")));
                bean.setSalajiang(cursor.getString(cursor.getColumnIndex("salajiang")));
                bean.setFanqie(cursor.getString(cursor.getColumnIndex("fanqie")));
                bean.setXiangcong(cursor.getString(cursor.getColumnIndex("xiangcong")));

                jiQiBeanList.add(bean);
            }
            cursor.close();
        }
        return jiQiBeanList;
    }

    /**
     * 更新机器信息
     *
     * @param jiQiBean
     */
    public void updateQiji(JiQiBean jiQiBean) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", jiQiBean.getId());
        contentValues.put("title", jiQiBean.getTitle());
        contentValues.put("jiqitupian", jiQiBean.getJiqitupian());
        contentValues.put("jiqiname", jiQiBean.getJiqiname());
        contentValues.put("jiqiweizhi", jiQiBean.getJiqiweizhi());
        contentValues.put("lajiao", jiQiBean.getLajiao());
        contentValues.put("salajiang", jiQiBean.getSalajiang());
        contentValues.put("fanqie", jiQiBean.getFanqie());
        contentValues.put("xiangcong", jiQiBean.getXiangcong());

        if (db != null) {
            db.update("jiqi", contentValues, "id = ?", new String[]{jiQiBean.getId()+""});
        }
    }





    /**
     * 删除机器信息
     *
     * @param id
     */
    public void deleteJiQi(int id) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            db.execSQL("DELETE FROM jiqi WHERE id = " + id);
        }
    }
    //存储鸡蛋饼信息数据
    public void saveJiQiBing(BingBean bean) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("INSERT INTO jiqibin(jiqiname , jiqiweizhi) " +
                "VALUES ('" + bean.getJiqiname()
                + "', '" + bean.getJiqiweizhi()

                + "')");


    }
    /**
     * 更新机器信息
     *
     * @param jiQiBean
     */
    public void updateJiQiBing(BingBean jiQiBean) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", jiQiBean.getId());
        contentValues.put("jiqiname", jiQiBean.getJiqiname());
        contentValues.put("jiqiweizhi", jiQiBean.getJiqiweizhi());


        if (db != null) {
            db.update("jiqibin", contentValues, "id = ?", new String[]{jiQiBean.getId()+""});
        }
    }

    //模糊查询机器信息
    public List<BingBean> getJiQiBean(String name) {
        List<BingBean> jiQiBeanList = new ArrayList<BingBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            //查询记录
            String[] selectioinArgs = {"%"+name+"%"};//注意:这里没有单引号
            String sql = "SELECT * FROM jiqibin" + " where jiqiname like ? ";
            Cursor cursor = db.rawQuery(sql, selectioinArgs);
            while (cursor.moveToNext()) {
                BingBean bean = new BingBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setJiqiname(cursor.getString(cursor.getColumnIndex("jiqiname")));
                bean.setJiqiweizhi(cursor.getString(cursor.getColumnIndex("jiqiweizhi")));

                jiQiBeanList.add(bean);
            }
            cursor.close();
        }
        return jiQiBeanList;
    }
    /**
     * 获取所有鸡蛋饼数据
     *
     * @return
     */
    public List<BingBean> getAllJiQiBing() {
        List<BingBean> records = new ArrayList<BingBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            Cursor cursor = db.rawQuery("SELECT * FROM jiqibin ", null);
            while (cursor.moveToNext()) {
                BingBean bean = new BingBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setJiqiname(cursor.getString(cursor.getColumnIndex("jiqiname")));
                bean.setJiqiweizhi(cursor.getString(cursor.getColumnIndex("jiqiweizhi")));

                records.add(bean);
            }
            cursor.close();
        }
        return records;
    }
    /**
     * 删除机器信息
     *
     * @param id
     */
    public void deleteJiQiBean(int id) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            db.execSQL("DELETE FROM jiqibin WHERE id = " + id);
        }
    }

    //存储鸡蛋饼订单信息数据
    public void saveJiQiBingDingDan(DingDanBean bean) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("INSERT INTO jiqidingdan(dingdanbianhao,userNmae , binName , lajiao , salajiang , fanqie, zuoliao, xiangcong, bianhao, price, shuliang, time,zhuangtai,huitui,wanzi,jidan) " +
                "VALUES ('" + bean.getDingdanbianhao()
                + "', '" + bean.getUserNmae()
                + "', '" + bean.getBinName()
                + "', '" + bean.getLajiao()
                + "', '" + bean.getSalajiang()
                + "', '" + bean.getFanqie()
                + "', '" + bean.getZuoliao()
                + "', '" + bean.getXiangcong()
                + "', '" + bean.getBianhao()
                + "', '" + bean.getPrice()
                + "', '" + bean.getShuliang()
                + "', '" + bean.getTime()
                + "', '" + bean.getZhuangtai()
                + "', '" + bean.getHuitui()
                + "', '" + bean.getWanzi()
                + "', '" + bean.getJidan()
                + "')");


    }

    /**
     * 获取所有鸡蛋饼订单数据
     *
     * @return
     */
    public List<DingDanBean> getAllJiQiBingDingDan(String user) {
        List<DingDanBean> records = new ArrayList<DingDanBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            Cursor cursor=null;
            if (TextUtils.isEmpty(user)){
                 cursor = db.rawQuery("SELECT * FROM jiqidingdan", null);

            }else {
                 cursor = db.rawQuery("SELECT * FROM jiqidingdan  WHERE  userNmae = ?", new String[]{user});

            }
            while (cursor.moveToNext()) {
                DingDanBean bean = new DingDanBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setDingdanbianhao(cursor.getString(cursor.getColumnIndex("dingdanbianhao")));
                bean.setUserNmae(cursor.getString(cursor.getColumnIndex("userNmae")));
                bean.setBinName(cursor.getString(cursor.getColumnIndex("binName")));
                bean.setLajiao(cursor.getString(cursor.getColumnIndex("lajiao")));
                bean.setSalajiang(cursor.getString(cursor.getColumnIndex("salajiang")));
                bean.setFanqie(cursor.getString(cursor.getColumnIndex("fanqie")));
                bean.setZuoliao(cursor.getString(cursor.getColumnIndex("zuoliao")));
                bean.setXiangcong(cursor.getString(cursor.getColumnIndex("xiangcong")));
                bean.setBianhao(cursor.getString(cursor.getColumnIndex("bianhao")));
                bean.setPrice(cursor.getString(cursor.getColumnIndex("price")));
                bean.setShuliang(cursor.getString(cursor.getColumnIndex("shuliang")));
                bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                bean.setZhuangtai(cursor.getString(cursor.getColumnIndex("zhuangtai")));
                bean.setHuitui(cursor.getString(cursor.getColumnIndex("huitui")));
                bean.setWanzi(cursor.getString(cursor.getColumnIndex("wanzi")));
                bean.setJidan(cursor.getString(cursor.getColumnIndex("jidan")));

                records.add(bean);
            }
            cursor.close();
        }
        return records;
    }

    /**
     * 获取订单数据
     *
     * @return
     */
    public List<DingDanBean> getDingDan(String name) {
        List<DingDanBean> records = new ArrayList<DingDanBean>();
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            //查询记录
            String[] selectioinArgs = {"%"+name+"%"};//注意:这里没有单引号
            String sql = "SELECT * FROM jiqidingdan" + " where dingdanbianhao like ? ";
            Cursor cursor = db.rawQuery(sql, selectioinArgs);            while (cursor.moveToNext()) {
                DingDanBean bean = new DingDanBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setDingdanbianhao(cursor.getString(cursor.getColumnIndex("dingdanbianhao")));
                bean.setUserNmae(cursor.getString(cursor.getColumnIndex("userNmae")));
                bean.setBinName(cursor.getString(cursor.getColumnIndex("binName")));
                bean.setLajiao(cursor.getString(cursor.getColumnIndex("lajiao")));
                bean.setSalajiang(cursor.getString(cursor.getColumnIndex("salajiang")));
                bean.setFanqie(cursor.getString(cursor.getColumnIndex("fanqie")));
                bean.setZuoliao(cursor.getString(cursor.getColumnIndex("zuoliao")));
                bean.setXiangcong(cursor.getString(cursor.getColumnIndex("xiangcong")));
                bean.setBianhao(cursor.getString(cursor.getColumnIndex("bianhao")));
                bean.setPrice(cursor.getString(cursor.getColumnIndex("price")));
                bean.setShuliang(cursor.getString(cursor.getColumnIndex("shuliang")));
                bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                bean.setZhuangtai(cursor.getString(cursor.getColumnIndex("zhuangtai")));
                bean.setHuitui(cursor.getString(cursor.getColumnIndex("huitui")));
                bean.setWanzi(cursor.getString(cursor.getColumnIndex("wanzi")));
                bean.setJidan(cursor.getString(cursor.getColumnIndex("jidan")));

                records.add(bean);
            }
            cursor.close();
        }
        return records;
    }
    /**
     * 删除订单
     *
     * @param id
     */
    public void deleteDingDan(int id) {
        SQLiteDatabase db = getWritableDatabase();
        if (db != null) {
            db.execSQL("DELETE FROM jiqidingdan WHERE id = " + id);
        }
    }
}

本项目当毕业设计,如果需要定制开发可加QQ:2258629201

体验APK链接:链接:https://pan.baidu.com/s/1o0qNI04n7diqh9K1ZmREqw
                         提取码:in0n

成品源码   链接:https://m.tb.cn/h.3i6yhV2?sm=78a5f7

联系QQ:2258629201

  • 12
    点赞
  • 119
    收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论 6

打赏作者

码大大

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值