2016-11-05 6 views
-2

私はandroid.butのsqlliteデータベースの情報に行きたいと思っています。 私のコードは次のとおりです。複数のクエリをアンドロイドで使用する

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を調整することです。

すでに2つの異なるCusorsに対して情報を取得するためにrawQueryを使用しています。これは動作しません。単一カーソルを使用して必要な情報を取得するための

ソリューションは次のようになります。

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; 
} 

(上記の例のコードがテストされていません!)

希望します。

Additonalのように、SQLiteのチュートリアルのように見ることができます http://www.tutorialspoint.com/sqlite/ sqliteに精通して取得する。 Anroids-のSQLiteとカーソルオブジェクトで可能であるものをより多くの情報を得るために https://www.tutorialspoint.com/android/android_sqlite_database.htm

はまたのようなアンドロイドSQLiteのチュートリアルにで見ています。

+0

ありがとうございました。データベースからの情報でいくつかのlistviewを使用したいと思います。私はいくつかのクラスで使用するいくつかのクエリを取得したい場合は、私を助けることができますか? –

関連する問題