【问题标题】:How to get name from database ?如何从数据库中获取名称?
【发布时间】:2014-03-29 02:36:09
【问题描述】:

我想从数据库中获取名称并在 textview 中显示。当我的应用程序已经安装时它工作正常,但是当应用程序被卸载并再次安装时,它会给出IndexOutOfBoundException。

这是我的代码:

if(c.getString(0)==null){
    TextView w1=(TextView)findViewById(R.id.textVi1);
    w1.setText("Welcome");
} else {
    TextView w1=(TextView)findViewById(R.id.textVi1);
    w1.setText("Welcome"+" "+c.getString(0)+",");
}

卸载重装应用时如何获取数据?

【问题讨论】:

  • 也许你的cursor 是空的,所以你不能在一个空列表中拉出getString(0)(获取第一个元素)。
  • 可能游标为空,无法判断。发布您的代码和堆栈跟踪。
  • 那么如何克服这个问题..
  • IndexOutOfBoundException at 0th location simple 意味着您获取的结果为空,或者可能是您的光标没有移动到 first 。自己检查一下,或者在这里发布您的代码,我们会尽力提供帮助。

标签: android sqlite indexoutofboundsexception


【解决方案1】:

在游标上调用getString()之前,确保它指向一个有效的行:

if (c.moveToFirst()) {
  // use c.getString(0)
} else {
  // don't access cursor, no data
}

【讨论】:

    【解决方案2】:

    试试这个。它可能会有所帮助。

    public String getResult(String id)
    {
        String name = null;
        try
        {
            Cursor c = null;
            c = db.rawQuery("select name from person where id=" + "\""+id+"\"", null);
            c.moveToFirst();
            name = c.getString(c.getColumnIndex("name"));
            c.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return name; 
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 2011-06-04
      • 2012-03-08
      • 1970-01-01
      相关资源
      最近更新 更多