ORMLite 是一款非要流行的Android平台上的数据库框架,性能优秀,代码简洁;
简述: 优点: 1.轻量级;2.使用简单,易上手;3.封装完善;4.文档全面。
缺点:1.基于反射,效率较低(本人还没有觉得效率低);2.缺少中文翻译文档
准备工作:
- jar包 地址:http://ormlite.com/releases/
- 集成方法:把jar包复制到as的libs文件夹下,并且引用jar包即可
之后创建一个类User,并完成相关配置
@DatabaseTable(tableName="tb_user")//标明数据库中的一张表,表名tb_user public class User { @DatabaseField(generatedId = true)//generatedId 表示id为主键且自动生成 private int id; @DatabaseField(columnName = "name") private String name; @DatabaseField(columnName = "desc") private String desc; public User() { } public User(String name, String desc) { this.name = name; this.desc = desc; } 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 String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; }}
基本的数据库操作
public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String TABLE_NAME = "sqlite-test.db"; /** * userDao ,每张表对于一个 */ private Dao<User, Integer> userDao; private DatabaseHelper(Context context) { super(context, TABLE_NAME, null, 2); } //创建表 @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource,User.class); } catch (SQLException e) { e.printStackTrace(); } } //更新表 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { try { TableUtils.dropTable(connectionSource,User.class,true);//删除操作 onCreate(sqLiteDatabase, connectionSource);//创建 } catch (SQLException e) { e.printStackTrace(); } } private static DatabaseHelper instance; public static synchronized DatabaseHelper getHelper(Context context) { if (instance == null) { synchronized (DatabaseHelper.class) { if (instance == null) instance = new DatabaseHelper(context); } } return instance; } /** * 获得userDao * * @return * @throws SQLException */ public Dao<User, Integer> getUserDao() throws SQLException { if (userDao == null) { userDao = getDao(User.class); } return userDao; } //释放资源 @Override public void close() { super.close(); } }
MainActivity.Java
得到操作对象
DatabaseHelper helper = DatabaseHelper.getHelper(this);
1:添加
User user= new User("zhangqie"+ni++, "2B青年"); try { helper.getUserDao().create(user);//返回>0则成功 } catch (SQLException e) { }
2:删除
try { return helper.getUserDao().deleteById(id); } catch (SQLException e) { } return 0;
3:修改
User user= new User("zhangqie----android", "2B青年"); user.setId(1);//修改Id=1的 try { return helper.getUserDao().update(user); } catch (SQLException e) { } return 0;
4:查询
try { List<User> users=helper.getUserDao().queryForAll(); } catch (SQLException e) { }
以上的实现方式是基本的使用方式;只有一个类User操作的,万一有多个呢,就不好操作了;
接下来的这种方式:通过一个DatabaseHelper类就可以完成所有类的数据库操作
整个DatabaseHelper使用单例只对外公布出一个对象,参考文章:http://www.touchlab.co/2011/10/single-sqlite-connectio
public class DatabaseHelpers extends OrmLiteSqliteOpenHelper { private static final String TABLE_NAME = "sqlite-test.db"; private Map<String,Dao> daos=new HashMap<String, Dao>(); public DatabaseHelpers(Context context){ super(context,TABLE_NAME,null,4); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, User.class); //多个类在此添加即可 //TableUtils.createTable(connectionSource, Article.class); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { try { TableUtils.dropTable(connectionSource, User.class, true); //TableUtils.dropTable(connectionSource,Article.class,true);//多个类在此添加即可 onCreate(sqLiteDatabase, connectionSource); } catch (SQLException e) { e.printStackTrace(); } } //整个DatabaseHelper使用单例只对外公布出一个对象,保证app中只存在一个SQLite Connection private static DatabaseHelpers instance; /** * 单例获取该Helper * * @param context * @return */ public static synchronized DatabaseHelpers getHelper(Context context) { context = context.getApplicationContext(); if (instance == null) { synchronized (DatabaseHelper.class) { if (instance == null) instance = new DatabaseHelpers(context); } } return instance; } public synchronized Dao getDao(Class clazz) throws SQLException { Dao dao = null; String className = clazz.getSimpleName(); if (daos.containsKey(className)) { dao = daos.get(className); } if (dao == null) { dao = super.getDao(clazz); daos.put(className, dao); } return dao; } /** * 释放资源 */ @Override public void close() { super.close(); for (String key : daos.keySet()) { Dao dao = daos.get(key); dao = null; } } }
我已User为例介绍即可
用一个UserDao来完成相关操作,多个实体类建立多个 XXDao操作了即可
public class UserDao { private Context context; //通过此集合和DatabaseHelper的Map集合相对应 Dao中的类 如User 就可以随意变换了 private Dao<User, Integer> userDaoOpe; private DatabaseHelpers helper; public UserDao(Context context) { this.context = context; try { helper = DatabaseHelpers.getHelper(context); userDaoOpe = helper.getDao(User.class); } catch (SQLException e) { e.printStackTrace(); } } /** * 增加一个用户 * @param user */ public int add(User user) { try { return userDaoOpe.create(user); } catch (SQLException e) { e.printStackTrace(); } return 0; } /** * 增加一个用户 * @param id */ public int delete(int id) { try { return userDaoOpe.deleteById(id); } catch (SQLException e) { e.printStackTrace(); } return 0; } /** * 修改 * @param user */ public int update(User user) { try { return userDaoOpe.update(user); } catch (SQLException e) { e.printStackTrace(); } return 0; } /** * 查询 */ public List<User> query() { try { return userDaoOpe.queryForAll(); } catch (SQLException e) { e.printStackTrace(); } return null; } public User get(int id) { try { return userDaoOpe.queryForId(id); } catch (SQLException e) { e.printStackTrace(); } return null; } }
得到操作对象UserDao
UserDao userDaos=new UserDao(this);
1:添加
User u1 = new User("zq"+(ni+=5), "2B青年"); userDaos.add(u1);
2:删除
int is= userDaos.delete(2);//成功 1 失败 0
3:修改
User u2 = new User("张三丰", "老道"); u2.setId(1); userDaos.update(u2);
4:查询
List<User> users=userDaos.query(); String username=""; for (int i=0;i<users.size();i++){ username+=+users.get(i).getId()+"-----"+users.get(i).getName()+"\n"; } textView.setText(username);
两种效果差不多,实现方式不同而已,运行效果如下:
由于代码太多,就不一一贴出来了,直接下载源码即可 源码点击下载
不足之处请留言指正!有问题的可以给我留言!谢谢!
时间: 2024-10-07 19:06:59