GreenDao 使用教程 二

2021-07-11 16:20:47 字數 2773 閱讀 4692

greendao的插入:

插入的方式有很多:

1:daosession

.getnotedao().insert(note);

//插入note 如果note指定主鍵與表中已經存在了,就會發生異常(android.database.sqlite.sqliteconstraintexception: unique constraint failed: tb_note._id (code 1555))而插入不進去

daosession.insert(note); 同上
2:

daosession

.getnotedao().insertorreplace(note);

當主鍵存在的時候會替換,所以能夠很好的執行插入操作,推薦

daosession.insertorreplace(note);同上

3:原生的sqlite
string insertsql = string.format("insert into %s (%s,%s,%s) values('%s','%s','%s')",

notedao.tablename,

notedao.properties.title.columnname,

notedao.properties.content.columnname,

notedao.properties.createtime.columnname,

note.gettitle(),

note.getcontent(),

note.getcreatetime());

daosession.getdatabase().execsql(insertsql);

4:批量插入
public void 

insertbatch(listnotes)

greendao的更新操作
1:單條更新(唯一性條件是主鍵相同)

public void update(note note) throws exception

2:批量更新

//批量更新

public void updatebatch(listnotes) throws exception

3:原生sql:
public void updatesql(string sql)
greendao 的刪除操作
1:單條刪除(唯一性是主見相同)
public void 

delete(note note)

throws

exception

2: 單條刪除 指定主鍵刪除

public void delete(long id)
3:批量刪除
public void deletebatch(listnotes)
4:批量按主鍵刪除
// 批量按主鍵刪除

public void deletebatchbykey(listpkids)

5:刪除所有
public void deleteall() throws exception
6:原始sqlite語句
daosession

.getdatabase().execsql(deletesql);

greendao 查詢操作也是最複雜的
1:單條查詢
//按主鍵查詢

public note query(long pk) throws exception

2: querybuilder 復合查詢

public listquerybuider() throws exception

public long querybuider2(long pk) throws exception

public void querybuider3() throws exception 

} finally

}

3:查詢所有:

public void queryall()

greenDao使用方法二

回看上篇文章,發現只簡述了怎麼配置greendao的生成工程,而使用的部分內容忘了寫了,罪過。在這裡補上吧。執行結束,會在你指定的路徑下生成相應的類檔案 daomaster與daosession是必定生成的,負責對資料庫表的操作,而customer和customerdao是在你新增了對應的表結構後生...

GreenDao簡明教程(查詢,Querying)

這是一篇關於greendao的簡明使用教程 其實就是官網tutorial的乙個大概的翻譯 1.基本介紹 2.實體建模 3.查詢 4.關係 查詢介面返回符合指定條件的實體物件集合.你可以使用sql組織你的查詢語句,或者採用更好的方法,使用greendao的querybuilder api.greend...

GreenDao的簡單使用

greendao相比較原生的sqlite確實方便了很多,下面我們來一起 greendao的簡單的使用方法。我們先來了解一下註解 基礎屬性註解 索引註解 實體 entity註解 關係註解 第一步要配置環境 buildscript dependencies dependencies新建實體類 entit...