2016-11-05 17 views
-2

我想去android中的sqllite數據庫的一些信息,但我不知道什麼。 我的代碼是:在Android中使用多個查詢在遊標中

public Cursor getData() { 
 
     String myPath = DB_PATH + DB_NAME; 
 
     db = SQLiteDatabase.openDatabase(myPath, null, 
 
       SQLiteDatabase.OPEN_READONLY); 
 
     Cursor c = db.rawQuery("SELECT text,_id from tbl_laws where parent_id = 0",null); 
 
     // Note: Master is the one table in External db. Here we trying to access the records of table from external db. 
 

 

 
     Cursor c1 = db.rawQuery("SELECT text,_id from tbl_nokte where parent_id = 0",null); 
 
     // Note: Master is the one table in External db. Here we trying to access the records of table from external db. 
 
     return c; 
 
     return c1; 
 
    }

誰能幫助我?

+0

請提供一些樣本數據和您想要的輸出。 –

回答

1

解決方案是調整您的SQL-Select-Query。

您已經使用rawQuery來獲取信息,但對於兩個不同的Cusors。這不起作用。

解決方案獲得了單光標您所需要的信息可能是:

public Cursor getData() { 
    String myPath = DB_PATH + DB_NAME; 
    db = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READONLY); 

    String query = "SELECT laws._id, laws.text," + 
          " nokt._id,nokt.text" + 
        " FROM tbl_laws AS laws, tbl_nokte AS nokt" + 
        " WHERE laws.parent_id = 0 AND nokt.parent_id = 0"; 


    Cursor c = db.rawQuery(query, null); 

    return c; 
} 

的用法是:(!上面的示例代碼沒有測試)

public List<YourObject> getDataForYourObjects(Cursor c){ 
    List<YourObject> yourObjects = new ArrayList<>(); 
    if(c != null){ 
     while(c.moveToNext(){ 
      //law results 
      long lawsID = c.getLong(0); 
      String lawsText = c.getString(1); 

      //nokt results 
      long noktID = c.getLong(2); 
      String noktText = c.getString(3); 

      YourObject yo = new YourObject(); 
      yo.addLaw(lawID, lawText); 
      yo.addNokt(noktID, noktText); 
      yourObjects.add(yo); 
     } 
     c.close(); 
    } 
    return yourObjects; 
} 

希望它有幫助。

另外,你可以看看SQLite的教程,如: http://www.tutorialspoint.com/sqlite/讓熟悉sqlite熟悉。

也有一個Android的SQLite的教程像一看: https://www.tutorialspoint.com/android/android_sqlite_database.htm 以獲取更多信息什麼是可能的Anroids,SQLite和光標對象。

+0

感謝您的幫助。但我想通過從數據庫通知使用幾個listview。我想我想要得到幾個查詢和在幾個類中使用,你能幫助我嗎? –

相關問題