Android徒手撸数据库系列——实现单表的增删改查

2024-05-29 19:58

本文主要是介绍Android徒手撸数据库系列——实现单表的增删改查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是手撸数据库框架的第二篇

之前完成了一篇文章

Android徒手撸数据库系列——注解与反射数据库关系模型

下面继续上一篇没有完成的内容

目录

文章目录

  • 目录
    • 1. 数据的更新
    • 2. 数据的删除
    • 3. 数据的查询
    • 详细代码

1. 数据的更新

数据的更新其实就是比数据的插入多了条件的查询

我们使用SQLiteDatabase中这个方法进行更新数据库

   /*** Convenience method for updating rows in the database.** @param table the table to update in* @param values a map from column names to new column values. null is a*            valid value that will be translated to NULL.* @param whereClause the optional WHERE clause to apply when updating.*            Passing null will update all rows.* @param whereArgs You may include ?s in the where clause, which*            will be replaced by the values from whereArgs. The values*            will be bound as Strings.* @return the number of rows affected*/public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {return updateWithOnConflict(table, values, whereClause, whereArgs, CONFLICT_NONE);}

我们可以看到多了参数whereClause和whereArgs

获取需要更新的字段与插入时相同

   Map<String, String> values = getValues(entity);ContentValues contentValues = getContentValues(values);

然后创建表示条件的对象

public class Condition {public String whereCause;//"name=? && password=?...."public String[] whereArgs;//new String[]{"ddssingsong"}// 构造查询语句public Condition(Map<String, String> whereCause) {ArrayList list = new ArrayList();StringBuilder stringBuilder = new StringBuilder();// 构造时忽略第一个and语句stringBuilder.append("1=1 ");Set keys = whereCause.keySet();Iterator iterator = keys.iterator();while (iterator.hasNext()) {String key = (String) iterator.next();String value = whereCause.get(key);if (value != null) {stringBuilder.append(" and " + key + "=?");list.add(value);}}this.whereCause = stringBuilder.toString();this.whereArgs = (String[]) list.toArray(new String[list.size()]);}
}

整个方法如下

 @Overridepublic int update(T entity, T where) {Map<String, String> values = getValues(entity);ContentValues contentValues = getContentValues(values);Map<String, String> whereCause = getValues(where);Condition condition = new Condition(whereCause);return mSqLiteDatabase.update(mTableName, contentValues, condition.whereCause, condition.whereArgs);}

2. 数据的删除

数据库删除和更新差不多

  @Overridepublic int delete(T where) {Map<String, String> map = getValues(where);Condition condition = new Condition(map);return mSqLiteDatabase.delete(mTableName, condition.whereCause, condition.whereArgs);}

3. 数据的查询

查询的话拿到Cursor后需要对返回结果进行处理

  private List<T> getResult(Cursor cursor, T where) {ArrayList<T> list = new ArrayList();T item;while (cursor.moveToNext()) {try {item = (T) where.getClass().newInstance();for (Map.Entry<String, Field> stringFieldEntry : cacheMap.entrySet()) {// 获取列名String columnName = (String) ((Map.Entry) stringFieldEntry).getKey();// 根据列名拿到游标的位置int columnIndex = cursor.getColumnIndex(columnName);Field field = (Field) ((Map.Entry) stringFieldEntry).getValue();Class type = field.getType();if (columnIndex != -1) {if (type == String.class) {//反射方式赋值field.set(item, cursor.getString(columnIndex));} else if (type == Double.class) {field.set(item, cursor.getDouble(columnIndex));} else if (type == Integer.class) {field.set(item, cursor.getInt(columnIndex));} else if (type == Long.class) {field.set(item, cursor.getLong(columnIndex));} else if (type == byte[].class) {field.set(item, cursor.getBlob(columnIndex));/*不支持的类型*/} else {continue;}}}list.add(item);} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}return list;}

从之前缓存的字段信息中查询出对应的字段信息,然后使用反射对其赋值

下面是整个查询方法

   @Overridepublic List<T> query(T where, String orderBy, Integer startIndex, Integer limit) {Map map = getValues(where);String limitString = null;if (startIndex != null && limit != null) {limitString = startIndex + " , " + limit;}Condition condition = new Condition(map);Cursor cursor = mSqLiteDatabase.query(mTableName, null, condition.whereCause, condition.whereArgs, null, null, orderBy, limitString);List<T> result = getResult(cursor, where);cursor.close();return result;}

详细代码

https://github.com/ddssingsong/AnyTool

这篇关于Android徒手撸数据库系列——实现单表的增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014501

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形