2013-10-11 152 views
2

我在我的項目中使用了GreenDao,並且將我的服務器模型映射到android設備上做得很好。我一直在努力的一段時間是,dao.update和/或dao.updateInTx方法不更新數據庫中的任何行。GreenDao更新不起作用

我做什麼:

/** 
* The first approach -> All in one runnable 
* 
*/ 

// list definitions 
ArrayList<Country> countryList; 
ArrayList<Country> countryListDetail; 

final DaoSession daoSession = daoMaster.newSession(); 

// execute everything in runnable 
// in order to optimize insert time 
daoSession.runInTx(new Runnable() { 

    @Override 
    public void run() { 
     CountryDao countryDao = new CaountryDao(); 


       // delete all records first 
       countryDao.deleteAll(); 

       // insert countries with less data 
       size = countryList.size(); 
       for (int i=0; i < size; i++) { 
        countryDao.insert(countryList.get(i)); 
       } 

       // update countries with more data 
       // ID's of the Objects in countryListDetail match those from 
       // the countryList, so the primary key mathces 
       size = countryListDetail.size(); 
       for (int i=0; i < size; i++) { 
        countryDao.update(countryListDetail.get(i)); 
       } 

     } 
} 

/** 
* The second approach -> updateInTx() 
* 
*/ 

// list definitions 
ArrayList<Country> countryList; 
ArrayList<Country> countryListDetail;    

// insert & update logic 
final DaoSession daoSession = daoMaster.newSession();         
CountryDao countryDao = new CaountryDao(); 

countryDao.insertInTx(countryList); 
countryDao.updateInTx(countryListDetail); 

在這兩種情況下,當我從設備拉數據庫,並檢查它,國家表只有基礎插入數據,但沒有詳細的數據應該來自更新聲明。在調試時,GreenDao邏輯似乎執行updateInsideSynchronized()方法,並且也調用stmt.execute()。

有人知道我可能做錯了什麼嗎?

+0

因爲我不知道該怎麼'countryList'和'countryListDetail'正在填充這個做只是一個建議。爲了更新工作,請檢查countryListDetail中的每個'Country'對象的主鍵是否與'countryList'中的'Country'對象的主鍵匹配。 – MDrabic

+1

嗨@MDrabic,謝謝你的建議。之前已經檢查過幾次,你說得對,它是問題的常見來源。不過,主鍵在兩個ArrayLists中絕對匹配。任何其他建議?我沒有找到任何方法來輸出正在執行的查詢,以便調試由greenDao執行的原始SQL。任何想法如何做到這一點? – Devdroid

回答

1

可能您的countyListDetail不包含Country-對象,已使用greendao查詢。因此,對象不知道數據庫,因爲沒有提及DaoSession。此更新僅適用於已從數據庫插入或查詢的對象。

如果countryList中的數據包含在countryListDetail的數據中,我會使用insertOrReplace

否則,您必須在插入之前或更新之前合併您的Country對象。

2

請啓用該檢查

dao.queryBuilder().LOG_VALUES=true; 
dao.queryBuilder().LOG_SQL=true; 

,並確保countryList和countryListDetail具有相同的PrimaryKey當你更新