无法使用 android 中的游标获取 SQLite 数据库中的第一个条目
Unable to fetch the first entry in SQLlite Database using cursors in android
我正在尝试使用 SQLite 获取数据库 table 中的所有条目。我 运行 查询,将值存储在游标中,然后通过循环获取所有值。但是我可以访问除第一个条目之外的所有条目。这是我的代码:
mydb1=new Database_CustomTransaction(getApplicationContext());
Cursor c12 = mydb1.executeQuery("Select * from table1");
System.out.println(c12);
if(c12 == null)
{
TextView nodataView = new TextView(this);
nodataView.setId(20);
nodataView.setText("No Data here !");
nodataView.setTextSize(20);
}
else
{
if(flagValue == false)
{
c12.moveToFirst();
flagValue = true;
}
while(c12.moveToNext())
{
type=c12.getString(0);
amount = c12.getInt(1);
spentOn = c12.getString(2);
date = c12.getString(3);
listType.add(i,type);
listSpentOn.add(i,spentOn);
listAmount.add(i,amount);
listDate.add(i,date);
i++;
}
}
latesttrans2.setAdapter(new TestAdapter2(this, listType, listSpentOn,listAmount,listDate));
知道我做错了什么吗?
c12.moveToFirst();
这将移动到第一行。
while(c12.moveToNext())
第一行之后移动到下一行。
我猜第一个电话应该挂断,但只有你自己知道 flagValue
的意图。
使用 do while 而不是仅使用 while.I 认为它跳过第一个条目并直接移动到下一个条目。
我正在尝试使用 SQLite 获取数据库 table 中的所有条目。我 运行 查询,将值存储在游标中,然后通过循环获取所有值。但是我可以访问除第一个条目之外的所有条目。这是我的代码:
mydb1=new Database_CustomTransaction(getApplicationContext());
Cursor c12 = mydb1.executeQuery("Select * from table1");
System.out.println(c12);
if(c12 == null)
{
TextView nodataView = new TextView(this);
nodataView.setId(20);
nodataView.setText("No Data here !");
nodataView.setTextSize(20);
}
else
{
if(flagValue == false)
{
c12.moveToFirst();
flagValue = true;
}
while(c12.moveToNext())
{
type=c12.getString(0);
amount = c12.getInt(1);
spentOn = c12.getString(2);
date = c12.getString(3);
listType.add(i,type);
listSpentOn.add(i,spentOn);
listAmount.add(i,amount);
listDate.add(i,date);
i++;
}
}
latesttrans2.setAdapter(new TestAdapter2(this, listType, listSpentOn,listAmount,listDate));
知道我做错了什么吗?
c12.moveToFirst();
这将移动到第一行。
while(c12.moveToNext())
第一行之后移动到下一行。
我猜第一个电话应该挂断,但只有你自己知道 flagValue
的意图。
使用 do while 而不是仅使用 while.I 认为它跳过第一个条目并直接移动到下一个条目。