将光标结果与EditText输入

时间:2017-03-31 16:14:45

标签: android

搞乱 Android Studio ,没有太多使用它,所以很多基本功能我仍然试图让我头脑发热。

我已经创建了一个本地数据库来存储用户的注册信息,让它工作没问题。有ID /名称/电子邮件/密码字段。

现在我正在尝试整理登录功能,这是我正在努力的地方,因为这是我第一次使用游标。只需要循环浏览电子邮件和密码字段,查找匹配项并将用户登录。这就是我目前在DatabaseHelperClass中所拥有的内容;

public boolean passwordOk(String Editemail, String Editpassword) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from " +  TABLE_NAME + " where " + COL_3 + "="+ Editemail + " and " + COL_4 + "=" + Editpassword + "'", null );

        return (res.getCount() == 1);

    }

以下是我在MainActivity中使用它的内容;

 public void passwordOK(){

        loginB.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                   boolean isInsert = myD.passwordOk(editEmail.getText().toString(), editPassword.getText().toString());
                        if (isInsert == true)
                            Toast.makeText(MainActivity.this, "You Have Successfully Logged in", Toast.LENGTH_LONG).show();
                 else  Toast.makeText(MainActivity.this, "Oops! Try Again", Toast.LENGTH_LONG).show();



                    }
                }
        );
    }

这是它向我投掷的错误,它关闭了应用程序。

  

03-31 16:11:37.351 26018-26018 / com.example.onein.jobgosite   E / AndroidRuntime:致命异常:主要                                                                                处理:com.example.onein.jobgosite,PID:26018                                                                                android.database.sqlite.SQLiteException:无法识别的标记:“'”(代码   1):,编译时:select * from user_Info,其中EMAIL = do和   PASSWORD =卫生署”                                                                                    在   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)                                                                                    在   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)                                                                                    在   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)                                                                                    在   android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                    在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                    在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37)                                                                                    在   android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)                                                                                    在   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)                                                                                    在   android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)                                                                                    在   com.example.onein.jobgosite.DatabaseHelperClass.passwordOk(DatabaseHelperClass.java:62)                                                                                    在   com.example.onein.jobgosite.MainActivity $ 2.onClick(MainActivity.java:67)                                                                                    在android.view.View.performClick(View.java:5198)                                                                                    在android.view.View $ PerformClick.run(View.java:21147)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

所有的帮助表示赞赏,它可能是一些愚蠢的东西,并且使它可以运行起来很简单,但出于某种原因,我正在努力想象游标如何工作以及如何使它工作。谢谢!

1 个答案:

答案 0 :(得分:0)

在参数中使用此字符',如下所示:

Cursor res = db.rawQuery( "SELECT * from " + "\'"+TABLE_NAME+"\'"+ " where " + "\'"+COL_3+"\'" + "="+"\'"+ Editemail +"\'"+ " and " +"\'"+ COL_4 +"\'"+ "=" + "\'"+Editpassword +"\'"+ "", null );

但更好的做法是使用操作数。

我意识到你检查了这个:

res.getCount() == 1

将其更改为:

res != null

另外,这里有一个额外的字符“'”,应该是这样的:

Editpassword + ""