Android Sqlite 代码实现 多表联合查询

最近开发ServerMonitor解决的一个issue,关于数据库的的升级,需要log表内添加一个is_scuuess字段并且对原数据库中每条日志的该记录赋值,判断成功的逻辑是根据不同协议的返回状态码及请求时间进行判断,因为日志表内只有一个site_id字段,具体日志记录的协议类型字段在site表内,于是决定采用多表联合查询的方法,通过site_id,将log表的日志记录与site表内的port_type一同查询出来 然后通过moveToNext()方法对每一条日志进行处理,具体核心代码是:

      Cursor cursorSite_log = db.rawQuery(" SELECT x.*, y.port_type FROM log x, site y "+
                "WHERE x.site_id = y._id",null);

需要在创建一个Log_siteInfo类包含log表内的所有字段和site表的port_type字段 :

  public Log_siteInfo(Cursor cursorSite_log) {
     this.port_type=cursorSite_log.getString(cursorSite_log.getColumnIndex("port_type"));
     ......
  }

然后在

      try {
             while (cursorSite_log != null && cursorSite_log.moveToNext()){
                   Log_siteInfo log_siteInfo = new Log_siteInfo(cursorSite_log);
                    //具体执行代码
                   }
        }finally {
            if (cursorSite_log != null) {
                cursorSite_log.close();
            }
        }
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的基于 SQLite 的购物车实现示例: 1. 创建商品表和购物车表: ```sql CREATE TABLE IF NOT EXISTS Product ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, price REAL NOT NULL ); CREATE TABLE IF NOT EXISTS Cart ( id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER NOT NULL, quantity INTEGER NOT NULL, FOREIGN KEY (product_id) REFERENCES Product(id) ); ``` 2. 在应用中,首先需要查询商品列表并显示在界面上: ```java List<Product> productList = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM Product", null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); float price = cursor.getFloat(cursor.getColumnIndex("price")); productList.add(new Product(id, name, price)); } cursor.close(); ``` 3. 当用户点击“加入购物车”按钮时,将商品添加到购物车中。如果购物车中已经存在该商品,则更新数量: ```java // 检查购物车中是否已经存在该商品 Cursor cursor = db.rawQuery("SELECT * FROM Cart WHERE product_id=?", new String[]{String.valueOf(productId)}); if (cursor.moveToFirst()) { // 更新数量 int id = cursor.getInt(cursor.getColumnIndex("id")); int quantity = cursor.getInt(cursor.getColumnIndex("quantity")); quantity += 1; ContentValues values = new ContentValues(); values.put("quantity", quantity); db.update("Cart", values, "id=?", new String[]{String.valueOf(id)}); } else { // 添加新的购物车项 ContentValues values = new ContentValues(); values.put("product_id", productId); values.put("quantity", 1); db.insert("Cart", null, values); } cursor.close(); ``` 4. 在购物车页面,查询购物车列表并显示在界面上。可以使用 JOIN 操作将商品信息和购物车信息关联起来: ```java List<CartItem> cartItemList = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT Cart.id, Cart.quantity, Product.name, Product.price " + "FROM Cart JOIN Product ON Cart.product_id=Product.id", null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); int quantity = cursor.getInt(cursor.getColumnIndex("quantity")); String name = cursor.getString(cursor.getColumnIndex("name")); float price = cursor.getFloat(cursor.getColumnIndex("price")); cartItemList.add(new CartItem(id, name, price, quantity)); } cursor.close(); ``` 5. 当用户点击“结算”按钮时,根据购物车中的商品数量计算总价: ```java float total = 0; Cursor cursor = db.rawQuery("SELECT Cart.quantity, Product.price " + "FROM Cart JOIN Product ON Cart.product_id=Product.id", null); while (cursor.moveToNext()) { int quantity = cursor.getInt(cursor.getColumnIndex("quantity")); float price = cursor.getFloat(cursor.getColumnIndex("price")); total += quantity * price; } cursor.close(); ``` 以上是一个简单的购物车实现示例,实际开发中还需要考虑更多因素,如商品库存、购物车数量限制等。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值