SQLite 查询异常 Android Studio 语法错误代码 1 [英] SQLite Query Exception Android Studio Syntax Error code 1

查看:28
本文介绍了SQLite 查询异常 Android Studio 语法错误代码 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图为我的 android 应用程序构建一个搜索方法,但我一直遇到这个错误

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

这是我运行查询的方法

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

    return res;
}

并且字符串 uName 来自这个方法

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;
    }

}

谁能帮助我解决这个错误?一切似乎都很好,我只是不明白为什么它停在姓氏上.但是用户名的格式正确,就像Ford Prefect"

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"

推荐答案

传递值时应该有单引号.即,您的查询应该以这种方式传递 "*SELECT * FROM Person WHERE name = '福特级长';*".下面我编辑了您的问题.

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;
}

这篇关于SQLite 查询异常 Android Studio 语法错误代码 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