如何访问android sqlite应用程序中的特定列值



我有一个应用程序,我想在我的应用程序中对数据库使用SQL查询返回的值。我想使用将这些值附加到链接到远程主机上的脚本的uri。我正在调用方法来检索值,之后我将提取它们。问题是,我在这样做的过程中面临着挑战。我懂一点php,我想在android中这样做,在php中:

....(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[] {};
    }
}

我需要帮助插入"password"列,以便它将与电子邮件一起返回。

试试这个

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

您需要将其存储在二维数组中,以行和列方式存储但是如果每次只给出一个结果

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

你应该这样修改你的设计

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

并像这样使用selectAll函数

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

最新更新