android sqlite 操作类封装,[Android] Sqlite 数据库操做 工具封装类

sqlite 数据库封装类html

DatabaseUtil.java(封装的类)java

packagecom.jack.androidbase.tools;importandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.SQLException;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.util.Log;/*** sqlite 数据库操做类*/

public classDatabaseUtil {private static final String TAG = "DatabaseUtil";/*** Database Name*/

private static final String DATABASE_NAME = "student_data";/*** Database Version*/

private static final int DATABASE_VERSION = 1;/*** Table Name*/

private static final String DATABASE_TABLE = "tb_student";/*** Table columns*/

public static final String KEY_NAME = "name";public static final String KEY_GRADE = "grade";public static final String KEY_ROWID = "_id";/*** Database creation sql statement*/

private static final String CREATE_TABLE =

"create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "

+ KEY_NAME + " text not null, " + KEY_GRADE + " text not null);";/*** Context*/

private finalContext mCtx;privateDatabaseHelper mDbHelper;privateSQLiteDatabase mDb;/*** Inner private class. Database Helper class for creating and updating database.*/

private static class DatabaseHelper extendsSQLiteOpenHelper {

DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);

}/*** onCreate method is called for the 1st time when database doesn't exists.*/@Overridepublic voidonCreate(SQLiteDatabase db) {

Log.i(TAG,"Creating DataBase: " +CREATE_TABLE);

db.execSQL(CREATE_TABLE);

}/*** onUpgrade method is called when database version changes.*/@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, intnewVersion) {

Log.w(TAG,"Upgrading database from version " + oldVersion + " to "

+newVersion);

}

}/*** Constructor - takes the context to allow the database to be

* opened/created

*

*@paramctx the Context within which to work*/

publicDatabaseUtil(Context ctx) {this.mCtx =ctx;

}/*** This method is used for creating/opening connection

*

*@returninstance of DatabaseUtil

*@throwsSQLException*/

public DatabaseUtil open() throwsSQLException {

mDbHelper= newDatabaseHelper(mCtx);

mDb=mDbHelper.getWritableDatabase();return this;

}/*** This method is used for closing the connection.*/

public voidclose() {

mDbHelper.close();

}/*** This method is used to create/insert new record record.

*

*@paramname

*@paramgrade

*@returnlong*/

public longinsert(String name, String grade) {

ContentValues initialValues= newContentValues();

initialValues.put(KEY_NAME, name);

initialValues.put(KEY_GRADE, grade);return mDb.insert(DATABASE_TABLE, null, initialValues);

}/*** This method will delete record.

*

*@paramrowId

*@returnboolean*/

public boolean delete(longrowId) {return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

}/*** This method will deleteAll record.

*

*@return

*/

public booleandeleteAll() {return mDb.delete(DATABASE_TABLE, " 1 ", null) > 0;

}/*** This method will return Cursor holding all the records.

*

*@returnCursor*/

publicCursor fetchAll() {return mDb.query(DATABASE_TABLE, newString[]{KEY_ROWID, KEY_NAME,

KEY_GRADE},null, null, null, null, null);

}/*** This method will return Cursor holding the specific record.

*

*@paramid

*@returnCursor

*@throwsSQLException*/

public Cursor fetch(long id) throwsSQLException {

Cursor mCursor=mDb.query(true, DATABASE_TABLE, newString[]{KEY_ROWID,

KEY_NAME, KEY_GRADE}, KEY_ROWID+ "=" + id, null,null, null, null, null);if (mCursor != null) {

mCursor.moveToFirst();

}returnmCursor;

}/*** This method will update record.

*

*@paramid

*@paramname

*@paramstandard

*@returnboolean*/

public boolean update(intid, String name, String standard) {

ContentValues args= newContentValues();

args.put(KEY_NAME, name);

args.put(KEY_GRADE, standard);return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0;

}

}

使用详解:android

