2016-05-18 35 views
-1

使用Cursor获取表格行的某些问题。 我有3个表格:联系人,匹配和玩家(最后从nxn关系中出来)。 我想要的是检索我的所有匹配(当前用户登录)作为输入我的ID。 这里是我的阶级DatabaseHelper表扩展SQLiteOpenHelper:使用Cursor和SQLiteDatabase获取特定行

// Tabella contacts 
static final String TABLE_NAME="contacts"; 
static final String COLUMN_ID="id"; 
static final String COLUMN_NAME="name"; 
static final String COLUMN_EMAIL="email"; 
static final String COLUMN_USERNAME="username"; 
static final String COLUMN_PASSWORD="password"; 


//Tabella match 
    static final String TABLE_MATCH="match"; 
    static final String COLUMN_ID_MATCH="id_match"; 
    static final String COLUMN_NAME_MATCH="name_match"; 
    static final String COLUMN_CONTATTO_MATCH="id_contatto"; 
    static final String COLUMN_MAX_GIOCATORI="max_giocatori"; 
    static final String COLUMN_CITTA="citta"; 
    static final String COLUMN_INDIRIZZO="indirizzo"; 
    static final String COLUMN_DATA="data"; 
    static final String COLUMN_ORA="ora"; 
    static final String COLUMN_LATITUDE="latitude"; 
    static final String COLUMN_LONGITUDE="longitude"; 

    // Tabella Player 
    static final String TABLE_PLAYER="player"; 
    static final String COLUMN_ID_PLAYER="id_player"; 
    static final String COLUMN_ID_CONTACT_PLAYER="id_contact_player"; 
    static final String COLUMN_ID_MATCH_PLAYER="id_match_player"; 

...

@Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
     db.execSQL(TABLE_CREATE_MATCH); 
     db.execSQL(TABLE_CREATE_PLAYER); 

     this.db=db; 

    } 

...

public String getContact() { 
     return contact3; 
    } 

//这里是我的功能...

public Cursor getMyMatchList3(SQLiteDatabase db){ 

//  db=this.getReadableDatabase(); 
     databaseHolder=new DatabaseHolder(); 
     String contatto=databaseHolder.getContact(); 
     String query="select id_contact_player,id_match_player from "+TABLE_PLAYER; 
     String query1="select * from "+TABLE_MATCH; 
     Cursor cursor=db.rawQuery(query,null); 
     Cursor cursor1=db.rawQuery(query1, null); 
     String id_contatto_player; 
     String id_partita; 
     String nome,citta,indirizzo,data,ora; 

     if(cursor.moveToFirst()){ 
      do{ 
       id_contatto_player=cursor.getString(0); 
       if (id_contatto_player.equals(contatto)){ 
        id_partita=cursor.getString(1); 
        Log.e("ID_MATCH_PLAYER",id_partita); 
        if (cursor1.moveToFirst()){ 
         do{ 
          if (id_partita.equals(cursor1.getString(0))){ 
           id_partita=cursor1.getString(0); 
           nome=cursor1.getString(1); 
           citta=cursor1.getString(4); 
           indirizzo=cursor1.getString(5); 
           data=cursor1.getString(6); 
           ora=cursor1.getString(7); 
           Log.e("ID_PARTITA_CURSOR_1",id_partita); 
           Log.e("nome",nome); 
           Log.e("citta",citta); 
           Log.e("indirizzo",indirizzo); 
           Log.e("data",data); 
           Log.e("ora",ora); 
           return cursor1; 
          }  
         } 
         while (cursor1.moveToNext()); 
        } 
       } 
      } 
      while (cursor.moveToNext()); 
     } 
     return cursor1; 
    } 

但是用这种方式,cursor1,并没有让我回到正确的行,但同时日志输出是正确的。我能怎么做??

原因,如果我想回整个表的匹配很容易..

public Cursor getMatch(SQLiteDatabase db){ 
    String query="select * from "+TABLE_MATCH; 
    Cursor cursor=db.rawQuery(query,null); 
    return cursor; 
} 

比其他类我把它称为这样的方式......

回答

0

我没有测试它自己,但我认为问题是循环:

do {...} 
while (cursor1.moveToNext()); 

您的日志是在循环内,但在日志打印出来后,您可以:

(cursor1.moveToNext()) 

所以你再次移动你的光标。

而您将cursor1移动到下一个位置后,您将返回此光标。我想你只需要删除这个do-while循环。

+0

我删除了do {..} while(cursor1.moveToNext())但仍然一样...我没有得到正确的行 – Koalito