android 数据库框架,sqlite database

Overview

DBExecutor

主要的功能

1.使用了读写锁,支持多线程操作数据。

2.支持事务

3.支持ORM

4.缓存Sql,缓存表结构

这个类库主要用于android 数据库操作。 始终围绕着一个类对应一个表的概念。 只要创建一个实体类,就不用当心它怎么存储在数据库中,不用重新写增删改查的代码。基本的功能已经帮你实现好了。 增删改查数据只要一句搞定

boolean isSuccess = db.insert(person);
boolean isSuccess = db.updateById(person);
boolean isSuccess  = db.deleteById(Person.class,1);
List<Person> persons = db.findAll(Person.class);
  • ---library_DB02 是项目的源码。
  • ---DBLibrary_TestCase 是项目的测试用例,主要介绍该类库详细的用法
  • ---doc 是网页文档
  • ---db.jar 是该类库的jar包
  • ---doc.chm 是该类库的chm文档
  • ---设计框架.docx 是该类库的uml图生成的图片
  • ---设计框架.mdl 是该类库的uml图,描述整个类库的架构

如果有什么建议可以发我邮箱 告知。[email protected]

最后非常感谢XUtils代码的作者wyouflf。 该代码是对开源项目XUtils的数据库模块进行重构,及性能优化。 因为他的框架设计的非常合理。很多都是在其基础上完善的。

1.定义一个简单对应表的实体类

@Table(name = "Person")
public class Person {
 @Id(autoIncrement = true)
 private int id;
 private String name;
 private int age;
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 public void setAge(int age) {
  this.age = age;
 }
 
}

必须包含@Id,@Id声明的字段对应表里的id。autoIncrement = true 自增长。 @Table 声明表的名字。@Table可以不需要。如果没有@Table以包名+类名为表的名字

支持基本类型以及基本类型的封装类,java.sql.Date 定义的字段。如果要支持其他类型的字段请查看DBLibrary_TestCase中的TestColumnConverter.java

2.主要类

1.DBExecutor主要用于执行sql语句,一个数据库对应一个DBExecutor

获取默认数据库的执行者 DBExecutor executor = DBExecutor.getInstance(context); 获取指定的数据库的执行者 DBExecutor.getInstance(dbHelper)

主要的方法用 db.execute(sqls),db.executeQuery(sql)等 封装了简基本的增删改查操作,使用db.insert(person);可以保存一条记录。 执行sql的时候。不用考虑表是否创建。如果表不存在,会自动创建。

2.Sql用于DBExecutor 执行的Sql

与sql文本语句是有差别的。 区别在于Sql 里包含
sql.getTable();//操作的表
sql.getSqlText();//可以带有?占位符的sql语句
sql.getBindValues();//?占位符对应的值。

sql.setCheckTableExit(checkTableExit);//设置执行sql时检查表是否存在,默认为true如果 检查到表不存在自动创建,设置为false 不做检查 不要害怕复杂,我们可以通过SqlFactory 创建它Sql

