android ormlite的简单使用
16lz
2021-12-04
1:了解什么是ormlite
ormlite 官网:http://ormlite.com/
首先要知道的是ormlite 是java web的数据库工具,其次才兼容android 的sqlite3。那么肯定的是,我们是因为android才使用ormlite。
介绍 android 的网址
要注重pdf文档的阅读http://ormlite.com/docs/ormlite.pdf在这个文档中,应该阅读的是关于android的部分。
2:使用在android 中使用 ormlite
(1)eclipse中在maven中下载jar 下载链接 ormlite-android和ormlite-core 的jar文件
(2)android studio 中
build.gradle 中添加
dependencies {
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
}
3:在代码中如何使用ormlite
逻辑结构 a:写一个Modle类 b:写一个类继承OrmLiteSqliteOpenHelper c:在Activity中写逻辑(逻辑可另外写个类,Activity调用,类似MVP模式)4:Modle 类
@DatabaseTable(tableName = "test")
public class Test {
Test() {
}//必须有
//注意的是,声明的类型顺序会影响在数据库中的列续
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private long time;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
5:写一个DataBaseHelper
这个用于创建数据库和当数据库表有出现修改的时候,可在这里配置当数据表修改和升级后的表格。在写完 DataBaseHelper之后,我们需要在Activity中使用这个DataBaseHelper的DAO
public class DataBaseHelper_ormlite extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "ormlite.db";//数据库名字
private static final int DATABASE_VERSION = 1;//版本
private Dao<TheCacheInfo,Integer> theCacheInfos;
private Dao<Test,Integer> integerDao;
public DataBaseHelper_ormlite(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Test.class);
//在这里可以添加数据进入到数据库,当数据库刚刚创建的时候
// dao.create(simple);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, Test.class, true);
// after we drop the old databases, we create the new ones
// 会终止旧的数据库,会创建一个新的数据库
onCreate(database, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
//获取数据库Dao操作
public Dao<Test,Integer> getintegerDao() throws SQLException {
if (integerDao==null){
integerDao = getDao(Test.class);
}
return integerDao;
}
@Override
public void close() {//关闭Dao
super.close();
integerDao = null;
}
}
6:Activity 中的调用
public class TestUI extends BaseActivity {
private Dao<Test, Integer> simpleDao = null;
private static String TAG = "TestUI";
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.testui_layout);
init();
}
private void init() {
try {
simpleDao = getHelper().getintegerDao();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void ADD(View view) {
if (simpleDao == null) {
init();
}//判断是否为空
add(System.currentTimeMillis());//添加数据
}
private void add(long time) {
try {
Test test = new Test();
test.setTime(time);
simpleDao.create(test);
Log.w(TAG, "数据添加成功 :" + time);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void Query(View view) {//对添加的数据查询
Log.w(TAG,"Query");
// try {
// List<Test> list = simpleDao.queryForAll();
// for (Test test:list){
// Log.w(TAG,"id:"+test.getId()+" 秒:"+test.getTime());
// }
//
// } catch (SQLException e) {
// e.printStackTrace();
// }
query(5, offest0);
}
private long offest0 = 0;//偏移量
private void query(long number, long offest) {
// Log.w("数据:","query开始");
try {
GenericRawResults<String[]> rawResults =
simpleDao.queryRaw(
"select * from test order by time DESC limit " + number + " offset " + offest);//测试可用
List<String[]> list = rawResults.getResults();
if (list.size()<number){
offest0= offest0+list.size();
}else {
offest0 = offest0+number;
}
for (String[] strings : list) {
for (String item :strings){
Log.w("数据:",""+item);
}
}//判断String 的长度和内容
} catch (SQLException e) {
e.printStackTrace();
}
}
public void DeleteAll(View view) throws SQLException {
simpleDao.delete(simpleDao.queryForAll());
}
private DataBaseHelper_ormlite databaseHelper = null;
private DataBaseHelper_ormlite getHelper() {//通过反射机制,获取databaseHelper 得到Dao
if (databaseHelper == null) {
databaseHelper = OpenHelperManager.getHelper(this, DataBaseHelper_ormlite.class);
}
return databaseHelper;
}
@Override
protected void onDestroy() {//每次Destroy后,销毁资源
super.onDestroy();
databaseHelper.close();
if (databaseHelper != null) {
OpenHelperManager.releaseHelper();
databaseHelper = null;
}
}
}
7:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ADD"
android:text="添加数据"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Query"
android:text="查询数据"/>
<Button
android:onClick="DeleteAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除全部"/>
</LinearLayout>
查询的时候存在偏移量,是为了更好的体验。 仅仅是简单的使用~~哦
来自为知笔记(Wiz)
更多相关文章
- Android的四大组件
- Learning Android(第一季)01-Android平台一日游
- 系出名门Android(9) - 数据库支持(SQLite),
- android manifest.xml 标签汇总
- Android的四大组件
- 简析Android对Linux内核的改动-(上)
- 【Android】SD卡的安全存储问题
- Android自定义视图一:扩展现有的视图,添加新的XML属性
- Android(安卓)Media (Audio) Framework Analyse