因为这个项目的业务层很薄,因此想在架构上尽量保持着「轻」,不会把创建DbHelper的interface。

而是直接用DAO创建DbHelper对象。

DAO和DbHelper也是同样使用懒汉模式。

1. UserDAO懒汉模式。

public class UserDAO {

    private Configuration config = new Configuration();
protected SQLiteDatabase db;
private DbHelper dbHelper; public UserDAO(Context context) {
this.dbHelper = DbHelper.getInstance(context);
this.db = this.dbHelper.getWritableDatabase();
} }

2. DAO负责处理Query SQlite的业务。这里分别有6个方法。

1. boolean setUser()
2. UserDTO getUser()
3. int getUserCount()
4. boolean setUserPortrait(Bitmap)
5. Bitmap getUserPortrait()
6. byte[] getUserPortraitBYTE()

3. 完整的DAO代码如下。

package com.paradise.electronic.eparadise2.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import com.paradise.electronic.eparadise2.dto.UserDTO;
import com.paradise.electronic.eparadise2.sqlite.DbHelper;
import com.paradise.electronic.eparadise2.util.Configuration; import java.io.ByteArrayOutputStream; /**
* Created by Administrator on 17-11-17.
*/
public class UserDAO { private Configuration config = new Configuration();
protected SQLiteDatabase db;
private DbHelper dbHelper; public UserDAO(Context context) {
this.dbHelper = DbHelper.getInstance(context);
this.db = this.dbHelper.getWritableDatabase();
} /*
* @return 1: Successful 0: Failed
*/
public boolean setUser (UserDTO user) {
ContentValues values = new ContentValues();
boolean result = true; values.put(config.USER_USERID, user.getId());
values.put(config.USER_NAME, user.getName());
values.put(config.USER_PASSWORD, user.getPassword());
values.put(config.USER_EMAIL, user.getEmail());
values.put(config.USER_DESCRIPTION, user.getDescription());
values.put(config.USER_TEAM, user.getTeam());
values.put(config.USER_ACADEMIC, user.getAcademic());
values.put(config.USER_PORTRAIT, user.getPortrait());
values.put(config.USER_REGISTER_DATE, user.getRegisterDate()); try {
db.insert(config.DB_USER, null, values);
} catch (SQLException e) {
result = false;
}
return result;
} /*
* @return UserDTO
*/
public UserDTO getUser() {
String query = " SELECT "+config.USER_USERID+
", "+config.USER_NAME+
", "+config.USER_PASSWORD+
", "+config.USER_EMAIL+
", "+config.USER_DESCRIPTION+
", "+config.USER_TEAM+
", "+config.USER_ACADEMIC+
", "+config.USER_PORTRAIT+
", "+config.USER_REGISTER_DATE+
" FROM "+config.DB_USER+
" ORDER BY "+config.USER_ID+" DESC LIMIT 1 ";
Cursor cursor = db.rawQuery(query, null); if (cursor != null) {
cursor.moveToFirst();
} UserDTO user = new UserDTO(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7),
cursor.getString(8));
return user;
} public void updateUser() { } public void deleteUser() { } public int getUserCount() {
String query = " SELECT * FROM "+config.DB_USER;
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
return count;
} /*
* @param [Bitmap]portrait
* @return 1: Successful 0: Failed
*/
public boolean setUserPortrait(Bitmap portrait) {
ContentValues values = new ContentValues();
boolean result = true; ByteArrayOutputStream stream = new ByteArrayOutputStream();
portrait.compress(Bitmap.CompressFormat.JPEG, 0, stream); values.put(config.USER_PORTRAIT_DATA, stream.toByteArray());
try {
db.insert(config.DB_USER, null, values);
} catch (SQLException e) {
result = false;
}
return result;
} /*
* @return [Bitmap]portrait
*/
public Bitmap getUserPortrait() {
String query = " SELECT "+config.USER_USERID+
", "+config.USER_PORTRAIT_DATA+
" FROM "+config.DB_USER+
" ORDER BY "+config.USER_ID+" DESC LIMIT 1 ";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
} byte[] bytePortrait = cursor.getBlob(0);
Bitmap portrait = BitmapFactory.decodeByteArray(bytePortrait, 0, bytePortrait.length);
return portrait;
} /*
* @reyurn [byte]protrait
*/
public byte[] getUserPortraitBYTE() {
String query = " SELECT "+config.USER_USERID+
", "+config.USER_PORTRAIT_DATA+
" FROM "+config.DB_USER+
" ORDER BY "+config.USER_ID+" DESC LIMIT 1 ";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
} return cursor.getBlob(0);
} }

