Get data from database Android

大憨熊 提交于 2019-12-06 08:05:43
Stefan Beike

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

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!