【问题标题】:Querying the database with a variable passed as an argument in SQLite android [duplicate]在SQLite android中使用作为参数传递的变量查询数据库[重复]
【发布时间】:2016-04-07 13:11:46
【问题描述】:

我正在创建一个应用程序,用户可以在其中使用用户通过编辑文本框提供的输入来搜索数据库。我将它们作为字符串参数传递给数据库文件进行查询,我收到错误。附加代码

这是按钮点击监听器,自动命名为 Autotextview

 b1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {

 MyDatabase m =new MyDatabase(getActivity());
 Cursor c=m.searchname(autoname.getText().toString());

 simpleCursorAdapter = new SimpleCursorAdapter(getActivity(),     R.layout.custom_layout_row, c,
                    new String[]{"_id", "name", "phone", "email", "subject"},
                    new int[]{R.id.rownumber, R.id.rowname, R.id.rowphone, R.id.rowmail, R.id.rowsubject}, 0);
            listView.setAdapter(simpleCursorAdapter);


        }
    });

这是Mydatabase.java(数据库文件)中的代码

    public Cursor searchname(String argument) {
    Cursor cursor = null;
    cursor = s.query("student", null, "name=?", new String[]{argument}, null, null, null);


    return cursor;
    }

我在查询字符串中遇到错误

如果我运行原始查询

Cursor cursor = db.rawQuery("select * from student where name =?", new String[]{argument});

我收到了这个错误:

java.lang.NullPointerException:尝试调用虚拟方法 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' 在空对象引用上 com.techpalle.miniproject.MyDatabase.searchname(MyDatabase.java:107) 在 com.techpalle.miniproject.SearchFragment$2.onClick(SearchFragment.java:71)

【问题讨论】:

  • 你可以尝试运行原始查询 Cursor cursor = db.rawQuery("select * from student where name =?", new String[]{argument});
  • 也报错。
  • java.lang.NullPointerException: 尝试调用虚拟方法 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])'在 com.techpalle.miniproject.SearchFragment$2.onClick(SearchFragment.java:71) 的 com.techpalle.miniproject.MyDatabase.searchname(MyDatabase.java:107) 的空对象引用上
  • 你能把错误贴在这里吗
  • 写 SQLiteDatabase s = this.getWritableDatabase(); Cursor cursor = s.rawQuery("select * from student where name =?", new String[]{argument});

标签: android database sqlite parameter-passing


【解决方案1】:

错误表明您正在尝试在空对象上运行 rawQuery :) 这意味着您的 s 为空 :) 您应该先获得一个可写的数据库引用,然后才能对其执行任何查询:)

所以你可以试试这个:)

public Cursor searchname(String argument) {
     Cursor cursor = null;
     SQLiteDatabase s = this.getWritableDatabase(); 
     Cursor cursor = s.rawQuery("select * from student where name =?", new String[]{argument});
     return cursor;
}

如果您还有问题,请让我知道 :) 快乐的编码伙伴 :)

【讨论】:

  • 非常感谢。它工作得很好。
  • @Phillen 永远欢迎朋友 :) 很高兴我能帮上忙 :)
  • this.getWritableDatabase();也遇到了麻烦,所以使用了数据库帮助器对象而不是这个......并且它起作用了。再次感谢你
  • 你的 Mydatabase.java 不是扩展了 SQLiteOpenHelper 哥们???我通常用 SQLiteOpenHelper 扩展我的数据库文件,所以我写了这个:) this.getWritableDatabase(); 有什么问题?很想知道:)
  • getWritableDatabase 是红色的,点击它显示 --cannot resolve method getwritabledatabase -- 我是初学者,所以我不深入了解。我没有从 SQLLiteOpenHelper 扩展。我已经导入了。我会扩展并让你知道结果。
猜你喜欢
  • 2021-01-28
  • 2018-08-30
  • 1970-01-01
  • 2019-01-30
  • 2015-06-21
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多