2011-08-26 62 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++; 
      } 
相关问题