安卓端有很多优秀的数据库框架来操作sqlite,如ormlite框架,这个框架可以用来实现表到对象的解析和转化。

使用:

首先去官网下载两个jar包,core和android(如果在安卓端开发的话),或者是在gradle配置文件build.gradle中加入

implementation 'com.j256.ormlite:ormlite-android:4.48' 和 implementation 'com.j256.ormlite:ormlite-core:5.0'

在使用ormlite的时候,首先得根据自己设计的数据库结构,新建几个对象类,这些对象类就对应了sql中的表。

这些对象类应该包括,成员变量(用来表示表结构中的列和字段名),对应的set和get方法。

在类名,和成员变量前应该加入描述符@DatabaseTable(....可用的参数和属性设置....)  @DatabaseField(.....可用的参数和属性设置....如id = true 即为设置这一列为主键)

一对多外键作为一个成员变量放在多的那个表中,而多对多外键则建一张新的表作为媒介,表列(成员变量)即为有关系的两个对象。

数据库结构建立好了之后,下面就是操作的部分了。

创建ORMLite数据库管理工具类ORMLiteDatabaseHelper.java:

  

package com.example.dell.db;

import android.content.Context;
import android.util.Log; import com.j256.ormlite.cipher.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import net.sqlcipher.database.SQLiteDatabase; import java.sql.SQLException;
import java.util.List; /**
* Created by wu-pc on 2018/5/9.
* Copied from official example, and revised for my own purpose
*/ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { final static private String TAG = "DatabaseHelper"; // name of the database file for your application -- change to something appropriate for your app
private static final String DATABASE_NAME = "heartTrace.db";
// any time you make changes to your database objects, you may have to increase the database version
private static final int DATABASE_VERSION = 2; // the DAO object we use to access the Diary table
private Dao<Diary, Integer> diaryDao = null;
private RuntimeExceptionDao<Diary, Integer> runtimeDiaryDao = null; private Dao<DiaryLabel, Integer> diaryLabelDao = null;
private RuntimeExceptionDao<DiaryLabel, Integer> runtimeDiaryLabelDao = null; private Dao<Diarybook, Integer> diarybookDao = null;
private RuntimeExceptionDao<Diarybook, Integer> runtimeDiarybookDao = null; private Dao<Sentence, Integer> sentenceDao = null;
private RuntimeExceptionDao<Sentence, Integer> runtimeSentenceDao = null; private Dao<Label, Integer> labelDao = null;
private RuntimeExceptionDao<Label, Integer> runtimeLabelDao = null; private Dao<SentenceLabel, Integer> sentenceLabelDao = null;
private RuntimeExceptionDao<SentenceLabel, Integer> runtimeSentenceLabelDao = null; private Dao<Sentencebook, Integer> sentencebookDao = null;
private RuntimeExceptionDao<Sentencebook, Integer> runtimeSentencebookDao = null; private Dao<SearchHistory, Integer> searchHistoryDao = null;
private RuntimeExceptionDao<SearchHistory, Integer> runtimeSearchHistoryDao = null; private PreparedQuery<Label> labelForDiaryQuery;
private PreparedQuery<Diary> diaryForLabelQuery; public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase.loadLibs(context);
} /**
* This is called when the database is first created. Usually you should call createTable statements here to create
* the tables that will store your data.
*/
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Diary.class);
TableUtils.createTable(connectionSource, Diarybook.class);
TableUtils.createTable(connectionSource, DiaryLabel.class);
TableUtils.createTable(connectionSource, Label.class);
TableUtils.createTable(connectionSource, Sentence.class);
TableUtils.createTable(connectionSource, SentenceLabel.class);
TableUtils.createTable(connectionSource, Sentencebook.class);
TableUtils.createTable(connectionSource, SearchHistory.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
} /**
* This is called when your application is upgraded and it has a higher version number. This allows you to adjust
* the various data to match the new version number.
* Don't need it by now.
*/
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, Diary.class, true);
TableUtils.dropTable(connectionSource, Diarybook.class, true);
TableUtils.dropTable(connectionSource, DiaryLabel.class, true);
TableUtils.dropTable(connectionSource, Label.class, true);
TableUtils.dropTable(connectionSource, Sentence.class, true);
TableUtils.dropTable(connectionSource, SentenceLabel.class, true);
TableUtils.dropTable(connectionSource, Sentencebook.class, true);
TableUtils.dropTable(connectionSource, SearchHistory.class, true);
// after we drop the old databases, we create the new ones
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
} protected String getPassword() {
return "hello I'm password";
} /**
* Returns the Database Access Object (DAO) for our Diary class. It will create it or just give the cached
* value.
*/
public Dao<Diary, Integer> getDiaryDao() throws SQLException {
if (diaryDao == null) {
diaryDao = getDao(Diary.class);
}
return diaryDao;
} public Dao<Sentence, Integer> getSentenceDao() throws SQLException {
if(sentenceDao == null) {
sentenceDao = getDao(Sentence.class);
}
return sentenceDao;
} public Dao<Label, Integer> getLabelDao() throws SQLException {
if (labelDao == null) {
labelDao = getDao(Label.class);
}
return labelDao;
} public Dao<DiaryLabel, Integer> getDiaryLabelDao() throws SQLException {
if (diaryLabelDao == null) {
diaryLabelDao = getDao(DiaryLabel.class);
}
return diaryLabelDao;
} //for the sentence public Dao<SentenceLabel, Integer> getSentenceLabelDao() throws SQLException {
if (sentenceLabelDao == null) {
sentenceLabelDao = getDao(SentenceLabel.class);
}
return sentenceLabelDao;
} public Dao<Sentencebook, Integer> getSentencebookDao() throws SQLException {
if (sentencebookDao == null) {
sentencebookDao = getDao(Sentencebook.class);
}
return sentencebookDao;
} public Dao<Diarybook, Integer> getDiarybookDao() throws SQLException {
if (diarybookDao == null) {
diarybookDao = getDao(Diarybook.class);
}
return diarybookDao;
} public Dao<SearchHistory, Integer> getSearchHistoryDao() throws SQLException {
if (searchHistoryDao == null) {
searchHistoryDao = getDao(SearchHistory.class);
}
return searchHistoryDao;
}

