从数据库中获得的数据的Andr​​oid [英] Get data from database Android

查看:104
本文介绍了从数据库中获得的数据的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,它可以从数据库中检索用户数据
但试图整天之后,我只能够得到表的列名而不是里面的值。

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.

这是我的code创建数据库

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

,这里是实现获取用户数据的功能

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

这里是我的code来显示结果

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

和敬酒结果只显示列名而不是里面的价值,没有任何错误,我做?我想限制设置为1,但在那里设置呢?

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?

感谢您帮助我

推荐答案

您尝试阅读的价值观是完全地错误的方式。
创建数组

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

后您从该阵列读出的值0和1。
你的面包是绝对工作正常becouse这个数组中定义的列名!

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

这篇关于从数据库中获得的数据的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