【问题标题】:Fetch data from sqlite android从 sqlite android 获取数据
【发布时间】:2016-02-28 15:43:06
【问题描述】:

我有下表

private static final String DB_TABLE = "create table user (id integer primary key autoincrement, " 
                                            + "username text not null, password text not null, tel text not null, info text not null, job text);";

如何使用游标获取数据并将其转换为字符串?因为我正在获取 null 我应该设置获取吗? 我必须通过用户名和密码获取工作和信息?

Cursor theUser = dbHelper.fetchUser(thisUsername, thisPassword);
   if (theUser != null) {

// How here also fetch a info and job in String?

            startManagingCursor(theUser);
            if (theUser.getCount() > 0) {
                //saveLoggedInUId(theUser.getLong(theUser.getColumnIndex(DatabaseAdapter.COL_ID)), thisUsername, thePassword.getText().toString());
                stopManagingCursor(theUser);
                theUser.close();
               // String text = dbHelper.getYourData();
                Intent i = new Intent(v.getContext(), InfoActivity.class);
                startActivity(i);
            }

            //Returns appropriate message if no match is made
            else {
                Toast.makeText(getApplicationContext(),
                        "You have entered an incorrect username or password.",
                        Toast.LENGTH_SHORT).show();
               // saveLoggedInUId(0, "", "");
            }
            stopManagingCursor(theUser);
            theUser.close();
        }

        else {
            Toast.makeText(getApplicationContext(),
                    "Database query error",
                    Toast.LENGTH_SHORT).show();
        }
    }

private static final String LOGIN_TABLE = "user";
    //Table unique id
    public static final String COL_ID = "id";
    //Table username and password columns 
    public static final String COL_USERNAME = "username";
    public static final String COL_PASSWORD = "password";
    private static final String KEY_PHONE = "tel";
    private static final String INFO = "info";
    private static final String JOB = "info";

public Cursor fetchUser(String username, String password) {
    Cursor myCursor = database.query(LOGIN_TABLE, 
            new String[] { COL_ID, COL_USERNAME, COL_PASSWORD }, 
            COL_USERNAME + "='" + username + "' AND " + 
            COL_PASSWORD + "='" + password + "'", null, null, null, null);

    if (myCursor != null) {
        myCursor.moveToFirst();
    }
    return myCursor;
}

提前谢谢你!!!

更新:

String name = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.COL_USERNAME));
String phone = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.KEY_PHONE));
String tel = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.KEY_PHONE));
String info = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.INFO));
String job = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.JOB));
String id = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.COL_ID));

我该如何写这个请求:

public Cursor fetchAllUsers() {
    return database.query(LOGIN_TABLE, new String[] { COL_ID, COL_USERNAME, 
            COL_PASSWORD }, null, null, null, null, null);
}

【问题讨论】:

  • 光标在logcat中给了我--- aandroid.database.sqlite.SQLiteCursor@52823ba4
  • 请包含完整的 logcat。该评论看起来像您打印了一个 Cursor 对象
  • 另外,您的 INFOJOB 字符串看起来是一样的

标签: android sqlite


【解决方案1】:

要在各个数据行之间移动,您可以使用moveToFirst()moveToNext() 方法。 isAfterLast() 方法允许检查是否已到达查询结果的末尾。

Cursor 提供类型化的get*() 方法,例如getLong(columnIndex)getString(columnIndex) 访问结果当前位置的列数据。 “columnIndex”是您正在访问的列的编号。

Cursor 还提供了getColumnIndexOrThrow(String) 方法,该方法允许获取表的列名的列索引。

当您完成游标时,需要使用close() 方法调用来关闭它。


您的代码应该类似于这样来获取信息列,但请注意在您的问题中 JOB 和 INFO 列具有相同的字符串,因此它们将返回相同的列索引,直到您修复该问题

String info = theUser.getString(theUser.getColumnIndex(DatabaseAdapter.INFO))

【讨论】:

  • 这意味着所有数据都带有Cursor,我不必制作其他方法
  • 您已经创建了一个返回游标的方法,所以不,您不必创建其他方法,除非您愿意。
  • 谢谢 终于搞明白了!!!但这是获得它的唯一方法?我更新了 questin
  • 我不确定你在问什么。如果您还有其他问题,欢迎您再发一个帖子,我相信有人会愿意提供帮助。您已经在这里接受了答案。
  • 如何使用该光标获取所有数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2016-05-08
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多