zoukankan      html  css  js  c++  java
  • 【数据存储】使用ContentValues封装数据(3)

    使用ContentValues修改MytabOperate类中的方法

    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    
    public class MytabOperate {
        // 表示要操作的数据表名称
        private static final String TABLENAME = "mytab"; 
        // 数据库操作
        private SQLiteDatabase db = null; 
        // 构造方法
        public MytabOperate(SQLiteDatabase db) {
            this.db = db;
        }
        public void insert(String name,String birthday) {
            ContentValues cv = new ContentValues() ;
            cv.put("name", name) ;
            cv.put("birthday", birthday) ;
            this.db.insert(TABLENAME, null, cv) ;
            this.db.close() ;
        }
        public void update(int id, String name, String birthday) {
            ContentValues cv = new ContentValues() ;
            cv.put("name", name) ;
            cv.put("birthday", birthday) ;
            // 更新条件
            String whereClause = "id=?" ;
            // 更新ID
            String whereArgs[] = new String[]{String.valueOf(id)} ;
            this.db.update(TABLENAME, cv, whereClause, whereArgs) ;
            this.db.close() ;
        }
        public void delete(int id) {
            String whereClause = "id=?" ;
            String whereArgs[] = new String[]{String.valueOf(id)} ;
            this.db.delete(TABLENAME, whereClause, whereArgs) ;
            this.db.close() ;
        }
    }

    虽然通过SQLiteDatabase对象可以执行SQL语句的操作,但从实际开发来讲,使用ContentValues更为标准,这样最大好处是和ContentProvider关联,但是如果现在用户并不需要将数据库操作发布成ContentProvider,则还是使用SQL操作会更方便。

  • 相关阅读:
    CentOS + java
    在 Centos7 用Jexus服务器 运行.Net Core 只需几部
    dotnet core 开发中遇到的问题
    Scratch3.0设计的插件系统(上篇)
    ASP.NET的编译原理
    搭建git服务器
    ubuntu安装Pillow
    MIT线性代数课程总结与理解-第三部分
    关于在ubuntu系统下显卡为goforce1060安装tensorflow(gpu)
    关于Clion中添加makefile相关参数
  • 原文地址:https://www.cnblogs.com/androidsj/p/3129445.html
Copyright © 2011-2022 走看看