SQLite Query Exception Android Studio Syntax Error code 1

夙愿已清 提交于 2019-12-20 07:49:00

问题


So im trying to build a search method for my android app and i keep running into this error

android.database.sqlite.SQLiteException: near "Prefect": syntax error (code 1): 
while compiling: SELECT * FROM Person WHERE name = Ford Prefect

This is the method i have that is running the query

public Cursor findUser(String uName)
{
    Cursor res = myDatabase.query("Person WHERE name = "+uName+"",
                                         null,null,null,null,null,null);

    return res;
}

And the String uName comes from this method

public void onClick(View v)
{
    EditText uNameField = (EditText)findViewById(R.id.editText);
    String userName=uNameField.getText().toString();

    switch (v.getId())
    {
        case R.id.button:
            Intent myIntent = new Intent(FindUser.this,
                    Results.class);
            myIntent.putExtra("uName", userName);
            startActivity(myIntent);
            break;
    }

}

Can anyone help with my im getting this error?? everything seems to be fine i just cant understand why it stops on the surname. But the usernames are correctly formatted like so "Ford Prefect"


回答1:


There should be single quote while passing the values .i.e., your query should be passed in this way "*SELECT * FROM Person WHERE name = 'Ford Prefect';*". Below I have edited your question.

public Cursor findUser(String uName) {
    Cursor res = myDatabase.query("Person WHERE name = '"+uName+"';",
                                         null,null,null,null,null,null);
    return res;
}


来源:https://stackoverflow.com/questions/36374433/sqlite-query-exception-android-studio-syntax-error-code-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!