网友通过本文主要向大家介绍了ormlite android,ormlite,ormlite框架,ormlite使用,servicestack.ormlite等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
android OrmLite,androidormlite
最近在使用ormlite框架进行数据库的操作,下面简单的写个demo来学习下
1.下载jar包
这里使用的是ormlite-core-5.0.jar 和 ormlite-android-5.0.jar
将下载的jar包放到我们项目的libs文件夹下
2.创建实体类对象
每一个实体类对应一张表,在我们项目中的bean目录下创建一个Student类
package com.item.jiejie.lite.db; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; /** * 定义实体类Bean,代表一张表 * 需要持久化到数据库的类,类名前需要添加@DatabaseTable,生成对应的表 类中的成员需要添加@DatabaseField,生成前面的表中的字段 * 注意:在需要持久化的类中必须要有无参的构造器 * Created by jiejie on 2017/5/3. */ @DatabaseTable(tableName = "tb_hello") public class Hello { @DatabaseField(generatedId = true) private int id; @DatabaseField(columnName = "name") private String name; @DatabaseField(columnName = "sex") private String sex; @DatabaseField(columnName = "age") private int age; /* * 常用参数 * generatedId = true 主键,自动生成的id 该注解下的字段必须是整形(int long) * id = true 主键 * unique = true 唯一约束 默认false * columnName = "name" 表字段名,默认为变量名称 * canBeNull = false 非空约束,默认为true,可以为null,设为false就不能为null * foreign = true 外键引用,字段不能是一个原始类型,应该定义一个对象当做外键引用,在外键对象的类中,必须要有一 * 个ID字段(ID, generatedId,generatedIdSequence) * foreignAutoRefersh = true 在使用外键引用时,由于ormlite的外键引用使用的是对象,所以添加这个字段的话在查询,会把 * 外键的对象数据都查询回来,否则外键数据就只有那个对象的主键有值,其余的值都是null * defaultValue = "小明" 默认值 * index = true 建立索引 默认为false * uniqueIndex = true 唯一索引 默认为false */ //空的构造方法一定要有,否则数据库会创建失败 public Hello(){ } public Hello(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Hello{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
3.编写我们的数据helper类
package com.item.jiejie.lite.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; import java.sql.SQLException; /** * 编写DAO类 * Created by jiejie on 2017/5/3. */ public class DataHelper extends OrmLiteSqliteOpenHelper{ private static final String TABLE_NAME = "Hellotext.db"; /** * helloDao 没张表对应一个 */ private Dao<Hello,Integer> helloDao = null; /**构造方法*/ public DataHelper(Context context) { super(context, TABLE_NAME, null, 1); } //创建数据表 @Override public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource,Hello.class); Log.d("jiejie","数据库创建成功"); } catch (SQLException e) { e.printStackTrace(); Log.d("jiejie","数据库更新成功"); } } //数据库更新 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) { try { TableUtils.dropTable(connectionSource,Hello.class,true); onCreate(sqLiteDatabase,connectionSource); } catch (SQLException e) { e.printStackTrace(); } } /** * 单例获取该Helper * 1.先把构造函数私有化 * 2.对外提供一个静态方法 * 3.在方法中判断如果已经存在就不再创建,如果不存在再创建这样就永远只有一个DataHelper对象 * 4.为了线程安全,需要再方法钱提供一个线程安全关键字synchronized */ private static DataHelper instance; public static synchronized DataHelper getHeper(Context context){ if(instance == null){ synchronized (DataHelper.class){ if(instance == null){ instance = new DataHelper(context); } } } return instance; } public Dao<Hello,Integer> getHelloDao() throws SQLException{ if(helloDao == null){ helloDao = getDao(Hello.class); } return helloDao; } public synchronized void clearData(Class<Hello> clase){ try { TableUtils.clearTable(connectionSource,clase); } catch (SQLException e) { e.printStackTrace(); } } /** * 释放资源 */ @Override public void close() { super.close(); helloDao = null; } }
4.