0

我从数据库中的“threadid”列中获取值。问题是它从前一个记录/行中获取值。当我尝试获取第一条记录时,我的应用程序崩溃了,如何解决这个问题以及问题是什么?

long id;
long threadid = datasource.getthreadid(id);
Toast.makeText(getActivity(), String.valueOf(threadid), Toast.LENGTH_SHORT).show();

public long getthreadid(long id)
  {

      String ide=String.valueOf(id);
      String queryz = "SELECT " + MySQLiteHelper.COLUMN_THREADID 
                + " FROM " + MySQLiteHelper.TABLE_NAME 
                + " WHERE " + MySQLiteHelper.COLUMN_ID + "=" + ide;
      Cursor cursor = database.rawQuery(queryz, null);
      cursor.moveToFirst();
     // cursor.moveToPosition(Integer.parseInt(String.valueOf(id)));
     long threadid= cursor.getLong(cursor.getColumnIndex("threadid"));

      cursor.close();
    return threadid;

  }
4

2 回答 2

0

如果您只期待一排。那么做这样的事情可能会很有用。

long threadid;

if (cursor.moveToFirst())
{
    threadid = cursor.getLong(cursor.getColumnIndex("threadid"));
}
else
{
    // ah oh, we do not have this id!
    // do something here if you need to
}

另外,如果你要使用字符串,我建议你绑定参数。我知道您事先有很长的时间,但是对于字符串,您可以这样做。

  String queryz = "SELECT " + MySQLiteHelper.COLUMN_THREADID 
            + " FROM " + MySQLiteHelper.TABLE_NAME 
            + " WHERE " + MySQLiteHelper.COLUMN_ID + " = ?";
  Cursor cursor = database.rawQuery(queryz, new String[]{ide});
于 2013-09-12T23:21:52.287 回答
0

要获取所有记录,您首先需要移动到第一个记录,然后遍历其余记录。

if (cursor.moveToFirst()) {
    do {
        // get row values
    } while (cursor.moveToNext());
}

cursor.close();
于 2013-09-12T23:23:54.670 回答