问题
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