安卓端有很多优秀的数据库框架来操作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. Linux下ls命令使用详解(转)

    说明:我们在linux下使用ll时,其实就是ls -l.ls才是最终的命令程序. ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,List即列表的意 ...

  2. (转)一次棘手的rootvg更换硬盘处理过程

    一次棘手的rootvg更换硬盘处理过程 原文:http://www.talkwithtrend.com/Article/160857 事件起因 下午接到现场工程师电话,一台双系统抽屉IBM P570一 ...

  3. jQuery多库共存问题解决方法

    一.问题概述: 1.随着jQuery的流行,采用jQuery和$符为命名空间的js库越来越多,当然jQuery的$符也是参照的Prototype库的,所以当多个库同时以$符或者jQuery为命名空间时 ...

  4. nuxt.js引入客户端脚本和第三方库出现window/document/ navigator未定义问题

    官方文档中已经给出解决方案: 实际操作也比较简单,比如我之前在项目中引入的wangeditor,这个插件里包含了navigator内容 解决:现在nuxt.config.js的webpack扩展配置中 ...

  5. Javac词法分析

    参考:<深入分析Java Web>技术内幕 许令波 词法分析过程涉及到的主要类及相关的继承关系如下: 词法分析的接口为Lexer,默认实现类为Scanner,Scanner会逐个读取Jav ...

  6. java线程状态 以及 sheep()、wait()、yield() 区别

    前言 最近看到很多人都在讨论多线程的问题,于是写出了这篇博客,希望可以帮到正在学习和使用这块的朋友们,首先我们先看看两个图(两个图都来自其他码农的分享)   这两个图是一样的逻辑,这里一起罗列出来,下 ...

  7. Ibatis框架之系统架构

    如果用最简洁的话来总结 iBATIS 主要完成那些功能时,我想下面几个代码足够概括. Class.forName("oracle.jdbc.driver.OracleDriver" ...

  8. [转]How to add a script in a partial view in MVC4?

    本文转自:https://stackoverflow.com/questions/14114084/how-to-add-a-script-in-a-partial-view-in-mvc4 问题: ...

  9. 八: 操作提示(wxml 即将废弃)

    首先需要注意的是 wxml的这些属性将要被废弃,不过可以看两眼.不愿意看的可以看下一章节同样是操作回馈只不过是js版的哦.   一.action-sheet 操作菜单 从屏幕底下出来菜单. 这里不用w ...

  10. Metronic 对话 chat

    http://keenthemes.com/preview/metronic/theme/admin_1/index.html: jquery让滚动条默认在最底部:$('#content').scro ...