gpt4 book ai didi

android - 从数据库中的列获取值

转载 作者:行者123 更新时间:2023-11-30 01:18:56 24 4
gpt4 key购买 nike

我从数据库中的“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;

}

最佳答案

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

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
}

此外,如果您要使用字符串,我建议您绑定(bind)参数。我知道你已经提前很久了,但是对于字符串你可以这样做。

  String queryz = "SELECT " + MySQLiteHelper.COLUMN_THREADID 
+ " FROM " + MySQLiteHelper.TABLE_NAME
+ " WHERE " + MySQLiteHelper.COLUMN_ID + " = ?";
Cursor cursor = database.rawQuery(queryz, new String[]{ide});

关于android - 从数据库中的列获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com