Android sqlite数据库读取问题

问题遇到的现象和发生背景

在实现读取写好的sqlite数据表并显示到AlertDialog对话框时读取错误

遇到的现象和发生背景,请写出第一个错误信息

(获取两个EditText中用户输入的数据,并且根据输入的数据去查询)

img

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%

用户输入的部分以及查询的部分如下

Button chaxunneirong = findViewById(R.id.chaxun_button);
        EditText editText1 = findViewById(R.id.chaxunkuang_1);
        EditText editText2 = findViewById(R.id.chengshikuang_1);
        chaxunneirong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String chaxundata = editText1.getText().toString();//用户内容输入获取
                String chaxundata1 = editText2.getText().toString();
                SQLiteDatabase db = dbHelper.getWritableDatabase();//获取数据库对象
                Cursor cursor = db.rawQuery("select * from fenlei where shi = ? and (mingzi like ? or leibie like ?)",new String[]{chaxundata1,"%"+chaxundata+"%","%"+chaxundata+"%"},null);
                String massage_1 = null;
                String massage_2 = null;
                String massage_3 = null;
                String massage_4 = null;
                if (cursor.moveToFirst()) {
                    do {
                        massage_1 = cursor.getString(cursor.getColumnIndexOrThrow("shi"));
                        massage_2 = cursor.getString(cursor.getColumnIndexOrThrow("leibie"));
                        massage_3 = cursor.getString(cursor.getColumnIndexOrThrow("mingzi"));
                        massage_4 = cursor.getString(cursor.getColumnIndexOrThrow("caizhi"));
                    } while (cursor.moveToNext());
                }
                cursor.close();
                //将获取的内容显示到对话框
                AlertDialog alertDialog1 = new AlertDialog.Builder(chaxun.this)
                        .setTitle(chaxundata)//标题
                        .setMessage(massage_1 + " " + massage_2 + " " + massage_3 + " " + massage_4)//内容
                        .setIcon(R.mipmap.ic_launcher)//图标
                        .create();
                alertDialog1.show();
            }

运行结果及详细报错内容

使用sqlite expert查询时没有问题

img

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

下面是已经写好的数据

img

尝试过使用query以及rawQuery两种方法,sqlite expert对数据库操作没有任何问题,但Android studio这查询时就出现问题


new String[]{chaxundata1,"%"+chaxundata+"%","%"+chaxundata+"%"}

你最好把这个打印出来看看,是不是合法的数据库查询语句呢?

  1. 没有错误,那个是正常的显示,因为你的结果集没有数据
  2. 请确认你的sqlite有数据是“天津”和“纸”?
  3. 你的sqlite expert写的sql和你android代码的sql也不一样,一个是shi=""一个是shi like ""

android读取sqlite数据库的数据并用listview显示
如有帮助望采纳
https://blog.csdn.net/x15037308498/article/details/79458655

是SQL语句问题,最后成功代码如下

String sql_str = "select * from ffenlei where mingzi like '%"+chaxundata+ "%' and shi='"+chaxundata1+"'";
                Cursor cursor = db.rawQuery(sql_str+"",null);

感谢@不会写代码的猴子