【问题标题】:How to write a sqlite query to get specific data?如何编写一个sqlite查询来获取特定数据?
【发布时间】:2019-04-03 08:38:01
【问题描述】:

我想获取用户 ID 用于登录的学生的名字、中间名和姓氏。我已经编写了这段特殊的代码,但它停止了我的应用程序。

我也使用过 database.query() 和 .rawquery() 这两种方法。

    Cursor studentData(String userId) {
        SQLiteDatabase db = getWritableDatabase();
        Cursor cursor = db.query(studentTable, new String[] { "First_Name", "Middle_Name", "Last_Name"}, "User_ID=?", new String[] { userId }, null, null, null, null);
//        Cursor cursor = db.rawQuery("select First_Name, Middle_Name, Last_Name from Student_Table where User_ID =?", new String[]{userId});
        String data = cursor.getString(cursor.getColumnIndex("First_Name"));
        db.close();
        return cursor;
    }

我应该在字符串中获取全名。

【问题讨论】:

  • 您能分享一下您的应用程序停止时显示的错误吗?它可以揭示你的代码(或其他东西)出错的地方

标签: android android-sqlite android-cursor


【解决方案1】:

你有很多问题。

  1. 尝试使用String data = cursor.getString(cursor.getColumnIndex("First_Name"));, 将导致错误,因为您没有将光标 移动 在第一行之前之外,并且尝试访问行 -1 将导致异常(您可能遇到的问题)。

    • 你可以使用各种招式???方法例如moveToFirst、moveToNext(最常见的2个)、moveToLast、moveToPosition。
    • 大部分光标移动???如果可以进行移动,方法返回 true,否则返回 false。
  2. 您不能关闭数据库然后访问光标(如果上述问题得到解决,就会发生这种情况)

    • 光标缓冲行,然后仅在需要时缓冲。

    • 即光标是从查询方法(或 rawQuery)返回的 第一行之前 (-1) 的位置时,仅在尝试移动时通过Cursor 填充CursorWindow(缓冲区)(包括getCount())并获得实际数据。所以数据库必须是打开的。

如果你想要一个字符串,全名,那么你可以使用:-

String studentData(String userId) { //<<<<<<<<<< returns the string rather than the Cursor
    SQLiteDatabase db = getWritableDatabase();
    String rv = "NO NAME FOUND"; //<<<<<<<<<< value returned if no row is located
    Cursor cursor = db.query(studentTable, new String[] { "First_Name", "Middle_Name", "Last_Name"}, "User_ID=?", new String[] { userId }, null, null, null, null);
    if (cursor.modeToFirst()) {
        String rv = 
            cursor.getString(cursor.getColumnIndex("First_Name")) +
            " " +
            cursor.getString(cursor.getColumnIndex("Middle_Name")) +
            " " +
            cursor.getString(cursor.getColumnIndex("Last_Name"));
    }
    cursor.close(); //<<<<<<<<<< should close all cursors when done with them
    db.close(); //<<<<<<<<<< not required but would result in an exception if returning a Cursor
    return rv;
}

或者:-

String studentData(String userId) { //<<<<<<<<<< returns the string rather than the Cursor
    SQLiteDatabase db = getWritableDatabase();
    String rv = "NO NAME FOUND"; //<<<<<<<<<< value returned if no row is located
    Cursor cursor = db.query(studentTable, new String[] { "First_Name"||" "||"Middle_Name"||" "||"Last_Name" AS fullname}, "User_ID=?", new String[] { userId }, null, null, null, null);
    if (cursor.modeToFirst()) {
        String rv = 
            cursor.getString(cursor.getColumnIndex("fullname"));
    }
    cursor.close(); //<<<<<<<<<< should close all cursors when done with them
    db.close(); //<<<<<<<<<< not required but would result in an exception if returning a Cursor
    return rv;
}
  • 基础查询为SELECT First_Name||" "||Middle_Name||" "||LastName AS fullname FROM student_table;,因此您将名称连接为查询的一部分,该查询仅返回一个动态创建的名为fullname 的列。

【讨论】:

  • 非常感谢。你已经清除了我对从游标获取数据的所有疑虑。
猜你喜欢
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
相关资源
最近更新 更多