开发者

How to access particular column value in android sqlite application?

开发者 https://www.devze.com 2023-03-31 12:36 出处:网络
I have an application where I want to use the values returned from an SQL query against a database in my application.I want to use attach these values to a uri that links to a script onremote host.I a

I have an application where I want to use the values returned from an SQL query against a database in my application. I want to use attach these values to a uri that links to a script on remote host. I am doing a call to the method to retrieve the values, after which I will now extract them. The issue is that I am facing a challenge in going about this. I know a bit of php and what I want in android is done like this in php:

....(precedin开发者_如何学JAVAg code)...$row['email'];

and I can now assign a variable e.g. $email=$row['email'];

How can I do this in android?

I don't want a cursor returned, just the values of the columns as strings.

Below is my code (I will need help to retrieve the password column)

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

I need help on inserting the "password" column so that it will be returned with the email.


try this way

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

you need to store this in two dimenstion array for row wise and column wise but if this will give only one result everytime

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


You should change your design like this

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

and use the selectAll function like 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++;
             }
0

精彩评论

暂无评论...
验证码 换一张
取 消