日常水文章之SQLite資料庫的增刪改查
阿新 • • 發佈:2021-01-06
技術標籤:安卓
上一節我建立了一個叫ShopStore.db的db資料庫,這一節我們就來看看怎麼使用這個資料庫
1.增加資料:
/** * 增加資料 * 引數介紹 * @param pid 唯一id * @param trayNo 托盤編號 * @param id 商品id * @param name 商品名稱 * @param image 商品圖片地址 * @param weight 重量(參考或者對比使用) * @param salePrice 零售價 * @param stock 庫存 */ fun setCommodity( trayNo: String, id: String, name: String, image: String, weight: String, salePrice: String, stock: String ) { db!!.beginTransaction()//以獨佔模式開始事務 val contentValues = ContentValues() contentValues.put("trayNo", trayNo) contentValues.put("id", id) contentValues.put("name", name) contentValues.put("image", image) contentValues.put("weight", weight) contentValues.put("salePrice", salePrice) contentValues.put("stock", stock) db!!.insert("Shop", null, contentValues) db!!.setTransactionSuccessful()//將當前事務標記為成功 db!!.endTransaction()//結束交易 }
此處的db是上一節的SQLiteDatabase,呼叫方式:
SQLiteDAO.instance!!.setCommodity( "trayNo", "id", "name", "image", "weight", "salePrice", "stock" )
2.刪除
(1).
/* * 清空資料庫表Shop的所有資料 * */ fun deleteTable(tableName: String) { val sql = "delete from " + tableName db!!.execSQL(sql) }
(2).
/* *刪除某個商品 因為pid是唯一主鍵 這個剛好可以判斷 java程式碼 */ public int getDeleteShop(String pid) { // DELETE FROM 表名稱 WHERE 列名稱 = 值 return db!!.delete("Shop", "pid=?", new String[]{String.valueOf(pid)}); }
3.改:
(1)
/** * 修改商品資料 所有資料 * @param pid 唯一id * @param trayNo 托盤編號 * @param id 商品id * @param name 商品名稱 * @param image 商品圖片地址 * @param weight 重量(參考或者對比使用) * @param salePrice 零售價 * @param stock 庫存 * */ fun updateUsers( trayNo: String?, id: String?, name: String?, image: String?, weight: String?, salePrice: String?, stock: String?, pid: String ) { Log.e("TAG", "修改狀態的id:$pid") val contentValues = ContentValues() contentValues.put("trayNo", trayNo) contentValues.put("id", id) contentValues.put("name", name) contentValues.put("image", image) contentValues.put("weight", weight) contentValues.put("salePrice", salePrice) contentValues.put("stock", stock) var ret: Int = -1 do { ret = db!!.update("Shop", contentValues, "pid=?", arrayOf(pid)) } while (ret < 0) }
(2):
/** * 修改商品資料 只修改庫存 * @param pid 唯一id * @param stock 庫存 * */ fun updateUsers( stock: String?, pid: String ) { Log.e("TAG", "修改狀態的id:$pid"+"\n庫存修改====="+stock) val contentValues = ContentValues() contentValues.put("stock", stock) var ret: Int = -1 do { ret = db!!.update("Shop", contentValues, "pid=?", arrayOf(pid)) } while (ret < 0) }
修改可以全部修改,也可以修改單個屬性,這裡主要根據pid去對應修改內容
4.查:
/** * 查詢 * */ fun getProduct(): List<ProductEntity> { val list: MutableList<ProductEntity> = ArrayList<ProductEntity>() val sql = "select * from Shop" try { val cursor: Cursor = db!!.rawQuery(sql, null) if (cursor != null) { while (cursor.moveToNext()) { val content = ProductEntity() content.pid = cursor.getInt(cursor.getColumnIndex("pid")) content.id = cursor.getString(cursor.getColumnIndex("id")) content.name = cursor.getString(cursor.getColumnIndex("name")) content.image = cursor.getString(cursor.getColumnIndex("image")) content.salePrice = cursor.getString(cursor.getColumnIndex("salePrice")) content.trayNo = cursor.getString(cursor.getColumnIndex("trayNo")) content.weight = cursor.getString(cursor.getColumnIndex("weight")) content.stock = cursor.getString(cursor.getColumnIndex("stock")) list.add(content) } cursor.close() } } catch (e: Exception) { Log.e("TAG", "內容獲取失敗=============") } Log.v(TAG, "獲取資料==select * from Shop===" + list.size) return list }
這裡的ProductEntity是實體類:
class ProductEntity { var pid = 0//唯一ID var trayNo: String? = null //托盤 var id: String? = null //物品id var name: String? = null //物品名字 var image: String? = null //物品圖片地址 var weight: String? = null //物品重量 單位g var salePrice: String? = null //物品單價 單位元 var stock: String? = null //物品庫存 }
這裡查詢主要看 val sql = "select * from Shop"這句命令,這裡可以使用where去查詢,也可以全查!