Android 数据库SQLite使用小结

前言

在Android开发过程中,对于数据的存储,我们或多或少的都会使用到数据库相关的操作,所以在此小小的总结一下,Android中使用SQLite数据库的技巧和方法,算是自己对数据库知识的复习。项目源代码
本博客同步发布于 XueLong的博客

增删改查

adb shell中操作SQLite

adb shell
su  //以管理员的身份运行命令
sqlite DATABASE_NAME.db //进入要操作的数据库
.table  //查看当前数据库有哪些表
.table t% //".tables"命令后也可以跟一参数,它是一个pattern,这样命令就只列出表名和该参数匹配的表。
.schema //显示最初用于创建数据库的CREATE TABLE和CREATE INDEX的SQL语句
.schema t% //".schema"命令可以包含一个参数,它是一个pattern,用于对表进行过滤,这时只会显示满足条件的表和所有索引的SQL语句
pragma table_info(TABLE_NAME);  //查看表的数据结构
.mode line  //切换显示模式,可用参数有 line list column
.separator |   //字段之间使用 | 间隔开
.output filename.txt  //将数据库中的数据输出到文件中
.width 12 6 //调整列宽 12表示第一列宽为12,6表示第二列宽为6
.databases //显示所有当前连接打开的数据库的一个列表,main:最初打开的数据库,temp:临时表的数据库
.exit //退出

小技巧

  • SQLite EXpert Personal 中注释SQL语句,单行注释使用– ,多行注释使用/**/
  • 每个 SQLite 数据库中都有一个隐藏的 sqlite_master 表,它记载了当前数据库中所有的建表语句。
  • 在对数据库进行增删改查前首先需要先调用SQLiteOpenHelpergetReadableDatabase()(查时调用)或getWritableDatabase()(增删改时调用)方法。
常规的SQL语句操作
SQLiteDatabase db = null;
//在执行增删改语句时使用
db = helper.getWritableDatabase();
//在执行查询语句时使用
//db = helper.getReadableDatabase();
db.execSQL("在这里填入你的SQL语句");

常用的SQL语句如下:

//建表语句
create table if not exists TABLE_NAME(Id integer orimary key,Name text,Age integer);
//增加一条数据
insert into TABLE_NAME(Id,Name,Age) values (1,'lixuelong',23);
//删除一条数据
delete from TABLE_NAME where Name = 'lixuelong';
//修改一条数据
update TABLE_NAME set Name = 'xuelong' where Id =1;
//查询语句
select * from TABLE_NAME where Name = 'lixuelong' order by Age desc;
//统计查询语句
select count(Id) from TABLE_NAME where Age = 18;
//比较查询语句
select Id,Name,Max(Age) as Age from TABLE_NAME;
Android自有的事务(Transaction)处理方法

增加数据操作
long insertOrThrow(String table, String nullColumnHack, ContentValues values)
无论第三个参数是否包含数据,执行此方法必定会插入一条数据

  • table:表名
  • nullColumnHack:用于指定空值字段的名称
  • values:要存放的ContentValues对象,可以为null
  • 返回值:返回新添记录的行号,与主键id无关,发生错误返回-1
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
// insert into TABLE_NAME (_id,Name,Age) values (6,'张三',18);
ContentValues contentValues = new ContentValues();
contentValues.put("_id","6");
contentValues.put("Name", "张三");
contentValues.put("Age", 18);
db.insertOrThrow(TABLE_NAME, null, contentValues);
db.setTransactionSuccessful();

删除数据操作
int delete(String table, String whereClause, String[] whereArgs)

  • table:表名
  • whereClause:删除的条件,如果为null,则整行删除
  • whereArgs:字符串数组,和whereClause配合使用,有两种使用方法,1、如果whereClause的条件已经直接给出,如”Name=’张三’”,则whereArgs可以设为null。2、如果whereClause的条件没有直接给出,如”Name=?”,则?会被whereArgs字符串数组中的值代替。
  • 返回值:0:删除是被,1:删除成功
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
db.delete(TABLE_NAME, "Name=?", new String[]{"张三"});
//db.execSQL("delete from TABLE_NAME where Name = '张三';");
db.setTransactionSuccessful();

