2011-08-26 63 views
0

我有一個應用程序,我想在我的應用程序中使用SQL查詢對數據庫返回的值。我想用這些值附加到一個鏈接到遠程主機上的腳本的URI。我正在調用方法來檢索值,之後我將提取它們。問題在於,我正面臨着這樣的挑戰。我知道一點的PHP的,我想在android系統是什麼在PHP這樣做:如何訪問android sqlite應用程序中的特定列值?

....(preceding code)...$row['email']; 

,我現在可以指派一個變量如$email=$row['email'];

我該如何在android中做到這一點?

我不希望遊標返回,只是列的值作爲字符串。

下面是我的代碼(我需要幫助找回密碼欄)

public String[] selectAll() 
{ 
    Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null); 
    if(cursor.getCount() >0) 
    { 
     String[] str = new String[cursor.getCount()]; 
     int i = 0; 

     while (cursor.moveToNext()) 
     { 
      str[i] = cursor.getString(cursor.getColumnIndex("email")); 

      i++; 
     } 
     return str; 
    } 
    else 
    { 
     return new String[] {}; 
    } 
} 

我需要插入「密碼」欄,使其與電子郵件被退回的幫助。

回答

0

嘗試這種方式

 String[][] str = new String[cursor.getCount()][cursor.getColumnCount()]; 

     while (cursor.moveToNext()) 
     { 
      for(int j=0;j<cursor.getColumnCount();j++) 
       str[i][j] = cursor.getString(j); /// may be this column start with the 1 not with the 0 

      i++; 
     } 

需要兩個dimenstion陣列存儲此爲行方向和列明智 但如果這會給結果只有一個,每次

 String[] str = new String[cursor.getColumnCount()]; // here you have pass the total column value 
     while (cursor.moveToNext()) 
     { 
      str[0] = cursor.getString(cursor.getColumnIndex("email")); 
      str[1] = cursor.getString(cursor.getColumnIndex("password")); 
     } 
0

你應該改變你像這樣的設計

public Cursor selectAll() {  
     Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null); 
return cursor;   
    } 

並使用selectAll函數l ike this

Cursor cursor=selectAll(); 
String[] mail = new String[cursor.getCount()]; 
String[] pass = new String[cursor.getCount()]; 
int i=0; 
while (cursor.moveToNext()) 
      { 
       mail[i] = cursor.getString(cursor.getColumnIndex("email")); 
       pass[i] = cursor.getString(cursor.getColumnIndex("password")); 

       i++; 
      } 
相關問題