浅入Android

浅入Android
  1. dcloud_uniplugins 注册
{
"type": "module",
"name": "PDUtils",
"class": "com.PanDianUtils"
}

2.新增数据

public static Boolean savaSheetGoodsInfo(Context context,SheetGoods options){
   boolean result=false;
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);SQLiteDatabase db = null;
    try{
        db = databaseUtil.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.clear();
        values.put("sheetId",options.getSheetId());
        values.put("goodid",options.getGoodsId());
        values.put("colorid",options.getColorId());
        values.put("longid",options.getLongId());
        values.put("sizeid",options.getSizeId());
        values.put("sizedesc",options.getSizedesc());
        values.put("colordesc",options.getColordesc());
        values.put("longdesc",options.getLongdesc());
        values.put("goodsno",options.getGoodsno());
        values.put("filedname",options.getFieldName());
        values.put("goodsname",options.getGoodsName());
        values.put("colorcode",options.getColorCode());
        values.put("quantity", options.getQuantity());
        values.put("scanTime", System.currentTimeMillis());
        db.insert("scang", null, values);
         result =true;
     }catch (Exception ex){

     }finally {
        //关闭数据库
        if (db!=null){
            db.close();
        }
     }
        return result;
}

3.单表查询

public static GoodsInfo getGoodsInfo (Context context,String sheetId,String goodsId,String colorID,String LongId,String sizeId){
    GoodsInfo goodsInfo =new GoodsInfo();
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);
    SQLiteDatabase db = databaseUtil.getWritableDatabase();
    String boxId="";
    int count = 0;
    try {
            Cursor cursor = db.rawQuery("select sum(quantity) as sumquantity ,goodsno,goodsname,colordesc,longdesc,sizedesc"             +" from scang " +
            " where sheetId=?" +
            " and goodid=?"+
            " and colorid=?"+
            " and longid=?"+
            " and sizeid=?", new String[]{sheetId,goodsId,colorID,LongId,sizeId});
            while (cursor.moveToNext()) {
                goodsInfo.setYisaoshu (cursor.getInt(cursor.getColumnIndexOrThrow("sumquantity")));
                goodsInfo.setGoodsName(cursor.getString(cursor.getColumnIndexOrThrow("goodsname")));
                goodsInfo.setGoodsNo(cursor.getString(cursor.getColumnIndexOrThrow("goodsno")));
                goodsInfo.setBarcode("");
                goodsInfo.setColordesc(cursor.getString(cursor.getColumnIndexOrThrow("colordesc")));
                goodsInfo.setLongdesc(cursor.getString(cursor.getColumnIndexOrThrow("longdesc")));
                goodsInfo.setSizedesc(cursor.getString(cursor.getColumnIndexOrThrow("sizedesc")));
                goodsInfo.setChayishu(0);
          }

    } catch (Exception ex) {

    } finally {
        //关闭数据库
        if (db != null) {
            db.close();
        }
    }
    return goodsInfo;
}

4.单表删除

public static Boolean deleteUniCode(Context context,String unicode,String sheetId){
    boolean result = false;
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);
    SQLiteDatabase db = null;
    try{
        db = databaseUtil.getReadableDatabase();
        db.execSQL(" delete from uniquecode where unicode=? and sheetId=?", new String[]{unicode,sheetId});
        result = true;
    }catch (Exception ex){

    }finally {
        //关闭数据库
        if (db!=null){
        db.close();
    }
}
    return result;
}

5.左连接 + 子查询

public static Map getBottom(Context context,String sheetId){
    Map map = new HashMap<>();
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);
    SQLiteDatabase db = null;

    try {
        db = databaseUtil.getReadableDatabase();
        Cursor cursor = db.rawQuery(" select ifnull(a.quantity,0) as quantity,ifnull(b.kuanShu,0) as kuanShu from \n" +
        " ( " +
        " SELECT sum(quantity) as quantity ,sheetId,longDesc, colorDesc,sizeDesc,goodsNo,goodid,colorId,longId,sizeId \n" +
        " from scang where scang.sheetId = ? \n" +
        " group by scang.sheetId\n" +
        " ) a \n" +
        " left join \n" +
        " ( " +
        " select count(distinct goodsId) as kuanShu,sheetId,longDesc, colorDesc,sizeDesc,goodsNo,goodid ,colorId,longId,sizeId \n"     +
        " from scang \n" +
        " where scang.sheetId = ? \n" +
        " group by scang.sheetId,scang.goodid\n" +
        " ) b on a.sheetId= b.sheetId"
        , new String[]{sheetId,sheetId});
    while (cursor.moveToNext()){
         map.put("zongsaomiaoshu",cursor.getInt(cursor.getColumnIndexOrThrow("quantity")));
        map.put("kuanShu",cursor.getInt(cursor.getColumnIndexOrThrow("kuanShu")));
        return map;
    }
    }catch (Exception e){

    }finally {

    }
    return map;
}

6.查询结果(1)如果 货品在scangoods不存在也会直接赋值 quantity ; 查询结果(2)则是 0

