IoTE. 2024-05-18 12:55 采纳率: 0%
浏览 5

这段代码为什么只会显示No activity found with that name


else if (id == R.id.bt_query) {
            name = et_name.getText().toString().trim();
            if (name.isEmpty()){
                tv_show.setText("Error querying the database");
                return;
            }
            db=myHelper.getReadableDatabase();
            Cursor cursor =null;
            try {
                cursor=db.query("information",null,"name=?",new String[]{name},null,null,null);
                if (cursor.moveToFirst()){
                    content=cursor.getString(cursor.getColumnIndex("content"));
                    tv_show.setText("name: " + name + "\ncontent: " + content);
                } else {
                    tv_show.setText("No activity found with that name");
                }
            } catch (SQLException e){
                // 处理查询过程中可能发生的异常
                e.printStackTrace();
                tv_show.setText("Error querying the database");
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                if (db != null && db.isOpen()) {
                    db.close();
                }
            }
        }
```java

```java


这段代码为什么只会显示No activity found with that name

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-18 12:55
    关注

    引自免费微信小程序:皆我百晓生

    在分析这段代码时,我们需要检查以下几个方面来找出为何会显示"No activity found with that name"。

    1. 数据库连接:确保已成功打开并获取到可读数据库。
    2. SQL 查询语句:确认查询条件是否正确匹配了数据库中的数据。
    3. 查询结果处理:检查当查询结果为空时的逻辑是否正确。

    首先,确保 myHelper 是一个正确的 SQLiteOpenHelper 类的实例,并且已经成功创建了一个名为 "information" 的表。接下来,我们对代码进行一些优化,简化错误处理并改进逻辑:

    else if (id == R.id.bt_query) {
        name = et_name.getText().toString().trim();
        if (name.isEmpty()){
            tv_show.setText("Error: Activity name is empty.");
            return;
        }
    
        db = myHelper.getReadableDatabase();
    
        Cursor cursor = db.query(
            "information",
            new String[]{"name", "content"},
            "name = ?",
            new String[]{name},
            null,
            null,
            null
        );
    
        if (cursor != null) {
            int count = cursor.getCount();
            if (count > 0) {
                cursor.moveToFirst();
                String content = cursor.getString(cursor.getColumnIndex("content"));
                tv_show.setText("Name: " + name + "\nContent: " + content);
            } else {
                tv_show.setText("No activity found with that name.");
            }
            cursor.close(); // 关闭游标
        }
    
        // 不需要额外判断数据库是否打开,因为 close() 方法会自动关闭未关闭的数据库连接
        db.close();
    }
    

    在这个版本中:

    • 我们将要查询的列名明确指定为 "name" 和 "content",这样可以避免在获取 "content" 字段时可能出现的问题。
    • 使用 getCount() 检查查询结果的数量,如果大于 0,则获取并显示数据。否则,显示 "No activity found with that name."。
    • finally 块中的关闭操作移到 if (cursor != null) 判断内,确保无论是否发生异常,都会正确关闭游标。

    如果你仍然遇到问题,那么可能是数据库中的数据不匹配输入的 name,或者 "information" 表不存在、没有被正确初始化,或者 myHelper 对象有问题。建议你检查这些方面,或者提供更多的上下文信息以便进一步排查。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月18日

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?