Sqlite教程(2) Data Access Object
因为这个项目的业务层很薄,因此想在架构上尽量保持着「轻」,不会把创建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的更多相关文章
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ...
- 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 ...
- Sqlite教程(4) Activity
之前我们已经有了DbHelper.Data Access Object.Configuration. 那麽现在就是由Activity去创建它们,然後就可以存取Sqlite. 架构图表示了它们的关系. ...
- 问题-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 ...
- FunDA(0)- Functional Data Access accessible to all
大数据.多核CPU驱动了函数式编程模式的兴起.因为函数式编程更适合多线程.复杂.安全的大型软件编程.但是,对许多有应用软件开发经验的编程者来说,函数式编程模式是一种全新的.甚至抽象的概念,可能需要很长 ...
- [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)
Performance Comparison: Data Access Techniques Priya DhawanMicrosoft Developer Network January 2002 ...
- Data transfer object
Data transfer object (DTO) is a design pattern used to transfer data between software application su ...
- Apache Cloudstack Development 101 -- Data Access Layer
刚接触CloudStack,也是第一次翻译英文文档,限于水平有限,不当之处欢迎拍砖! 原文地址:https://cwiki.apache.org/confluence/display/CloudSta ...
- Xamarin SQLite教程Xamarin.iOS项目添加引用
Xamarin SQLite教程Xamarin.iOS项目添加引用 使用直接方式访问SQLite数据库,需要将System.Data和Mono.Data.SQlite库导入到创建的项目中.下面将分别讲 ...
随机推荐
- 实验吧Web-中-登陆一下好吗??
题目上说:不要怀疑,我已经过滤了一切,还再逼你注入,哈哈哈哈哈! 可以试试,只要是输入的关键字都被过滤了,双写也被过滤掉了. 用万能密码发现,or被过滤掉了. 这里用到的是admin为:'=',密码为 ...
- 算法设计和分析(Prim算法构建最小生成树)
问题: 给定无向图G(N,M)表明图G有N个顶点,M条边,通过Prim算法构造一个最小生成树 分析: 算法流程: 构造好的最小生成树就是step6 运行代码: #include<cstdio&g ...
- python脚本下载 Google Driver 文件
使用python脚本下载 Google Driver 文件 import yaml import sys import requests import os import re import tarf ...
- python查找数组中出现次数最多的元素
方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...
- HTTP协议(四):首部
前言 作者说:上一节中介绍了HTTP报文中的状态码,这一节同样是对报文的补充,介绍的是HTTP首部字段.不过,你如果是第一次见到这个东西,肯定会特别疑惑,什么是HTTP首部? <图解HTTP&g ...
- SpringCloud学习之Ribbon使用(四)
1.关于 Ribbon Spring Cloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端负载均衡的工具.Ribbon 是 Netflix 发布的开源项目,主要功能是提供客 ...
- pytorch 自动求梯度
自动求梯度 在深度学习中,我们经常需要对函数求梯度(gradient).PyTorch提供的autograd包能够根据输入和前向传播过程自动构建计算图,并执行反向传播.本节将介绍如何使用autogra ...
- jquery---利用jquery插件生成二维码
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/jquer ...
- WebSocket的简单实现&jsp
创建一个web项目 导入依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- addlayer添加神经网络层
def addlayer(inputs,insize,outsize,activity_function = None): weights = tf.Variable(tf.random_nor ...