<font color=#FF0000>LZ-Says:擼文不易,且行且珍惜~
每日一笑:
假如生活欺騙了你,找50個程序員問問為什么編程;
假如生活讓你想死,找50個程序員問問Bug改完了沒有;
假如你覺得生活拮據(jù),找50個程序員問問工資漲了沒有;
假如你覺得活著無聊,找50個程序員問問他們一天都干了什么!
<center>這里寫圖片描述</center>
前言
端午三天,去天津玩了一圈,出去走走,放松放松,感覺也不錯。來點正能量,今天,讓我們一塊研究研究如何使用GreenDao 3.2.2,從而逐步增強我們的逼格。
來來來,一塊浪起來~
Android開發(fā)過程中,有時候我們需要使用SQLite數(shù)據(jù)庫去本地存儲一些臨時數(shù)據(jù),之前,我們通過繼承SQLiteOpenHelper實現(xiàn)創(chuàng)建數(shù)據(jù)庫,基本表以及迭代開發(fā)中數(shù)據(jù)庫 數(shù)據(jù) 內(nèi)容 字段 變更時處理,簡單了解可以查看LZ之前總結(jié)的一些對SQLite常用知識,地址如下:
而今天為大家介紹的GreenDao 3.2.2,便是與咱上面說的數(shù)據(jù)庫有關(guān)系,接著往下瞧~
首先為大家展示下本篇運行結(jié)果:
<center>GreenDao初識
有的小伙伴可能就會問了,我用的SQLiteOpenHelper挺方便的,干嘛要用這個呢?他有什么優(yōu)勢或者值得讓我去使用的亮點嗎?
下面來聊聊有關(guān)GreenDao的獨特魅力~
官方資料地址
首先為大家附上官方地址,方便英文水準(zhǔn)好的直接查閱,如下:
GreenDao 官網(wǎng):http://greenrobot.org/greendao/
GreenDao 特征介紹:http://greenrobot.org/greendao/features/
GreenDao 學(xué)習(xí)文檔:http://greenrobot.org/greendao/documentation/
GreenDao 更新日志:http://greenrobot.org/greendao/changelog/
GreenDao GitHub地址:https://github.com/greenrobot/greenDAO
接下來為大家介紹有關(guān)GreenDao八大特性,相當(dāng)666哦~
GreenDao 八大特性
- 對象/關(guān)系映射(ORM)[Object/relation mapping]
GreenDAO的本質(zhì)是為存儲在關(guān)系數(shù)據(jù)庫SQLite中的數(shù)據(jù)提供面向?qū)ο蟮慕缑妗?/strong>
<font color=#FF0000>使用過程中,我們只需定義數(shù)據(jù)模型,而GreenDAO將創(chuàng)建Java數(shù)據(jù)對象(實體)和DAO(數(shù)據(jù)訪問對象),節(jié)省部分代碼~
接下來給大家附上一張對象關(guān)系映射圖:
<center></center>
- 性能
關(guān)于這點,引入官方的一句話:
GreenDAO does not make any compromises regarding performance. <font color=#FF0000>(GreenDAO對性能不做任何妥協(xié)) 666~
So 在目前所知道的ORM中,GreenDao是最快的,非常適合存儲大量數(shù)據(jù)。<font color=#FF0000>舉一個簡單的例子,使用了GreenDao,大多數(shù)實體可以以每秒幾千個實體的速率進行插入,更新和加載。 是不是很6?
鑒于咱都是用事實說話的老百姓,下面將官方測試圖例附上供大家觀看。
<center></center>
腫么樣,是不是很666?
三個字概括,<font color=#FF0000>高性能~!!!
- 加密支持
GreenDao支持加密數(shù)據(jù)庫來保護敏感數(shù)據(jù),當(dāng)然你也可以和SQLCipher一塊配合使用。點我查看API文檔
- 微小的依賴庫
GreenDao關(guān)鍵依賴庫大小不超過100kb,So,也不會出現(xiàn)因為引入GreenDao而出現(xiàn)65k問題~<font color=#FF0000>(當(dāng)然,如果你的app快到臨界線了,這個LZ可就不敢保證了哦~</font>)
- 活動實體
如果需要,實體可以被“激活”。而活動實體可以透明地解析關(guān)系(我們要做的只是調(diào)用getter即可),并且有更新、刪除和刷新方法,以便方便地訪問持久性功能
- 協(xié)議緩沖區(qū)支持
GreenDAO允許您將協(xié)議緩沖區(qū)(protobuf)對象直接保存到數(shù)據(jù)庫中。如果您通過protobuf通話到您的服務(wù)器,則不需要另一個映射。常規(guī)實體的所有持久性操作都可用于protobuf對象。So,相信這是GreenDAO的獨特之處。<font color=#FF0000>雖然LZ也不是很懂,相信玩轉(zhuǎn)之后,也會有自己的理解~
- 自動生成代碼
使用GreenDao,我們無需關(guān)注實體類以及Dao,體貼的GreenDao已為我們自動生成了是不是很贊!!!
- 開源
開源,有興趣的同學(xué)可以查看源碼,深入了解機制,流程,當(dāng)然,LZ目前只是停留在會用的層面即可。
同樣在GitHub,到現(xiàn)在還在維護,貌似最近的一次是倆個月前了吧,關(guān)注人還不少,版本也越來越完善,感謝那些背后默默付出的人~
PS:個人覺得開源有利有弊,就比如Android,開源使得Android越來越向成熟,完善之路走去,但是所帶來的弊端同樣不容忽視,各種廠商對應(yīng)的各種定制化Android,讓身為Android開發(fā)的不得不羨慕ios開發(fā),哈哈,純屬LZ個人理解~
了解了以上內(nèi)容,我們總結(jié)下使用GreenDao的優(yōu)勢,如下。
GreenDao 優(yōu)勢
- 目前來說性能最高,內(nèi)存消耗最小,支持數(shù)據(jù)庫加密;
- 依賴庫小于100kb,且使用人數(shù)眾多,維護者也一直在更新;
- 完善的api,并且對Android進行了高度優(yōu)化,個人覺得很不錯~
GreenDao 對外提供核心類簡介
下面為大家附上一張圖,大家可以仔細查看后,在仔細往下看,有助于理解~
<center>1.DaoMaster:
使用GreenDao的切入點(開始)。 DaoMaster保存數(shù)據(jù)庫對象(SQLiteDatabase)并管理特定模式的Dao類(而不是對象)。它具有靜態(tài)方法來創(chuàng)建表或?qū)⑺鼈儎h除。其內(nèi)部類OpenHelper和DevOpenHelper是在SQLite數(shù)據(jù)庫中創(chuàng)建模式的SQLiteOpenHelper實現(xiàn)。
2.DaoSession:
管理特定模式的所有可用Dao對象,您可以使用其中一個getter方法獲取。DaoSession還為實體提供了一些通用的持久性方法,如插入,加載,更新,刷新和刪除。最后,DaoSession對象也跟蹤一個身份范圍。有關(guān)更多詳細信息,[請點擊查看會話文檔](http://greenrobot.org/greendao/documentation/sessions/)。
3.Dao層:
數(shù)據(jù)訪問對象(Dao)持續(xù)存在并查詢實體。對于每個實體,GreenDao生成一個Dao,它比DaoSession有更多的持久化方法,例如:count,loadAll和insertInTx。
4.實體:
持久對象 通常實體是使用標(biāo)準(zhǔn)Java屬性(如POJO或JavaBean)來表示數(shù)據(jù)庫行的對象
鋪墊到這里,感覺也差不多了,開搞吧~
Hey , GreenDao , Let's continue our study~!!!
1. 配置Gradle
1.配置<font color=#FF0000>工程目錄下build.gradle
添加GreenDao插件支持,要添加的內(nèi)容后面都已經(jīng)加上了簡單注釋
buildscript {
repositories {
jcenter()
mavenCentral() // 添加遠程倉庫地址
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // 添加插件 更好支持GreenDao
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
之后點擊右上角Sync Now ,AS會自動下載插件,等待就好~!如下圖所示:
2.配置<font color=#FF0000>項目下的build.gradle
- 添加應(yīng)用插件依賴;
apply plugin: 'org.greenrobot.greendao' // 添加應(yīng)用依賴插件
- 添加庫文件
compile 'org.greenrobot:greendao:3.2.2' // 添加庫
- 初始化GreenDao配置
按照官方的說法,我們無需任何其他配置,但是,數(shù)據(jù)庫版本這個你得考慮下吧,當(dāng)然,如果也不想考慮,那就使用默認的(默認版本為1)。
// 配置GreenDao基本參數(shù)
greendao {
schemaVersion 1 //當(dāng)前數(shù)據(jù)庫版本
}
下面具體為大家介紹下配置中可用參數(shù)作用<font color=#FF0000>(PS:大家可選擇性添加):
1. schemaVersion:當(dāng)前版本的數(shù)據(jù)庫模式。這被OpenHelpers類用于在模式版本之間遷移。如果更改了實體/數(shù)據(jù)庫模式,則必須增加該值。默認為1。
2. daoPackage:生成的Dao,DaoMaster和DaoSession的包名稱。 默認為源實體的包名稱。
3. targetGenDir:生成源應(yīng)存儲在的位置。 默認為構(gòu)建目錄( build / generated / source / greendao)中生成的源文件夾。
4. generateTests: 設(shè)置為true以自動生成單元測試。
5. targetGenDirTests: 應(yīng)存儲生成的單元測試的基本目錄。默認為 src / androidTest / java。
這一步配置完結(jié)果如下,省略部分內(nèi)容:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // 添加應(yīng)用依賴插件
android {
// 省略部分內(nèi)容...
}
// 配置GreenDao基本參數(shù)
greendao {
schemaVersion 1 //當(dāng)前數(shù)據(jù)庫版本
}
dependencies {
// 省略部分內(nèi)容...
compile 'org.greenrobot:greendao:3.2.2' // 添加庫
}
到這里,GreenDao基本配置也就告一段落了`
而使用GreenDao前期準(zhǔn)備工作也告一段落了,那么接下來讓我們實際去感受下GreenDao的魅力吧~
2. 了解如何編寫GreenDao所需要的實體類
在前面一部分我們曾經(jīng)說過,我們只需要去關(guān)注實體類即可,那么這個實體類和普通實體類又有什么區(qū)別?我們該怎么去實現(xiàn)我們的實體類呢?別急,繼續(xù)往下瞅~
我們先看一下官方為我們提供的小例子,通過講解這個小例子,讓我們充分了解關(guān)于使用GreenDao一些方式方法,從而更好的去get這一技能~
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount; // not persisted
// getters and setters for id and user ...
}
首先定義了一個User對象,這個對象包含三個屬性,分別為id,name以及tempUsageCount。乍眼一看,沒啥區(qū)別嘛,可大家注意到了@Entity,@Id以及@Transient這倆個東西了嗎?這是啥?我們一塊具體瞅瞅
-
<font color=#FF0000>@Entity注解
@Entity注解標(biāo)記了一個Java類作為greenDAO一個presistable實體。簡單理解為,他告訴GreenDao,要根據(jù)這個實體類去生成相應(yīng)的Dao,方便我們?nèi)ゲ僮鳎瑯右蚕喈?dāng)于將我們的實體類和表做了關(guān)聯(lián),當(dāng)然這些東西是人GreenDao去搞定的,了解了解總是有好處的嘛`
同樣我們也可以使用@Entity配置一些詳細信息,如下:
@Entity(
// 如果你有一個以上的模式,你可以告訴greendao實體屬于哪個模式(選擇任何字符串作為名稱)。
schema = "myschema",
// 標(biāo)志允許實體類可有更新,刪除,刷新方法
active = true,
// 指定數(shù)據(jù)庫中表的名稱。默認情況下,該名稱基于實體類名。
nameInDb = "AWESOME_USERS",
// 在這里定義多個列的索引
indexes = {
@Index(value = "name DESC", unique = true)
},
// 如果DAO創(chuàng)建數(shù)據(jù)庫表(默認為true),則設(shè)置標(biāo)記去標(biāo)識。如果有多個實體映射到一個表,或者在greenDAO之外創(chuàng)建表創(chuàng)建,將此設(shè)置為false。
createInDb = false,
// 是否應(yīng)該生成所有的屬性構(gòu)造函數(shù)。一個無args構(gòu)造函數(shù)總是需要的
generateConstructors = true,
// 是否生成屬性的getter和setter
generateGettersSetters = true
)
-
<font color=#FF0000>@Id注解
選擇long / Long屬性作為實體ID。在數(shù)據(jù)庫方面,它是主要的關(guān)鍵參數(shù)autoincrement 是使ID值不斷增加的標(biāo)志(不重復(fù)使用舊值),也就是咱常說的**自增長**。
-
<font color=#FF0000>@Property
允許您定義屬性映射到的非默認列名稱。如果缺少,greenDAO將以SQL-ish方式使用字段名稱(大寫字母,下劃線而不是駝峰命名法,例如 customName將成為 CUSTOM_NAME)。注意:當(dāng)前只能使用內(nèi)聯(lián)常量來指定列名。
-
<font color=#FF0000>@NotNull
該屬性在數(shù)據(jù)庫端成為“NOT NULL”列。通常使用@NotNull標(biāo)記原始類型(long,int,short,byte)是有意義的,而具有包裝類(Long,Integer,Short,Byte))的可空值。
-
<font color=#FF0000>@Transient
標(biāo)記要從持久性排除的屬性,使用這些臨時狀態(tài)等。或者,也可以使用來自Java 的transient關(guān)鍵字。
下面為大家拓展倆個注解,有用到的可以參考下:
-
<font color=#FF0000>@Index
為相應(yīng)的數(shù)據(jù)庫列創(chuàng)建數(shù)據(jù)庫索引 名稱:如果不喜歡greenDAO為索引生成的默認名稱,則可以在此處指定。 唯一:向索引添加UNIQUE約束,強制所有值都是唯一的。
下面有個小例子,供大家查閱(官網(wǎng)直接拷貝,哈哈~):
@Entity
public class User {
@Id private Long id;
@Index(unique = true)
private String name;
}
-
<font color=#FF0000>@Unique
向數(shù)據(jù)庫列添加了一個UNIQUE約束。請注意,SQLite還會隱式地為其創(chuàng)建索引。例子如下:
@Entity
public class User {
@Id private Long id;
@Unique private String name;
}
大概的翻譯了官方的內(nèi)容,LZ英文不是很好,雖然靠著翻譯后的網(wǎng)頁進行閱讀,可有些翻譯的還不太通順,所以部分按照自己的理解寫了,大家諒解~
嗯,到現(xiàn)在,了解了一部分內(nèi)容,我們開始編碼Go Go Go ~!!!
Coding大刀闊斧的干吧
Hello,我們的第一個實體類~
我們先設(shè)定一個簡單場景,方便我們下面代碼演示,如下:
定義一個Student類,其中分別有如下幾個屬性:
- 學(xué)員id;
- 學(xué)員編號;
- 學(xué)員姓名;
- 學(xué)員性別;
- 學(xué)員成績。
接下來我們將要根據(jù)以上屬性進行GreenDao學(xué)習(xí)~
Hi ,GreenDao 實體類實現(xiàn)
package cn.hlq.greendaostudy.entity;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
/**
* Created by HLQ on 2017/6/5
*/
@Entity
public class Student {
@Id(autoincrement = true) // id自增長
private Long stuId; // 學(xué)院id
@Index(unique = true) // 唯一性
private String stuNo; // 學(xué)員編號
private String stuName; // 學(xué)員姓名
private String stuSex; // 學(xué)員性別
private String stuScore; // 學(xué)員成績
}
編譯項目,生成Dao相關(guān)文件~
<center>編輯之后,我們會發(fā)現(xiàn)我們的實體類多了一些東西,這里貼出來大家看下:
package cn.hlq.greendaostudy.entity;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Generated;
/**
* Created by HLQ on 2017/6/5
*/
@Entity
public class Student {
@Id(autoincrement = true) // id自增長
private Long stuId; // 學(xué)院id
@Index(unique = true) // 唯一性
private String stuNo; // 學(xué)員編號
private String stuName; // 學(xué)員姓名
private String stuSex; // 學(xué)員性別
private String stuScore; // 學(xué)員成績
@Generated(hash = 315497705)
public Student(Long stuId, String stuNo, String stuName, String stuSex,
String stuScore) {
this.stuId = stuId;
this.stuNo = stuNo;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuScore = stuScore;
}
@Generated(hash = 1556870573)
public Student() {
}
public Long getStuId() {
return this.stuId;
}
public void setStuId(Long stuId) {
this.stuId = stuId;
}
public String getStuNo() {
return this.stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return this.stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return this.stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getStuScore() {
return this.stuScore;
}
public void setStuScore(String stuScore) {
this.stuScore = stuScore;
}
}
這里再次闡述下,如下:
<font color=#FF0000>1. 編譯后自動生成無參,有參構(gòu)造; <font color=#FF0000>2. 編譯后自動生成getter,setter;
因為咱之前沒有指定生成Dao內(nèi)容存儲的位置,但是GreenDao有默認地址哦所以,我們?nèi)ツJ地址查看即可
<center>GreenDao中邏輯運算符
<center>這里從源碼中找了一段,很清楚,大家簡單看一下即可~
擼碼實現(xiàn)~
獲取StudentDao
也就是初始化Dao,如下:
/**
* 獲取StudentDao
*/
private void getStuDao() {
// 創(chuàng)建數(shù)據(jù)
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(self, "hlq.db", null);
daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
daoSession = daoMaster.newSession();
stuDao = daoSession.getStudentDao();
}
新增一條數(shù)據(jù)
// 新增一條數(shù)據(jù)
findViewById(R.id.id_insert).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student stu = new Student(null, "001", "賀da寶", "男孩", "50");
long end = stuDao.insert(stu);
String msg = "";
if (end > 0) {
msg = "001新增成功~";
} else {
msg = "新增失敗~";
}
stuDao.insert(new Student(null, "002", "賀er寶", "男人", "66"));
stuDao.insert(new Student(null, "003", "賀san寶", "爺兒們", "23"));
stuDao.insert(new Student(null, "004", "賀si寶", "男人", "65"));
Toast.makeText(self, "001 002 003 004新增成功~", Toast.LENGTH_SHORT).show();
}
});
新增List集合數(shù)據(jù)
// 新增List集合數(shù)據(jù)
findViewById(R.id.id_insert_list).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Student> stuList = new ArrayList<Student>();
stuList.add(new Student(null, "005", "賀利權(quán)", "小爺兒們", "43"));
stuList.add(new Student(null, "006", "賀利權(quán)", "大爺兒們", "35"));
stuList.add(new Student(null, "007", "賀利權(quán)", "老爺兒們", "99"));
stuList.add(new Student(null, "008", "賀利權(quán)", "老少爺兒們", "88"));
stuDao.insertInTx(stuList);
Toast.makeText(self, "新增成功~", Toast.LENGTH_SHORT).show();
}
});
查詢所有
findViewById(R.id.id_search_all).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Student> stuList = stuDao.queryBuilder().list();
if (stuList != null) {
String searchAllInfo = "";
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAllInfo += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore() + "\n";
}
TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_all_info);
tvSearchInfo.setText(searchAllInfo);
}
}
});
查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息
// 查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息
findViewById(R.id.id_search_assign).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchAssignInfo = "";
List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("賀da寶")).list();
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAssignInfo += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore() + "\n";
}
TextView tvSearchAssign = (TextView) findViewById(R.id.id_search_assign_info);
tvSearchAssign.setText(searchAssignInfo);
}
});
查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息并按照成績排序-降序
// 查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息并按照成績排序-降序
findViewById(R.id.id_search_assign_order_desc).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchAssignOrderDesc = "";
List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("賀利權(quán)")).orderDesc(StudentDao.Properties.StuScore).list();
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAssignOrderDesc += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore();
}
TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_assign_order_desc_info);
tvSearchOrderDesc.setText(searchAssignOrderDesc);
}
});
查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息并按照成績排序-升序
// 查詢指定數(shù)據(jù) 查詢姓名為"賀da寶"的信息并按照成績排序-升序
findViewById(R.id.id_search_assign_order_asc).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchAssignOrderDesc = "";
List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("賀利權(quán)")).orderAsc(StudentDao.Properties.StuScore).list();
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAssignOrderDesc += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore();
}
TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_assign_order_asc_info);
tvSearchOrderDesc.setText(searchAssignOrderDesc);
}
});
組合查詢數(shù)據(jù) 查詢姓名為"賀利權(quán)" 并且成績小于等于60
// 組合查詢數(shù)據(jù) 查詢姓名為"賀利權(quán)" 并且成績小于等于60
findViewById(R.id.id_search_combination).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchAssignOrderDesc = "";
QueryBuilder<Student> stuQB = stuDao.queryBuilder();
stuQB.where(StudentDao.Properties.StuName.eq("賀利權(quán)"), StudentDao.Properties.StuScore.le("60"));
List<Student> stuList = stuQB.list();
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAssignOrderDesc += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore() + "\n";
}
TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_combination_info);
tvSearchOrderDesc.setText(searchAssignOrderDesc);
}
});
查詢所有返回數(shù)據(jù) 但只返回前三條數(shù)據(jù)
// 查詢所有返回數(shù)據(jù) 但只返回前三條數(shù)據(jù)
findViewById(R.id.id_search_limit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Student> stuList = stuDao.queryBuilder().limit(3).list();
if (stuList != null) {
String searchAllInfo = "";
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAllInfo += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore() + "\n";
}
TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_limit_info);
tvSearchInfo.setText(searchAllInfo);
}
}
});
查詢所有返回數(shù)據(jù) 但只返回前三條數(shù)據(jù) 并且跳過第一條數(shù)據(jù)
// 查詢所有返回數(shù)據(jù) 但只返回前三條數(shù)據(jù) 并且跳過第一條數(shù)據(jù)
findViewById(R.id.id_search_limit_offset).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Student> stuList = stuDao.queryBuilder().limit(3).offset(1).list();
if (stuList != null) {
String searchAllInfo = "";
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
searchAllInfo += "id:" + stu.getStuId() + "編號:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性別:" + stu.getStuSex() + "成績:" + stu.getStuScore() + "\n";
}
TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_limit_offset_info);
tvSearchInfo.setText(searchAllInfo);
}
}
});
查詢所有信息總條數(shù)
// 查詢所有信息總條數(shù)
findViewById(R.id.id_search_count).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int stuSumCount = stuDao.queryBuilder().list().size();
TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_count_info);
tvSearchInfo.setText(stuSumCount + "");
}
});
刪除指定信息
// 刪除指定信息
findViewById(R.id.id_delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("賀si寶")).buildDelete().executeDeleteWithoutDetachingEntities();
Toast.makeText(self, "刪除成功~", Toast.LENGTH_SHORT).show();
// stuDao.delete(new Student()); // 刪除指定對象
// stuDao.deleteAll(); // 刪除所有
}
});
更新指定信息
// 更新指定信息
findViewById(R.id.id_update).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student student = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("賀da寶")).build().unique();
if (student != null) {
student.setStuName("I Love You");
stuDao.update(student);
}
Toast.makeText(self, "更新成功~", Toast.LENGTH_SHORT).show();
}
});
結(jié)束語以及源碼查看地址
GitHub地址
作為初學(xué)者,希望這邊文章對大家有所幫助,歡迎大家指正~
Thanks~
如果覺得寫的不錯,歡迎大家贊助LZ抽根煙Thanks