DatabaseUtil dbUtil = new DatabaseUtil(this);

dbUtil.open();

switch (v.getId()) {

case R.id.other_btn_sqlite_list: //查询

Cursor cursor = dbUtil.fetchAll();

if (cursor != null) {

while (cursor.moveToNext()) {

Log.i("Student", "ID:" + cursor.getInt(0) + ",Student Name: " + cursor.getString(1) +

",Grade: " + cursor.getString(2));

}

}

break;

case R.id.other_btn_sqlite_add:

dbUtil.insert("Prashant Thakkar", "100");

Log.i("Student", "add over");

break;

case R.id.other_btn_sqlite_update:

dbUtil.update(1, "aa", "bb");

Log.i("Student", "update over");

break;

case R.id.other_btn_sqlite_del:

dbUtil.delete(2);

Log.i("Student", "delete over");

break;

case R.id.other_btn_sqlite_del_all:

dbUtil.deleteAll();

Log.i("Student", "delete all over");

break;

}

dbUtil.close();sql

参考网址:数据库

https://www.cnblogs.com/sowhat4999/p/4439856.htmlide

https://www.cnblogs.com/ProMonkey/p/5765616.html   (有添加单例模式)fetch

本博客地址: wukong1688this

转载请著名出处!谢谢~~spa

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419541.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle之数据处理之约束2

--修改约束 alter table emp5 modify (salary number(10,2) not null)运行结果 --删除约束 alter table emp5 drop constraint emp5_name_nn 运行结果

css实现一个写信的格式

一、目标 目标实现如下效果: 二、完成 1、分析 这个效果看起来很简单,实际上可能并不那么容易实现。 首先是全部东西都居中显示,除了“亲爱的starof”这个称呼的地方。这也是难点,也是本文要重点说的地方。 开始我尝试将“ 亲爱的…

android 6.0版本名字,棉花糖Marshmallow 是Android 6.0的名字

Android 6.0 五月下旬在 I/O 大会上亮相,将于今年秋天与用户见面。但是依照惯例,谷歌并没有宣布新 Android 的代号全称,而 Android M 中 M 所代表的甜品:Marshmallow(棉花糖)。并不是MM巧克力豆,巧克力豆的粉丝&#x…

Java达到MySQL数据库备份(两)