1.     Cursor cursor = db.rawQuery("select a.sheetId, a.goodsname, a.goodsno,a.goodid, ifnull(b.quantity,0) as quantity from\n" +
"(\n" +
"SELECT sheetId, goodsname, goodsno,goodid, quantity\n" +
"from sheetg\n" +
"where sheetg.sheetId =?\n" +
"GROUP BY sheetg.goodid\n" +
")a\n" +
"LEFT JOIN\n" +
"(\n" +
"SELECT sum(quantity) as quantity ,goodid,sheetId\n" +
"from scang\n" +
"where scang.sheetId =?\n" +
")b on a.sheetId = b.sheetId "
, new String[]{sheetId,sheetId});
2.     Cursor cursor = db.rawQuery("select a.sheetId, a.goodsname, a.goodsno,a.goodid, ifnull(b.quantity,0) as quantity from\n" +
"(\n" +
"SELECT sheetId, goodsname, goodsno,goodid, quantity\n" +
"from sheetg\n" +
"where sheetg.sheetId =?\n" +
"GROUP BY sheetg.goodid\n" +
")a\n" +
"LEFT JOIN\n" +
"(\n" +
"SELECT sum(quantity) as quantity ,goodid,sheetId\n" +
"from scang\n" +
"where scang.sheetId =?\n" +
")b on a.sheetId = b.sheetId and a.goodid=b.goodid"
, new String[]{sheetId,sheetId});

7.绝对值取值 (quantity不管是正数还是负数,都会取正数)

Math.abs(quantity)

8.嵌套使用

public static List checkChouCode(Context context, String sheetId, String barcode){
    List list = new ArrayList<>();
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);
    SQLiteDatabase db = null;

    try {
        db = databaseUtil.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from barcode WHERE barcode=?",new String[]{barcode});
        while (cursor.moveToNext()){
        String goodsid = cursor.getString(cursor.getColumnIndexOrThrow("goodsid"));
        Cursor cursor1 = db.rawQuery("select * from sheetg WHERE goodsid=? and sheetId=?",new String[]{ goodsid,sheetId});
        while (cursor1.moveToNext()) {
            SheetGood sheetGoods = new SheetGood();
            sheetGoods.setGoodsno(cursor1.getString(cursor1.getColumnIndexOrThrow("goodsno")));
            sheetGoods.setQuantity(cursor1.getInt(cursor1.getColumnIndexOrThrow("quantity")));
            sheetGoods.setColordesc(cursor1.getString(cursor1.getColumnIndexOrThrow("colordesc")));
            sheetGoods.setSizedesc(cursor1.getString(cursor1.getColumnIndexOrThrow("sizedesc")));
            sheetGoods.setLongdesc(cursor1.getString(cursor1.getColumnIndexOrThrow("longdesc")));
            list.add(sheetGoods);
        }
    }
    }catch (Exception exception){
        String sheet=exception.getMessage();
    }finally {
        if (db!=null){
            db.close();
        }
    }
    return list;
}

9.更新表

public static void updateSheetBoxQuantity(Context context,String sheetId,Long boxId) {
    DatabaseUtil databaseUtil = new DatabaseUtil(context, null);
    SQLiteDatabase db = null;

    try {
        db = databaseUtil.getReadableDatabase();

        db.execSQL("update sheetb set jianNum=" +
            "(select sum(quantity) from sheetd where sheetId=? and boxId=?)" +
            "where sheetId=? and boxId=?"
            , new String[]{sheetId, String.valueOf(boxId), sheetId, String.valueOf(boxId)});
    } catch (Exception ex) {
        String EX = ex.getMessage();
    } finally {
        //关闭数据库
        if (db != null) {
            db.close();
        }
    }
}

10.判断某个字段是否未空

 if (!"".equals(zancun.getSheetCode())){}
  1. 自定义方法,直接用来在uni-app中调用
@JSMethod(uiThread = true) //表示 JScript 方法。 此类属于名称绑定类别
public void getSku(JSONObject options, JSCallback jsCallback){
    myCallback = jsCallback;
    int OriQuantity=PanDian.getOriQuantityByGoods(mWXSDKInstance.getContext()
    ,options.getString("sheetId")
    ,options.getString("goodsId")
    ,options.getString("colorId")
    ,options.getString("longId")
    ,options.getString("sizeId")) ;
    Map map = new HashMap<>();
    map.put("code", "200");
    map.put("msg", "查询成功");
    map.put("data",OriQuantity);
    myCallback.invoke(map);
}

12.uni-app 调用 android 方法

requireNativePlugin:内置原生插件,uni-app已默认集成,支持直接在内置基座运行。

const PDUtils = uni.requireNativePlugin('PDUtils');   // PanDianUtils 为原生插件名称
PanDianUtils.clear({
    "sheetId": this.panDianInfo.id
}, result => {
    if (result.code == 200) {
        this.cont = "撤销录入成功";
        this.$forceUpdate();
        this.$refs.messageBox.open()
    }
})
// dcloud_uniplugins 注册
{
"type": "module",
"name": "PanDianUtils",
"class": "com.PDUtils"
}

*注意

  • 使用前一定要注册
  • SQLite 语句的大小写不敏感,但是使用Cursor(游标)获取数据时很敏感,一定要在创建的表中存在一样的字段
  • execSQL(sql语句””,参数)里面的?代表占位符,有几个占位符就需要几个参数,参数对应的位置要保持一致
  • 每一个方法都需要加@JSMethod(uiThread = true),否则即使参数没有问题,也没法成功请求方法

Original: https://blog.csdn.net/weixin_44543219/article/details/123427819
Author: 一只快乐的蓝胖纸
Title: 浅入Android

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815656/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球