安卓端有很多优秀的数据库框架来操作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. How to deal with the problem '<' in OpenERP's view file

    In this case,if you write some stirng in your fields which contains '<' , OpenERP will give a err ...

  2. Mac下使用crontab来实现定时任务

    说明: 1.Linux和Mac下操作crontab都是一致的 2.配置文件都在/etc/crontab下,如果没有就创建. 3.测试发现直接使用crontab -e命令创建的定时任务是放在临时文件夹的 ...

  3. springboot项目:Redis分布式锁的使用(模拟秒杀系统)

    模拟秒杀系统: 第一步:编写Service package com.payease.service; /** * liuxiaoming * 2017-12-14 */ public interfac ...

  4. java常量类的实现方式_枚举类_项目实践

    前言 众所周知,系统里有很多比如订单状态.审核状态:性别.结算方式.交易类型等属性,这些属性只有几个值,一般用0.1.2.3等的数字标识存入数据库,每次对这些属性所属对象的增删改操作,都会在代码里给状 ...

  5. JVM的类加载时机

    类加载过程中每个步骤的顺序 我们已经知道,类加载的过程包括:加载.连接.初始化,连接又分为:验证.准备.解析,所以说类加载一共分为5步:加载.验证.准备.解析.初始化. 其中加载.验证.准备.初始化的 ...

  6. 13 Timer和TimerTask 示例

    定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行.在Java中,可以通过Timer和TimerTask类来实现定义调度的功能 1 Timerjava.lang.Objec ...

  7. linux下perforce(p4)的使用方法和命令

    环境变量: export P4PASSWD=abcdefg export P4CLIENT=dyoldfish.com export P4USER=dyoldfish export P4PORT=19 ...

  8. nginx 配置 单页面应用的解决方案

    server { listen 80; server_name example.com; root /var/www/example.com; gzip_static on;  location / ...

  9. jquery操作radio单选按钮,实现取值,动态选中,动态删除的各种方法

    本文主要讲的是在jquery里操作表单radio单选按钮的各种方法,如获取选中的radio的值,动态选中指定的radio项等. 1.获取选中的radio单选按钮的值: var v=$(":r ...

  10. Python——爬虫学习1

    爬虫了解一下 网络爬虫(Web crawler),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. Python的安装 本篇教程采用Python3 来写,所以你需要给你的电脑装上Python ...