Sqlite教程(2) Data Access Object的更多相关文章

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ...

  2. csharp: Procedure with DAO(Data Access Object) and DAL(Data Access Layer)

    sql script code: CREATE TABLE DuCardType ( CardTypeId INT IDENTITY(1,1) PRIMARY KEY, CardTypeName NV ...

  3. Sqlite教程(4) Activity

    之前我们已经有了DbHelper.Data Access Object.Configuration. 那麽现在就是由Activity去创建它们,然後就可以存取Sqlite. 架构图表示了它们的关系. ...

  4. 问题-Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed.

    问题现象:软件在启动时报如下错误信息:Exception Exception in module zhujiangguanjia.exe at 001da37f. Error creating obj ...

  5. FunDA(0)- Functional Data Access accessible to all

    大数据.多核CPU驱动了函数式编程模式的兴起.因为函数式编程更适合多线程.复杂.安全的大型软件编程.但是,对许多有应用软件开发经验的编程者来说,函数式编程模式是一种全新的.甚至抽象的概念,可能需要很长 ...

  6. [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)

    Performance Comparison: Data Access Techniques Priya DhawanMicrosoft Developer Network January 2002 ...

  7. Data transfer object

    Data transfer object (DTO) is a design pattern used to transfer data between software application su ...

  8. Apache Cloudstack Development 101 -- Data Access Layer

    刚接触CloudStack,也是第一次翻译英文文档,限于水平有限,不当之处欢迎拍砖! 原文地址:https://cwiki.apache.org/confluence/display/CloudSta ...

  9. Xamarin SQLite教程Xamarin.iOS项目添加引用

    Xamarin SQLite教程Xamarin.iOS项目添加引用 使用直接方式访问SQLite数据库,需要将System.Data和Mono.Data.SQlite库导入到创建的项目中.下面将分别讲 ...

随机推荐

  1. mysql第四篇:数据操作之单表查询

    单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...

  2. 前端框架vue学习笔记

    占坑

  3. Multiarmed Bandit Algorithm在股票中的应用

    股票与Bandit Machine看起来相去甚远,但实际上通过限制买入和卖出的行为,股票可以转换为Bandit Machine,比如:规定股票必须在买入一天以后卖出.为什么要大费周折地把股票变成Ban ...

  4. 读取cookie、写进cookie方法

    整理 读取cookie.写进cookie方法. //设置cookies中的值 function setCookie(name, value) { var Days = 30; var exp = ne ...

  5. 合并排序_python

    #!/usr/bin/python # --coding:utf-8 -- def sort_merge(left,right): i,j=0,0 result=[] while i<len(l ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 元数据

    你可能想知道MySQL以下三种信息: 查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数. 数据库和数据表的信息: 包含了数据库及数据表的结构信息. MySQL服务器信息: ...

  7. Django1.11创建

    Djiango 1.11.1 虚拟环境配置 创建虚拟环境 mkvirtualenv 虚拟环境名字 -p python3 安装django pip install django==1.11.11 卸载包 ...

  8. Jetson TX2入门学习之Ubuntu默认密码

    在使用TX2开发板时进行软件更新时需要身份验证,TX2默认有两个登录身份,一个是ubuntu 一个是nvidia 登录其中的哪一个都可以更新   两个身份的密码和登录名是一样的用户:ubuntu 密码 ...

  9. 题解【[HAOI2006]受欢迎的牛】

    切水题,写题解~ tarjan缩一波点,然后 只有一个出度为0的点:他的size就是答案 有多个初度为0的点:无解,0个 因为是强联通分量,所以肯定有出度为0的点,否则--就是你tarjan写挂了~ ...

  10. TCP/IP通信过程

    一.参考网址 1.以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 2. 二.TCP的建立过程 1.例子: 192.168.22.66 telenet到192.168.22.74的tcp ...