3 Star 1 Fork 1

Gitee 极速下载/SQLiteUtils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/YeDaxia/SQLiteUtils
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

About SQLiteUtils

一个简单的基于Android的Sqlite数据库的操作封装,它有如下的好处:

  • 便捷地创建表和增添表字段
  • 通过操作对象来insert或者update表记录
  • 支持多种查询方式,支持分页查询
  • 支持事务

Quick Start:

  1. 定义表对应的对象,对于基本类型,请使用对象类型来声明类成员,如int用Integer。以免在update的时候出现意想不到的情况。
@Table(name="t_user")
public class UserModel {
	@Table.Column(name="user_id",type=Column.TYPE_INTEGER,isPrimaryKey=true)
	public Integer userId;
	
	@Table.Column(name="user_name",type=Column.TYPE_STRING,isNull=false)
	public String userName;
	
	@Table.Column(name="born_date",type=Column.TYPE_TIMESTAMP)
	public Date bornDate;
	
	@Table.Column(name="pictrue",type=Column.TYPE_BLOB)
	public byte[] pictrue;
	
	@Table.Column(name="is_login",type=Column.TYPE_BOOLEAN)
	public Boolean isLogin;
	
	@Table.Column(name="weight",type=Column.TYPE_DOUBLE)
	public Double weight;
}
  1. 增删查改: 初始化对象:
SQLiteDatabase db = context.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
DbSqlite dbSqlite = new DbSqlite(db);
IBaseDao<UserModel> userDAO = DaoFactory.createGenericDao(dbSqlite, UserModel.class);

创建Table:

userDAO.createTable();

insert 一条记录:

UserModel user = new UserModel();
user.userName = "darcy";
user.isLogin = true;
user.weight = 60.5;
user.bornDate = new Date();
byte[] picture = {0x1,0x2,0x3,0x4};
user.pictrue = picture;
userDAO.insert(user);

update 记录:

UserModel user = new UserModel();
user.weight = 88.0;
userDAO.update(user, "user_name=?", "darcy");

查询结果:

//单条结果查询
UserModel user = userDAO.queryFirstRecord("user_name=?", "darcy");

//一般查询
List<UserModel> userList = userDAO.query("user_name=? and weight > ?", "darcy" , "60");

//分页查询
PagingList<UserModel> pagingList = userDAO.pagingQuery(null, null, 1, 3);

事务支持:

DBTransaction.transact(mDb, new DBTransaction.DBTransactionInterface() {
		@Override
		public void onTransact() {
			// to do 		
		}
};

更新表[只支持添加字段]:

@Table(name="t_user" , version=2) //修改表版本
public class UserModel {
	//members above...
	
	//new columns
	
	@Table.Column(name="new_column_1",type=Column.TYPE_INTEGER)
	public Integer newColumn;
	
	@Table.Column(name="new_column_2",type=Column.TYPE_INTEGER)
	public Integer newColumn2;
}

userDAO.updateTable();

License

Copyright 2016 yedaxia

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.

空文件

简介

SQLiteUtils 是一个简单的基于Android的Sqlite数据库的操作封装,它有如下的好处: 便捷地创建表和增添表字段 灵活的数据类型处理 通过操作对象来insert或者 展开 收起
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/mirrors/SQLiteUtils.git
git@gitee.com:mirrors/SQLiteUtils.git
mirrors
SQLiteUtils
SQLiteUtils
master

搜索帮助