博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqlite ORMLite 框架应用
阅读量:5753 次
发布时间:2019-06-18

本文共 6407 字,大约阅读时间需要 21 分钟。

bean

package com.test.deamo.bean;import android.os.Parcel;import android.os.Parcelable;import com.j256.ormlite.field.DatabaseField;import com.j256.ormlite.table.DatabaseTable;/** * author: yhf * Description: * date: 2016/04/20 18:22 */@DatabaseTable(tableName = "tb_account")public class Account implements Parcelable {    @DatabaseField(generatedId = true)    private int id;    @DatabaseField(columnName = "user_name")    private String userName;// 用户姓名    @DatabaseField(columnName = "password")    private String password;//密码    @DatabaseField(columnName = "token")    private String token;// token值    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }        public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getToken() {        return token;    }    public void setToken(String token) {        this.token = token;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(this.id);        dest.writeString(this.userName);        dest.writeString(this.password);        dest.writeString(this.token);    }    public Account() {    }    protected Account(Parcel in) {        this.id = in.readInt();        this.userName = in.readString();        this.password = in.readString();        this.token = in.readString();    }    public static final Creator
CREATOR = new Creator
() { @Override public Account createFromParcel(Parcel source) { return new Account(source); } @Override public Account[] newArray(int size) { return new Account[size]; } };}
DatabaseHelper
package com.test.deamo.utils.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;import com.j256.ormlite.dao.Dao;import com.j256.ormlite.support.ConnectionSource;import com.j256.ormlite.table.TableUtils;import com.test.deamo.bean.Account;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;/** * Created by administrator * Description 数据库操作封装 * 2016/4/27 9:06. */public class DatabaseHelper extends OrmLiteSqliteOpenHelper {    private static final String TABLE_NAME = "test.db";    private Map
daos = new HashMap
(); private DatabaseHelper(Context context) { super(context, TABLE_NAME, null, 4); } @Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, Account.class); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { TableUtils.dropTable(connectionSource, Account.class, true); onCreate(database, connectionSource); } catch (SQLException e) { e.printStackTrace(); } } private static DatabaseHelper instance; /** * 单例获取该Helper * * @param context * @return */ public static synchronized DatabaseHelper getHelper(Context context) { context = context.getApplicationContext(); if (instance == null) { synchronized (DatabaseHelper.class) { if (instance == null) instance = new DatabaseHelper(context); } } return instance; } public synchronized Dao getDao(Class clazz) throws SQLException { Dao dao = null; String className = clazz.getSimpleName(); if (daos.containsKey(className)) { dao = daos.get(className); } if (dao == null) { dao = super.getDao(clazz); daos.put(className, dao); } return dao; } /** * 释放资源 */ @Override public void close() { super.close(); for (String key : daos.keySet()) { Dao dao = daos.get(key); dao = null; } }}

dao类

package com.test.deamo.dao;import android.content.Context;import com.j256.ormlite.dao.Dao;import com.j256.ormlite.stmt.QueryBuilder;import com.j256.ormlite.stmt.Where;import com.test.deamo.bean.Account;import com.test.deamo.utils.db.DatabaseHelper;import java.sql.SQLException;public class AccountDao {    private Context context;    private Dao
accountDao; private DatabaseHelper databaseHelper; public AccountDao(Context context) { this.context = context; try { databaseHelper = DatabaseHelper.getHelper(this.context); accountDao = databaseHelper.getDao(Account.class); } catch (SQLException e) { e.printStackTrace(); } } /** * 添加用户信息 * @param account */ public void add(Account account){ try { accountDao.create(account); } catch (SQLException e) { e.printStackTrace(); } } /** * 更新用户信息 * @param account */ public void update(Account account){ try { accountDao.update(account); } catch (SQLException e) { e.printStackTrace(); } } /** * 根据token获取account实体 * @param token * @return Account account */ public Account getAccountWithToken(String token){ return getAccountWithFiled("token", token); } /** * 根据字段名和值获取账号实体 * @param fieldName * @param fieldVal * @return */ public Account getAccountWithFiled(String fieldName, String fieldVal){ Account account = null; try { QueryBuilder
queryBuilder = accountDao.queryBuilder(); Where
where = queryBuilder.where(); where.eq(fieldName, fieldVal); account = where.queryForFirst(); } catch (SQLException e) { e.printStackTrace(); } return account; }}

 

转载地址:http://avkkx.baihongyu.com/

你可能感兴趣的文章
springmvc下的web.xml的配置
查看>>
主机-配件-接口-整机-3c-1
查看>>
职业生涯
查看>>
JQ_完美hover技巧
查看>>
索引 聚集索引 唯一索引 普通索引 联合索引 覆盖索引
查看>>
HTML学习笔记03-HTML基础
查看>>
Delphi Webbrowser使用方法详解(二)
查看>>
python画地图学校_python使用pyecharts库画地图数据可视化的实现
查看>>
mysql身份证校验码_通过SQL校验身份证号码是否正确
查看>>
python列表推导式求完全数_Python列表推导式(List comprehension)
查看>>
python读取文件夹下所有图像_Python 读取指定文件夹下的所有图像方法
查看>>
temp在python里的意思_python temp是什么
查看>>
python元组可以修改吗_python元组元素可以修改吗
查看>>
python中文分词与词云画像_使用Python绘制肖像词云
查看>>
python中替换字符串中子串的函数为_python替换字符串中的子串图文步骤
查看>>
怎么用python写数据库_Python实现数据库编程方法详解
查看>>
linux tcp测试工具_Linux下3种常用的网络测速工具
查看>>
nacos动态配置数据源_Spring Cloud 系列之 Alibaba Nacos 配置中心
查看>>
通信维修专用电源_万可PRO 2电源 | 开拓性通信功能,自信迈入数字化时代
查看>>
django图片上传到oss_从攻防角度看oss安全(二)
查看>>