Giter Site home page Giter Site logo

smartstore's Introduction

SmartStore

SmartStore是一个android的数据库帮助库,几乎不用编数据库操作代码就可以实现数据库到java对象的映射,使用SmartStore的方法很简单,比如现在需要创建一个User的表并使用User对象与之关联,只需简单三步:

  1. 编写User对象,并在User对象中标记出 数据库字段 和 主键字段(注意:变量的名称和数据库中的字段名称一样)
public class User extends SmartModel {

	    @DBPrimaryKey
	    @DBField
	    public int user_id;
	
	    @DBField
	    public String user_name;
	
	    @DBField
	    public int user_age;
	
	    //not db field
	    public int user_sex;
}
  1. 在应用初始化的时候,设置数据库的代理
WPStore.setWPStoreDelegate(new WPStoreDelegate() {
			@Override
			public void onCreateTables(SQLiteDatabase db) {
				//这里可以做一些创建数据库和升级的操作
				db.execSQL(new User().getCreateTableSql());
			}
			
			@Override
			public String getDBName() {
				//这里可以判断登录用户,不用的用户返回不同的数据库名字
				return "test" + currentLoginUid + ".db";
			}
			
			@Override
			public Context getContext() {
				return getApplicationContext();
			}

			@Override
			public void onDBDestroy(String oldDBPath) {
				//数据库切换时旧数据库被销毁的通知
				Log.d("MainActivity", "onDBDestroy");
			}			
		});
  1. 在需要的地方使用数据库
	User user = new User();
	user.user_id = 1001;
	user.user_name = "Summer";
	user.user_age = 10;
	user.user_sex = 1;
	WPStore.getInstance().saveSync(user);
	
	User user1 = new User();
	user1.user_id = 1002;
	user1.user_name = "Hehe";
	user1.user_age = 11;
	user.user_sex = 2;
	WPStore.getInstance().saveSync(user1);
	
	StringBuilder ret = new StringBuilder();
	String sql = "select * from " + user.getTableName();
	List modelList = WPStore.getInstance().fetchListSync(user, sql);
	for(SmartModel model : modelList){
	    User u = (User) model;
	    ret.append("id -->"+u.user_id+"  name-->"+u.user_name+"  age-->"+u.user_age+"  sex-->"+u.user_sex+"\n");
	}
	Log.d("test", ret.toString());

#数据库操作Api 数据库操作Api都封装到WPStore类中,包括

  • removeSync(SmartModel) //同步删除
  • removeAsync(SmartModel, Callback) //异步删除
  • saveSync(SmartModel) //同步存储
  • saveAsync(SmartModel) //异步存储
  • saveListByTransaction(List) //同步存储列表
  • saveListAsyncByTransaction(List, Callback) //异步存储列表
  • fetchListSync(SmartModel, sql) //同步获取列表
  • fetchListAsync(SmartModel, sql, Callback) //异步获取列表
  • execSql(sql) //同步执行sql
  • execSqlAsync(sql) //异步执行sql

#关于 smart store 的原理 和 注意事项

  • smart store 通过类中标记的数据库字段和主键字段,能够自动生成数据库的创建语句(以类名为表明,字段名为数据库字段名称),以及数据库的查询 和 插入功能
  • smart store 通过主键进行存储和删除,所有如果需要调用save 方法 或者remove方法, 则需要加入主键标记, 否则会报错
  • smart store 写的比较简单, 只支持 int long String 三种类型的字段, 但作者认为这三种字段对于sqlite来说也够用了, 所以需要注意java类中的字段类型

smartstore's People

Contributors

mrsummer avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.