Java 语法错误(Sqlite代码1 Sqlite_错误)

Java 语法错误(Sqlite代码1 Sqlite_错误),java,database,sqlite,android-studio,Java,Database,Sqlite,Android Studio,我想查询表中的所有数据,其中Name_s=value intent 它在类中的这一行失败: Cursor data=db.rawQuery(查询,空) 我的班级: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mushroom); dbhp = new

我想查询表中的所有数据,其中Name_s=value intent

它在类中的这一行失败:
Cursor data=db.rawQuery(查询,空)

我的班级:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mushroom);

        dbhp = new DatabaseHelper(this);

        NS = getIntent().getStringExtra("name_s");

        txtName = (TextView)findViewById(R.id.textName);
        txtType = (TextView)findViewById(R.id.textType);
        txtName_s = (TextView)findViewById(R.id.textName_s);
        txtFeature = (TextView)findViewById(R.id.textFeature);
        txtProperties = (TextView)findViewById(R.id.textProperties);
        imageV = (ImageView) findViewById(R.id.imageV);

        SQLiteDatabase db = dbhp.getWritableDatabase();
        String query = "SELECT * FROM " + dbhp.TABLE_NAME + " where " + dbhp.KEY_NAME_S + " = " + NS ;
        Cursor data = db.rawQuery(query, null);
        m_name = data.getString(data.getColumnIndex(DatabaseHelper.KEY_NAME));
        m_name_s = data.getString(data.getColumnIndex(DatabaseHelper.KEY_NAME_S));
        m_type = data.getString(data.getColumnIndex(DatabaseHelper.KEY_TYPE));
        m_feature = data.getString(data.getColumnIndex(DatabaseHelper.KEY_FEATURE));
        m_properties = data.getString(data.getColumnIndex(DatabaseHelper.KEY_PROPERTIES));
        m_image = data.getBlob(data.getColumnIndex(DatabaseHelper.KEY_IMAGE));



        Bitmap bitmap = BitmapFactory.decodeByteArray(m_image, 0, m_image.length);

        txtName.setText(m_name);
        txtType.setText(m_type);
        txtName_s.setText(m_name_s);
        txtFeature.setText(m_feature);
        txtProperties.setText(m_properties);
        imageV.setImageBitmap(bitmap);

在SQL中,查询字符串值时,需要用引号将字符串括起来。因此,在您的情况下,代码应该是:

String query = "SELECT * FROM " + dbhp.TABLE_NAME + " where " + dbhp.KEY_NAME_S + " = '" + NS + "'";

但是为了让自己的生活更轻松,并防止将来出现与查询相关的问题,您可能需要进行研究。一篇好文章的开头是。

您应该在字符串
NS
周围使用单引号,但即使这样可以消除错误,也不是构建查询的推荐且安全的方法。
为sql语句中传递的每个参数使用
占位符,并在
rawQuery()
的第二个参数中将参数作为字符串数组传递:


通过这种方式,参数由
rawQuery()
处理,您不必连接单引号,而且代码是安全的,不会出现错误

这显然与您发布的问题无关。
String query = "SELECT * FROM " + dbhp.TABLE_NAME + " where " + dbhp.KEY_NAME_S + " = ?";
Cursor data = db.rawQuery(query, new String[] {NS});