修改数据操作
update(String table, ContentValues values, String whereClause, String[] whereArgs)

  • table:表名
  • values:要存放的ContentValues对象,可以为null
  • whereClause:修改的条件,如果为null,则整行修改
  • whereArgs:字符串数组,和whereClause配合使用,有两种使用方法,1、如果whereClause的条件已经直接给出,如”Name=’张三’”,则whereArgs可以设为null。2、如果whereClause的条件没有直接给出,如”Name=?”,则?会被whereArgs字符串数组中的值代替。
  • 返回值:the number of rows affected
SQLiteDatabase db = null;
db = helper.getWritableDatabase();
db.beginTransaction();
// update TABLE_NAME set Name = 'zhangsan' where Name = '张三'
ContentValues contentValues = new ContentValues();
contentValues.put("Name", "zhangsan");
db.update(TABLE_NAME, contentValues, "Name = ?", new String[]{"张三"});
db.setTransactionSuccessful();

查询数据操作
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
query方法较多,这里只列举一个,有兴趣的朋友可以自行研究

  • table:表名
  • columns:需要返回的列的列表,如果为null,则返回全部的列
  • selection:查询的条件,符合什么条件的行将返回。如果为null,则这个表里的所有行都将返回。其两种用法和update里的一样
  • selectionArgs:用法和update里的一样
  • groupBy:用于控制分组
  • having:用于对分组进行过滤
  • orderBy:用于对记录进行排序
  • 返回值:Cursor对象
SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select * from TABLE_NAME where Age = 18
cursor = db.query(TABLE_NAME, TABLE_COLUMNS, "Age = ?", new String[]{String.valueOf(18)},null, null, null);
if (cursor.getCount() > 0) {
   List<DBSQLBean> beanList = new ArrayList<DBSQLBean>();
   while (cursor.moveToNext()) {
        DBSQLBean bean = parseBean(cursor);
        beanList.add(bean);
    }
    return beanList;
}

统计查询数据操作

int count = 0;
SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select count(_id) from TABLE_NAME where Age = 18
cursor = db.query(DBSQLHelper.TABLE_NAME, new String[]{"COUNT(_id)"},"Age = ?", new String[]{String.valueOf(18)},null, null, null);
if (cursor.moveToFirst()) {
    count = cursor.getInt(0);
}

比较查询数据操作

SQLiteDatabase db = null;
Cursor cursor = null;
db = helper.getWritableDatabase();
// select _id,Name,Max(Age) as Age from TABLE_NAME
cursor = db.query(DBSQLHelper.TABLE_NAME, new String[]{"_id", "Name","Max(Age) as Age"},null, null, null, null, null);
if (cursor.getCount() > 0) {
    if (cursor.moveToFirst()) {
        return parseBean(cursor);
    }
}

写在最后

以上就是对Android中数据库操作的小小总结。

如果你在参考过程中遇到问题,可以在我的联系方式中给我提问。

后面会继续介绍,Android的相关知识,欢迎继续关注我博客的更新。

项目源代码

参考资源

转载请注明:XueLong的博客 » Android 数据库SQLite使用小结

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中使用sqlite3工具可以进行SQLite数据库的创建和操作。有两种方式可以使用sqlite3工具:使用Android Studio自带的adb工具或使用Windows命令行操作模式。如果选择使用adb工具,可以在Android Studio的SDK目录下的platform-tools目录找到adb.exe程序。然而,有些人反映这个程序会闪退,所以不推荐使用这种方式。更推荐的方式是使用DOS命令行。 首先,打开SDK目录,使用cd命令进入Android Studio的platform-tools目录。然后,使用adb shell命令连接到模拟器的Linux系统。需要注意的是,只能同时开启一个模拟器,否则会报错。 接下来,可以开始创建数据库。首先,使用mkdir命令创建数据库文件夹,例如mkdir /data/data/com.takemehand/database。然后,可以在Linux系统中启动sqlite3工具,并在Android程序目录中创建数据库和数据表,使用命令对数据表中的内容进行添加、删除和修改。 总结来说,使用Android Studio的sqlite3工具可以方便地进行SQLite数据库的创建和操作,可以选择使用adb工具或使用Windows命令行操作模式。详细的步骤可以参考引用、和中提供的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android Studio模拟器使用sqlite3建立SQLite数据库](https://blog.csdn.net/qq_45488453/article/details/121358929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Android Studio开发使用SQLite3数据库](https://blog.csdn.net/qq_45325916/article/details/101070402)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值