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) Privateint ID; @DatabaseField (ColumnName="user_name") Private String userName;//User name@DatabaseField (ColumnName ="Password") private String password;//Password@DatabaseField (ColumnName ="token") Private String token;//Token value PublicintgetId () {returnID; } 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 Publicintdescribecontents () {return0; } @Override public void Writetoparcel (Parcel dest,intflags) {Dest.writeint (this.ID); Dest.writestring (This.username); Dest.writestring (This.password); Dest.writestring (This.token); The public account () {} protected account (Parcelinch) {this.ID=inch. ReadInt (); This.username=inch. readString (); This.password=inch. readString (); This.token=inch. readString (); } public static final Creator<Account> CREATOR = new creator<account>() {@Override public account Createfromparcel (Parcel source) {return new account (source); } @Override Public account[] NewArray (intsize) {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 database operation package * 2016/4/27 9:06.*/Public class Databasehelper extends Ormlitesqliteopenhelper {private static final String table_name="test.db"; Private Map<string, dao> Daos = new hashmap<string, dao>(); 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 connection Source,intOldversion,intnewversion) {try {tableutils.droptable (Connectionsource, Account.class,true); OnCreate (database, Connectionsource); } catch (SQLException e) {e.printstacktrace (); }} private static Databasehelper instance; /** * single case Get the 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; } /** * Release Resources*/@Override public void Close () {super.close (); for(String key:daos.keySet ()) {DAO DAO=Daos.get (key); DAO=NULL; } }}
DAO class
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<account, integer>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 (); } } /** * Add user information * @param account*/Public void Add (account account) {try {accountdao.create (account); } catch (SQLException e) {e.printstacktrace (); } } /** * Update user information * @param account*/Public void Update (account account) {try {accountdao.update (account); } catch (SQLException e) {e.printstacktrace (); } } /** * Get account entity based on token * @param token * @return*/Public account Getaccountwithtoken (String token) {return getaccountwithfiled ("token", token); } /** * Get account entity based on field name and value * @param fieldName * @param fieldval * @return*/Public account getaccountwithfiled (String fieldName, String fieldval) { account account=NULL; try {QueryBuilder<account, integer> QueryBuilder =Accountdao.querybuilder (); Where<account, integer> where =Querybuilder.where (); Where.eq (FieldName, fieldval); Account=Where.queryforfirst (); } catch (SQLException e) {e.printstacktrace (); } return account; }}
SQLite ormlite Framework Application