Frage

I have a page which can retrieve user data from database but after whole day of trying, I am only able to get the table column name but not the value inside.

this is my code to create database

 public static final String LASTLOGIN = "lastuser"; 
 public static final String USER_ID = "suser_id";
 public static final String USER_NAME = "suser_name";
 public static final String USER_PASSWORD = "spassword";
 public static final String PRIME_ID = "id";

 private static final String TABLE_USER = 
         "create table "+ LASTLOGIN+" ("
        +PRIME_ID+" integer primary key autoincrement, "
        + USER_ID + " text, "
        + USER_NAME +" text, "
        +USER_PASSWORD+" text); ";

and here is the function implemented to get user data

public Cursor getuser()
{
    String[] columns = new String[]{PRIME_ID, USER_NAME, USER_PASSWORD};
    Cursor cursor = sqLiteDatabase.query(
           LASTLOGIN, columns, null, null, null, null, PRIME_ID +" DESC");
    Log.d("TAG", columns[1]);
    return cursor;
}

and here is my code to display the result

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();

cursor = mySQLiteAdapter.getuser();
String[] resultvalue = new String{
         SQLiteAdapter.PRIME_ID,SQLiteAdapter.USER_NAME, SQLiteAdapter.USER_PASSWORD};
Toast.makeText(this, resultvalue[0]+resultvalue[1], Toast.LENGTH_LONG).show();

and the toast result only show the column name but not the value inside, is there any mistake i made? and I want to set limit to 1, but where to set it?

Thanks for helping me

War es hilfreich?

Lösung

the way you try reading the values is completly wrong. you create an array

String[] resultvalue = new String[]{
                       SQLiteAdapter.PRIME_ID,
                       SQLiteAdapter.USER_NAME,
                       SQLiteAdapter.USER_PASSWORD};

after that you read the values 0 and 1 from this array. Your toast works absolutly correctly becouse inside this array you define the column names!

If you want to show the values from your query do it this way:

    while(cursor.moveToNext()){
            Integer str1 = str 1 + cursor.getInteger(1);
            String str2 =str2 + cursor.getString(2);
       Toast.makeText(this, str1 + str2, Toast.LENGTH_LONG).show();
    }

or a better way receiving the correct index:

cursor.getInteger( cursor.getColumnIndex(SQLiteAdapter.PRIME_ID) );
cursor.getString( cursor.getColumnIndex(SQLiteAdapter.USER_NAME) );

Andere Tipps

Please note when retrieving data from a database, you store it in a Cursor in the memory and hence can only access it using that particular Cursor object, which you have used in the following line of code.

Cursor cursor = mySQLiteAdapter.getuser();

The Following line retrieves the column names and not the values.

String[] resultvalue = new String[]{SQLiteAdapter.PRIME_ID,SQLiteAdapter.USER_NAME, SQLiteAdapter.USER_PASSWORD};

So the following is doing what you have asked it to do, retrieve column names not values

Toast.makeText(this, resultvalue[0]+resultvalue[1], Toast.LENGTH_LONG).show();

You need something like following:

if(cursor.getCount() != 0)
{
        while(cursor.moveToNext())
        {

                    resultvalue [0] = csr.getString(0);
                    resultvalue [1] = csr.getString(1);
                    //....
        }
}

Hope this helps

here is my solution:

final String TABLE_NAME = "table_name";
String selectQuery = "SELECT Column FROM "+TABLE_NAME+" WHERE column='"+some_value+"'";
SQLiteDatabase db  = this.openDatabase();
Cursor cursor      = db.rawQuery(selectQuery, null);
String[] data      = new String[cursor.getCount()];;
int i = 0;

if (cursor.moveToFirst()) {
    do {
        i=Integer.parseInt(cursor.getString(cursor.getColumnIndex("value")));
    } while (cursor.moveToNext());
}
cursor.close();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top