【问题标题】:Sqlite cursor doesn't return data [duplicate]Sqlite游标不返回数据[重复]
【发布时间】:2018-11-28 11:17:57
【问题描述】:

游标携带数据有问题,调试,我知道了"java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed."

但是我仍然无法调整光标问题,我知道在光标返回数据之前不应该关闭连接,但我不知道如何调整它。

我想从 getEmpData 方法中获取一些信息,然后传递它。

我的方法:

public Cursor getEmpData(Integer employeeID)
        {
            EmpDept = getReadableDatabase();
            Integer[] empRow = {employeeID};

            Cursor c = EmpDept.rawQuery("Select name, Title, phone, email from Employee where EmpID like ?", new String[]{employeeID.toString()});
            if (c != null)
            {
                c.moveToFirst();
            }
            EmpDept.close();
            return c;

        }

列表视图部分:

单击名称时,它应该将数据移动到新活动。

       namelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id)
              {
                  String name = namelist.getItemAtPosition(position).toString();
                   //Getting ID of emp,dept
                  Cursor empID = Emp.getEmpID(name);
                  Cursor DepID = Emp.getDeptID(name);
                  int eID = empID.getInt(0);
                  int dID = DepID.getInt(0);



                  Intent intent = new Intent(MainActivity.this, empDetails.class);

                  intent.putExtra("empName", Emp.getEmpData(eID).toString());
                  intent.putExtra("empTitle",Emp.getEmpData(eID).toString());
                  intent.putExtra("empPhone",Emp.getEmpData(eID).toString());
                  intent.putExtra("empEmail",Emp.getEmpData(eID).toString());

                  intent.putExtra("empDept",Emp.getDeptName(dID).toString());
                  startActivity(intent);
              }
          });

【问题讨论】:

  • 请停止转发此问题。正如我上次提到的,只需使用您拥有的任何新信息、您尝试过的任何新代码或任何已发布答案不起作用的解释来编辑您的原始帖子,就会将它推到活动队列的顶部。

标签: java android sqlite cursor


【解决方案1】:

使用此代码:

public Employee getEmpData(Integer employeeID)
        {
            EmpDept = getReadableDatabase();
            Integer[] empRow = {employeeID};

            Cursor c = EmpDept.rawQuery("Select name, Title, phone, email, dept from Employee where EmpID like ?", new String[]{employeeID.toString()});
            Employee employee = new Employee();
            if (c != null && c.getCount() > 0 && c.moveToFirst())
            {
                employee.setName(c.getString(0));
                employee.setTitle(c.getString(1));
                employee.setPhone(c.getString(2));
                employee.setEmail(c.getString(3));
                employee.setDept(c.getString(4));
            }
            c.close();
            return employee;

        }

你的清单部分:

    Intent intent = new Intent(MainActivity.this, empDetails.class);
    Employee employee = Emp.getEmpData(eID);
    intent.putExtra("empName", employee.getName());
    intent.putExtra("empTitle",employee.getTitle());
    intent.putExtra("empPhone",employee.getPhone());
    intent.putExtra("empEmail",employee.getEmail());
    intent.putExtra("empDept",employee.getDept());

Employee 是一个对象,包括姓名、职务、电话、电子邮件、部门及其getter 和setter。

【讨论】:

  • 为什么我不能使用它,没有员工对象部分?
  • @AspiringLilly 首先你要关闭数据库并返回它的游标,所以游标将变得不可用。其次,将光标返回到您的活动是不正确的。第三,您使用光标的方式是错误的;)
  • 那我应该如何调整它才能返回活动,为什么返回那里是不对的?我没听懂
  • @AspiringLilly 游标的内部可能包含大量资源,因此在适当处理后清理游标很重要,因为最好从游标中提取数据并将其保存到对象之后关闭光标,然后将对象返回到您的活动。
猜你喜欢
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多