通过Helper得到相应表结构的dao之后就可以对数据库进行一系列的操作了

如dao.create(diary)

dao.delete(diary)

查找:需要先得到一个queryBuilder:

QueryBuilder<Diary, Integer> qb = helper.getDiaryDao().queryBuilder();

在这个queryBuilder的where()返回的where对象中加入查找的条件

如:qb.where().eq(列名, 待查)

在加入了所有的查找条件之后,qb.query()返回查找的所有返回对象,是一个List。

android端的ormlite框架的更多相关文章

  1. [Android]Android端ORM框架——RapidORM(v2.1)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6020412.html [Android]Android端ORM ...

  2. [Android]Android端ORM框架——RapidORM(v2.0)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5626716.html [Android]Android端ORM ...

  3. android对象关系映射框架ormlite之一对多(OneToMany)

    前两天,用ormlite对单张表进行了基本的操作,但是,我们知道通常情况对于单张表格进行操作在实际情况中很前两天不现实,那么ormlite能否像Hibenate那样实现多张表之间的一对多,多对多(即O ...

  4. Android ORMLite 框架的入门用法

    大家在Android项目中或多或少的都会使用数据库,为了提高我们的开发效率,当然少不了数据库ORM框架了,尤其是某些数据库操作特别频繁的app:本篇博客将详细介绍ORMLite的简易用法. 下面开始介 ...

  5. android数据库持久化框架, ormlite框架,

    前言 Android中内置了SQLite,但是对于数据库操作这块,非常的麻烦.其实可以试用第3方的数据库持久化框架对之进行结构上调整, 摆脱了访问数据库操作的细节,不用再去写复杂的SQL语句.虽然这样 ...

  6. 用.Net打造一个移动客户端(Android/IOS)的服务端框架NHM(四)——Android端Http访问类(转)

    本章目的 在上一章中,我们利用Hibernate Tools完成了Android Model层的建立,依赖Hibernate Tools的强大功能,自动生成了Model层.在本章,我们将继续我们的项目 ...

  7. Java服务器对外提供接口以及Android端向服务器请求数据

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5056780.html 讲解下java服务器是如何对移动终端提供接口的,以什么数据格式提供出去,移动端又是怎么 ...

  8. pc端和android端应用程序测试有什么区别?(ps面试题)

    pc端和android端应用程序测试有什么区别?(ps面试题) [VIP7]大连-凭海临风(215687736) 2014/4/10 8:56:171.测试环境不同PC平台一般都是windows an ...

  9. 【转载】Android端手机测试体系

    1.冒烟测试 跟web端 的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做 我们的测试工具.就跟我之前博 ...

随机推荐

  1. Smarty <= 3.1.32 Remote Code execution(CVE-2017-1000480)

    Smarty介绍   smarty是一个php模板引擎,其项目地址:https://github.com/smarty-php/smarty 测试环境搭建   下载:https://github.co ...

  2. (四)Audio子系统之AudioRecord.read

      在上一篇文章<(三)Audio子系统之AudioRecord.startRecording>中已经介绍了AudioRecord如何开始录制音频,接下来,继续分析AudioRecord方 ...

  3. pytest文档博客链接

    关于pytest的博客:   https://www.cnblogs.com/yoyoketang/tag/pytest/default.html?page=2

  4. java中的this和super(构造函数)

    1.this:表示当前对象 常用的代码: public class A{ private String name; public void setName(String name){ this.nam ...

  5. C# 扩展方法一

    1. 何为扩展方法 扩展方法是C#3.0引入的语法特性,是一种特殊的静态方法.它使得我们能向现有的数据类型“动态”添加方法,而不需要创建行的派生类型.重新编译或直接修改原始类型的源代码. 注意扩展方法 ...

  6. Universal App图片文件和图片byte[]信息转换为bitmap

    1. 打开图片文件并转换为BitmapImage类 首先要做的自然是打开一个图片文件了,可以使用FileOpenPicker来手动选择图片,总之能拿到一个StorageFile都行. //打开图片选择 ...

  7. RabbitMQ---5、qos内存溢出+prefetch消息堵塞问题

    1.prefetch消息堵塞问题 mq是实现代码扩展的有利手段,个人喜欢用概念来学习新知识,介绍堵塞问题的之前,先来段概念的学习. ConnectionFactory:创建connection的工厂类 ...

  8. <深入理解JavaScript>学习笔记(2)_揭秘命名函数表达式

    写在前面的话 注:本文是拜读了 深入理解JavaScript 之后深有感悟,故做次笔记方便之后查看. 感觉这章的内容有点深奥....略难懂啊. 先坐下笔记,加深一下印象吧. 我主要记一下自己感觉有用的 ...

  9. 6、Object、String、StringBuffer

    Java的Api以及Object类 API概念 * A:API(Application Programming Interface) * 应用程序编程接口 * B:Java API * 就是Java提 ...

  10. git远程易错点

    git pull下来用git branch -r查看远程分支才有数据 解决方案:指定当前工作目录工作分支,跟远程的仓库,分支之间的链接关系. 比如我们设置master对应远程仓库的master分支 g ...