博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【数据存储】使用ContentValues封装数据(3)
阅读量:7241 次
发布时间:2019-06-29

本文共 1355 字,大约阅读时间需要 4 分钟。

使用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操作会更方便。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://ffybm.baihongyu.com/

你可能感兴趣的文章
21天打造分布式爬虫-简书整站爬取(十)
查看>>
es6 - 回调深渊
查看>>
streamsets k8s 部署试用
查看>>
在Windows Mobile和Wince(Windows Embedded CE)下使用.NET Compact Framework进行GPS NMEA data数据分析的开发...
查看>>
网络基础知识:(一)网络分层和网络设备
查看>>
android布局 - fill_parent/match_paren/wrap_content的区别
查看>>
论go语言中goroutine的使用
查看>>
解决td标签上的position:relative属性在各浏览器中的兼容性问题
查看>>
H5图片上传插件
查看>>
iOS5问题汇总
查看>>
[译]Chipmunk教程 - 3 初始化
查看>>
也谈WebKit、Gecko使用图形库
查看>>
6个寓言故事
查看>>
android用sharepreference保存输入框中的内容
查看>>
C# 鼠标穿透窗体功能
查看>>
Windows平台上C++开发内存泄漏检查方法
查看>>
hbase 0.96 java 示例
查看>>
XML与Web Service基础知识点
查看>>
visual studio使用技巧
查看>>
C#几个经常犯错误汇总
查看>>