OrmLite can help us operate databases without having to write SQL statements by ourselves, and set it in the same way as using Sqlite. It is a relational data, what I like most is that it helps me determine whether data table items are updated or created, and does not need to write a large number of SQL statements to determine, the following describes its usage documents and some basic notes:
Official Website: http://ormlite.com/
JavaDOC address: http://ormlite.com/javadoc/ormlite-android/
Development Documentation address: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite.html
1. Create a model
First, create a Model as a data table. In Android, Parcelable is implemented. The Code is as follows:
Package com. jwzhangjie. capricorntv. bean; import com. j256.ormlite. field. databaseField; import com. j256.ormlite. table. databaseTable; import android. OS. parcel; import android. OS. parcelable;/***** @ author zj contains the video parameter video name "channel_id": 9, "channel_name": "Hunan TV", * "icon_url": "http:// TV .togic.com: 8080/ShowTimeService/images/182.png", * "province": "Hunan", "mode": "SD", "url": * "http://live.gslb.letv.com/gslb? Stream_id = hunan & tag = live & ext = m3u8 & sign = live_ TV & platid = 10 & splatid = 1012 & temporarykey = db7c39a0ee39ab2d4d2e781d5 "*, *" second_url ": ["http://live-cdn.kksmg.com/channels/tvie/test/flv:500k" *, * "http://live.gslb.letv.com/gslb? Stream_id = hunanHD_1800 & tag = live & ext = m3u8 & sign = live_ TV & platid = 10 & splatid = 1012 & temporarykey = db7c39a0ee39ab2d4d2e781d5 "*, *" http://pplive.shntv.cn/live/5/30/e9301e073cf94732a380b765c8b9573d.m3u8? Type = ipad "*," rtsp: // rlive.tv189.cn/live/112 "]," types ":" 2 | 0 "*/public class LiveItemBean implements Parcelable {@ Overridepublic int describeContents () {return 0;} public LiveItemBean () {} private LiveItemBean (Parcel source) {readFromParcel (source) ;}@ DatabaseField (id = true) private int channel_id; @ DatabaseFieldprivate String channel_name; @ DatabaseFieldprivate String icon_url; @ DatabaseFieldprivate String province; @ DatabaseFieldprivate String mode; @ DatabaseFieldprivate String url; @ DatabaseFieldprivate String second_urls; private String [] second_url; @ DatabaseFieldprivate String types; private void readFromParcel (Parcel source) {channel_name = source. readString (); icon_url = source. readString (); province = source. readString (); mode = source. readString (); url = source. readString (); second_urls = source. readString (); second_url = (String []) source. readArray (LiveItemBean. class. getClassLoader (); types = source. readString () ;}@ Overridepublic void writeToParcel (Parcel dest, int flags) {dest. writeInt (channel_id); dest. writeString (channel_name); dest. writeString (icon_url); dest. writeString (province); dest. writeString (mode); dest. writeString (url); dest. writeString (second_urls); dest. writeArray (second_url); dest. writeString (types);} public static Creator
CREATOR = new Creator
() {@ Overridepublic LiveItemBean createFromParcel (Parcel source) {return new LiveItemBean (source) ;}@ Overridepublic LiveItemBean [] newArray (int size) {return new LiveItemBean [size] ;}}; public int getChannel_id () {return channel_id;} public void setChannel_id (int channel_id) {this. channel_id = channel_id;} public String getChannel_name () {return channel_name;} public void setChannel_name (String channel_n Ame) {this. channel_name = channel_name;} public String getIcon_url () {return icon_url;} public void setIcon_url (String icon_url) {this. icon_url = icon_url;} public String getProvince () {return province;} public void setProvince (String province) {this. province = province;} public String getMode () {return mode;} public void setMode (String mode) {this. mode = mode;} public String getUrl () {return url;} public Void setUrl (String url) {this. url = url;} public String getSecond_urls () {return second_urls;} public void setSecond_urls (String second_urls) {this. second_urls = second_urls;} public String [] getSecond_url () {return second_url;} public void setSecond_url (String [] second_url) {this. second_url = second_url; StringBuffer buffer = new StringBuffer (); int count = second_url.length; for (int I = 0; I <count; I ++) {buffer. append (second_url [I]); if (I! = Count-1) {buffer. append (";") ;}} second_urls = buffer. toString ();} public String getTypes () {return types;} public void setTypes (String types) {this. types = types ;}}
We analyze the above Code and introduce the related Configuration:
1. It is the class name. Here, the name of the data table used is the lower case of the default class. Of course, you can also specify the table name by using @ DatabaseTable (tableName = "liveitembeans "),
2. Primary Key. In the above Code, the annotation object @ DatabaseField (id = true) is the primary key. Of course, we sometimes use an auto-increment id, we can set it to @ DatabaseField (generatedId = true). Of course, there are many other annotation configurations that I will not describe here. You can go to the official website to find what you need.
2. Create DBHelper
package com.jwzhangjie.capricorntv.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;import com.j256.ormlite.support.ConnectionSource;import com.j256.ormlite.table.TableUtils;import com.jwzhangjie.capricorntv.bean.LiveItemBean;public class DBHelper extends OrmLiteSqliteOpenHelper {private static final String DATABASE_NAME = "jwzhangjie.db";private static final int DATABASE_VERSION = 1;public DBHelper(Context context){super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase,ConnectionSource connectionSource) {try {TableUtils.createTable(connectionSource, LiveItemBean.class);} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase,ConnectionSource connectionSource, int oldVer, int newVer) {try {TableUtils.dropTable(connectionSource, LiveItemBean.class, true);onCreate(sqLiteDatabase, connectionSource);} catch (Exception e) {e.printStackTrace();}}}
TableUtils is used to create and Delete tables. Other functions such as clearing table content
3. Create an operation tool
The next step is to create a database-operated tool DAO
Package com. jwzhangjie. capricorntv. uitls; import java. SQL. SQLException; import java. util. arrayList; import java. util. list; import android. content. context; import com. j256.ormlite. dao. dao; import com. jwzhangjie. capricorntv. bean. liveItemBean; import com. jwzhangjie. capricorntv. db. DBHelper; public class DBUtils {public static Dao
LiveDao = null; public DBUtils (Context context) {if (liveDao = null) {DBHelper dbHelper = new DBHelper (context); try {liveDao = dbHelper. getDao (LiveItemBean. class);} catch (SQLException e) {e. printStackTrace () ;}}/ *** insert live data. If the data exists, update ** @ param liveItemBean */public void LiveCreate (LiveItemBean liveItemBean) {try {liveDao. createOrUpdate (liveItemBean);} catch (SQLException e) {e. printStackTrace () ;}}/*** insert consecutively. If yes, update */public void LiveCreates (List
Lists) {try {for (LiveItemBean liveItemBean: lists) {liveDao. createOrUpdate (liveItemBean);} catch (Exception e) {e. printStackTrace () ;}}/*** query all live video elements * @ return */public List
GetLiveItemBeans () {List
ListsBeans = new ArrayList
(); Try {listsBeans = liveDao. queryForAll ();} catch (SQLException e) {e. printStackTrace () ;}return listsBeans ;}}
The creation and query are implemented above. There is a continuous insertion of multiple data above. You can also use the batch processing task method provided by OrmLite as follows:
/*** Insert consecutively. If yes, update */public void LiveCreates (final List
Lists) {try {liveDao. callBatchTasks (new Callable
() {@ Overridepublic Void call () throws Exception {for (LiveItemBean liveItemBean: lists) {liveDao. createOrUpdate (liveItemBean);} return null ;}});} catch (Exception e) {e. printStackTrace ();}}
In addition to the existing interfaces, you can use SQL statements to perform queries. For example:
// find out how many orders account-id #10 has GenericRawResults
rawResults = orderDao.queryRaw( "select count(*) from orders where account_id = 10"); // there should be 1 result List
results = rawResults.getResults(); // the results array should have 1 value String[] resultArray = results.get(0); // this should print the number of orders that have this account-id System.out.println("Account-id 10 has " + resultArray[0] + " orders");
Iv. final release
When the application exits, We need to release the previous object.
OpenHelperManager.releaseHelper();dbHelper = null;