3.SqlFactory主要用于创建Sql语句,可以创建复杂增删改查的sql语句。

  // 查询语句
  Sql sql = SqlFactory.find(Person.class);
  sql = SqlFactory.find(Person.class).where("name", "=", "试着飞");
  sql = SqlFactory.find(Person.class).where("age", "=", "10").or("age", "=", 11);
  sql = SqlFactory.find(Person.class).where("age", "=", "10").orderBy("name", false);
  sql = SqlFactory.find(Person.class).where("age", "=", "10").orderBy("name", false).limit(1);
  sql = SqlFactory.find(Person.class).where("name", "like", "%飞");
  sql = SqlFactory.find(Person.class).where("name", "like", "_着_");
  sql = SqlFactory.find(Person.class).where("age", "in", new int[] { 10, 11, 12 });
  sql = SqlFactory.find(Person.class).where("name", "in", new String[] { "小明", "小红" });
  sql = SqlFactory.find(Person.class, "name", "age").where("name", "=", "试着飞");
  sql = SqlFactory.find(Person.class, "count(*) as num").where("age", "=", "11");
  sql = SqlFactory.find(Person.class, "max(age) as maxAge", "min(age) as minAge").where("sex", "=", "男");
  sql = SqlFactory.find(Person.class, new MaxFunction("age", "maxAge"), new MinFunction("age", "minAge")).where("sex", "=", "男");
  sql = SqlFactory.find(Person.class).where("name=? and age=?", new Object[] { "小明", 11 });
 
  // 删除语句
  sql = SqlFactory.deleteAll(Person.class);
  sql = SqlFactory.delete(Person.class).where("age", "=", 11);
  sql = SqlFactory.deleteById(Person.class, 11);
  sql = SqlFactory.deleteById(Person.class, new int[] { 11, 12 });
 
  // 更新语句
  sql = SqlFactory.update(Person.class, new String[] { "name", "age" }, new Object[] { "小明", "11" }).where("id", "=", 1);
  sql = SqlFactory.updateById(new Person(1, "小明", 11, "男"));
  sql = SqlFactory.updateById(new Person(1, "小明", 11, "男"), "name");
 
  // 如果存在 id为1的记录,就更新,否则 插入一条新记录
  sql = SqlFactory.updateOrInsertById(new Person(1, "小明", 11, "男"));
 
  // 插入语句
  sql = SqlFactory.insert(new Person(1, "小明", 11, "男"));
 
  // 自拼接sql语句
  sql = SqlFactory.makeSql(Person.class, "select * from Person where age = ?", new Object[] { 11 });

混淆配置
-keepclasseswithmembers class * { @com.shizhefei.db.annotations.Id <fields>; *;}
更多详细信息请查看项目文档里面的内容

联系方式和问题建议

License

Copyright 2014 shizhefei(LuckyJayce)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
Android SQLite API based on SQLCipher

Download Source and Binaries The latest AAR binary package information can be here, the source can be found here. Compatibility SQLCipher for Android

Extended SQLite functionality for Android

sqlite-provider A simplification of database access for Android. Description sqlite-provider implements a ContentProvider for you that allows database

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.

Thanks to JetBrains for support Kripton Persistence Library project! Kripton Persistence Library Kripton is a java library, for Android platform, that

AndroidQuery is an Android ORM for SQLite and ContentProvider which focuses on easy of use and performances thanks to annotation processing and code generation

WARNING: now that Room is out, I no longer maintain that library. If you need a library to easy access to default android ContentProvider, I would may

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

Extended SQLite functionality for Android

sqlite-provider A simplification of database access for Android. Description sqlite-provider implements a ContentProvider for you that allows database

Exercicio praticando o SQLite, Room e Flow

Bus Scheduler App This folder contains the source code for the Bus Scheduler app codelab. Introduction The Bus Scheduler app displays a list of bus st

Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

Owner
null
SquiDB is a SQLite database library for Android and iOS

Most ongoing development is currently taking place on the dev_4.0 branch. Click here to see the latest changes and try out the 4.0 beta. Introducing S

Yahoo 1.3k Dec 26, 2022
Compile time processed, annotation driven, no reflection SQLite database layer for Android

SqliteMagic Simple yet powerful SQLite database layer for Android that makes database handling feel like magic. Overview: Simple, intuitive & typesafe

Siim Kinks 118 Dec 22, 2022
LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.

#LiteOrm:Android高性能数据库框架 A fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line

马天宇 1.5k Nov 19, 2022
An Android library that makes developers use SQLite database extremely easy.

LitePal for Android 中文文档 LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most o

Lin Guo 7.9k Jan 4, 2023
Realm is a mobile database: a replacement for SQLite & ORMs

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Java version of Realm

Realm 11.4k Jan 2, 2023
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
Active record style SQLite persistence for Android

ActiveAndroid ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to s

Michael Pardo 4.7k Dec 29, 2022
greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Check out ObjectBox Check out our new mobile database ObjectBox (GitHub repo). ObjectBox is a superfast object-oriented database with strong relation

Markus Junginger 12.6k Jan 3, 2023
Active record style SQLite persistence for Android

ActiveAndroid ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to s

Michael Pardo 4.7k Dec 29, 2022