博客《Java实现MySQL数据库备份(一)》使用I/O流的方式实现了MySQL数据库的备份,这样的方法比較繁杂。以下介绍还有一种备份MySQL数据库的方法: import java.io.File; import java.io.IOException;/*** MySQL数据库备份* * author …

找不到android的sdk,CircleCI – 找不到Android Studio项目的SDK位置

尝试在CircleCI上构建项目时,在gradle构建期间发生以下错误.这个问题的原因是什么?我正在运行CircleCI 2.0.FAILURE: Build failed with an exception.What went wrong: A problem occurred configuring project ‘:app’.SDK location not found. Define location …

选项卡,下拉菜单操做时的页面数据更新,highcharts,d3 结合。

1.选项卡:给要选中的元素添加css样式,加active,单击时先移除active,再把当前单击元素添加active。 单击时页面切换,按钮和页面要有关联,通过获取$(this).text();年龄,教育,职业&…

oracle之数据处理之约束练习

57. 定义非空约束1). 非空约束只能定义在列级.2). 不指定约束名create table emp2 (name varchar2(30) not null, age number(3));3). 指定约束名 create table emp3(name varchar2(30) constraint name_not_null not null, age number(3));58. 唯一约束1). 列级定义①. 不指定…

小米9android系统怎么关闭,小米MIUI系统怎么禁用虚拟键 小米MIUI系统禁用虚拟键方法...

想新很多米粉对对miui系统不会陌生。这个系统还很是不错。但是刚刚入手小米手机的米粉可能就不太熟悉了。那么,虚拟键要怎么去禁用呢?下面就一起来看看小米MIUI系统虚拟键禁用方法。大家用安卓手机的时候是否曾遇到过以下折磨人的场景:小米MI…

C#性能优化:延迟初始化LazyT

1. 概述 我们创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次运行都创建该对象,这时候延迟初始化(也叫延迟实例化)就出场了。 延迟初始化出现于.NET 4.0,主要用于提高性能&…

android手机值不值得买,安卓直屏Redmi K40手机值得买吗?

描述还记得我们上一次谈论直屏与曲面屏的关系吗?小Z从后台留言中发现了很多人确实对如今清一色的旗舰曲面屏感到不满,用着远没有几年前的直屏来得顺手。但在如今的手机市场中,想要找到一款屏幕素质不错,硬件配置足够旗舰&#xff…

oracle之数据处理之视图

--创建视图 create view empview as select employee_id,last_name,salary from employees where department_id80;--查询视图 select * from empview--修改视图 update empview set salary20000 where employee_id179 运行结果 --修改视图 create or replace view empview2 a…

simplified build configuration

http://blogs.msdn.com/b/saraford/archive/2005/08/16/452411.aspx Did you know… That you can hide the solution and advanced build configurations Under Tools – Options – Projects and Solutions – General, there are options for both Always show solution and…

oracle之数据处理之视图练习

62. 查询员工表中 salary 前 10 的员工信息.select last_name, salary from (select last_name, salary from employees order by salary desc) where rownum < 10说明: rownum "伪列" ---- 数据表本身并没有这样的列, 是 oracle 数据库为每个数据表 "加上的…

android集合优化,android-性能优化之集合类优化

优化包括&#xff1a;I/O的优化、网络操作的优化、内存的优化、数据结构的优化、代码层次的优化、UI渲染优化、CPU资源使用率的优化、异常处理的优化等》ArrayList和VectorArrayList和Vector都是内部以数组实现的List&#xff0c;它们两唯一的区别就是对多线程的支持&#xff0…

oracle之数据处理之其他数据库对象

--创建序列 create sequence empseq increment by 10---每次增长10 start with 10--从10开始 maxvalue 100--提供最大值 cycle --循环 nocache --不需要缓存登录 运行结果 --查看 查看 select empseq.nextval from dual 运行结果 --创建表 create table emp11 as select empl…

android 线程太多,应用程序可能在其主线程上做了太多的工作。

任何开始开发android应用程序的人都会在logcat上看到这个消息。编舞(ABC)&#xff1a;跳过xx帧&#xff01;应用程序可能在其主线程上做了太多的工作。“那么&#xff0c;它到底意味着什么&#xff0c;你为什么要关心它&#xff0c;以及如何解决它。这意味着您的代码需要很长时…

关于Docker官方CentOS镜像无法启动mysqld的总结

很多童鞋反映&#xff0c;在Docker官方CentOS镜像中安装了Mysql server后&#xff0c;无法正常启动。 无法正常启动表现为两种情况&#xff1a; 1> 初始完数据库后&#xff0c;mysqld启动报错 2> systemctl start mysqld或者service mysqld start报错 首先重现一下现场。…

oracle之数据处理之其他数据库对象练习

1. 创建序列dept_id_seq&#xff0c;开始值为200&#xff0c;每次增长10&#xff0c;最大值为10000 a) create sequence dept_id_seq b) start with 200 c) increment by 10 d) maxvalue 10000 2. 使用序列向表dept中插入数据 a) insert into dept01 b) values(dept_id_seq.nex…

android 刷rom,刷ROM是什么?刷ROM是什么意思?

刷ROM是什么意思首先&#xff0c;ROM是由英文Read only Memory的首字母构成的&#xff0c;意为只读存储器。顾名思义&#xff0c;就是这样的存储器只能读&#xff0c;不能像RAM一样可以随时读和写。它只允许在生产出来之后有一次写的机会&#xff0c;数据一旦写入则不可更改。它…