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

查看:178
本文介绍了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 =' Ford Prefect'; * ".下面,我已经编辑了您的问题.

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天全站免登陆