数据库查询在SQLite会议室中不起作用

时间:2018-10-29 06:26:15

标签: android sqlite android-room

我是android新手。我正在尝试学习Android,但遇到了问题。在使用SQLite时,正在执行我的插入操作。我的loadAll用户也正在执行。但是在使用参数检索用户时,该操作未执行。我在下面共享代码。在此先感谢您的帮助。

AppHome.java

package com.impresscapital.iclapp;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.impresscapital.iclapp.dbobj.User;
import com.impresscapital.iclapp.util.ToastMessage;
import com.impresscapital.iclapp.util.UserSigninAuthentication;

public class AppHome extends AppCompatActivity {

    private TextView mUsernameTextView;
    private TextView mPasswordTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_home);

        initiateUI();
    }

    private void initiateUI(){
        mUsernameTextView=findViewById(R.id.editTextUsername);
        mPasswordTextView=findViewById(R.id.editTextPassword);
    }

    public void signUpPageLoader(View view){
        Intent registrationPageIntent=new Intent(AppHome.this,RegistrationPage.class);
        startActivity(registrationPageIntent);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void signInUser(View view){
        try {
            String username=mUsernameTextView.getText().toString();
            String password=mPasswordTextView.getText().toString();
            UserSigninAuthentication userSigninAuthentication=new UserSigninAuthentication();
            if (userSigninAuthentication.authenticate(username, password, getApplicationContext())) {
                Intent userHomePageIntent = new Intent(AppHome.this, UserHome.class);
                startActivity(userHomePageIntent);
            } else {
                mPasswordTextView.setBackground(new ColorDrawable(Color.RED));
                mUsernameTextView.setBackground(new ColorDrawable(Color.RED));
                ToastMessage.toastMessage(this, "Username or password do not match");
                onResume();
            }
        }catch (NullPointerException e){
            mPasswordTextView.setBackground(new ColorDrawable(Color.RED));
            mUsernameTextView.setBackground(new ColorDrawable(Color.RED));
            ToastMessage.toastMessage(this, "Username or password do not match");

        }
    }
}

UserSigninAuthentication.java

package com.impresscapital.iclapp.util;

import android.content.Context;
import android.util.Log;

import com.impresscapital.iclapp.database.AppDatabase;
import com.impresscapital.iclapp.database.AppExecutors;
import com.impresscapital.iclapp.dbobj.User;

public class UserSigninAuthentication {

    private AppDatabase mDb;
    private Boolean isAuthenticate=false;

    public boolean authenticate(final String username, final String password,Context context){
        dataBaseOperation(username,password,context);
        return isAuthenticate;
    }

    public void dataBaseOperation(final String username, final String password, final Context context){
        AppExecutors.getsInstance().getDiskIO().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d("DB","Inside db opt");
                    User user = mDb.userDao().loadUser(username, password);
                    if (user != null){
                        Log.d("DB","user found");
                        //ToastMessage.toastMessage(context,user.getUserID()+user.getEmail());
                        isAuthenticate = true;
                    }
                }catch (NullPointerException n){
                    isAuthenticate=false;
                }
            }
        });
    }
}

UserDao.java

package com.impresscapital.iclapp.dao;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import com.impresscapital.iclapp.dbobj.User;

import java.util.List;

@Dao
public interface UserDao {


    @Query("Select * from user_table where username=:username and password=:password")
    User loadUser(String username,String password);

    @Query("Select * from user_table")
    List<User> retrieveAllUser();
    @Insert
    void registerUser(User user);

    @Update
    void updateUser(User user);

    @Delete
    void deleteUser(User user);
}

我已经检索了另一个类中的所有用户。日志代码为

List<User> users=mDb.userDao().retrieveAllUser();
                for(User user1:users){
                    Log.d("Db working",user1.getUsername()+" "+user1.getPassword());
                }
                finish();

和日志消息:

2018-10-29 13:11:47.210 11488-13813/com.impresscapital.iclapp D/Db working: y y
2018-10-29 13:11:47.210 11488-13813/com.impresscapital.iclapp D/Db working: g 1

为方便起见,上传了AppDatabase类:

AppDatabase.claa

package com.impresscapital.iclapp.database;


import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.TypeConverter;
import android.arch.persistence.room.TypeConverters;
import android.content.Context;
import android.util.Log;

import com.impresscapital.iclapp.dao.UserDao;
import com.impresscapital.iclapp.dbobj.User;
import com.impresscapital.iclapp.util.DateConverter;

@Database(entities = {User.class},version = 1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {

    private static final String LOG_CAT=AppDatabase.class.getSimpleName();
    private static final Object LOCK=new Object();
    private static final String DATABASE_NAME="db_icl_app";
    private static AppDatabase sInstance;

    public static AppDatabase getInstance(Context context){
        if(sInstance==null){
            synchronized (LOCK){
                Log.d(LOG_CAT,"Creating new Database");
                sInstance=Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class,AppDatabase.DATABASE_NAME).build();
            }

        }
        Log.d(LOG_CAT,"Getting the database instance");
        return sInstance;
    }

    public abstract UserDao userDao();
}

UserSigninAuthentication.class

package com.impresscapital.iclapp.util;

import android.content.Context;
import android.util.Log;

import com.impresscapital.iclapp.database.AppDatabase;
import com.impresscapital.iclapp.database.AppExecutors;
import com.impresscapital.iclapp.dbobj.User;

import java.util.List;

public class UserSigninAuthentication {

    private AppDatabase mDb;
    private Boolean isAuthenticate=false;

    public boolean authenticate(final String username, final String password,Context context){
        dataBaseOperation(username.trim(),password.trim(),context);
        return isAuthenticate;
    }

    public void dataBaseOperation(final String username, final String password, final Context context){

        Log.d("Username","username: "+username);
        Log.d("Username","username length: "+username.length());
        Log.d("Password","password:"+password);
        Log.d("Password","password length:"+password.length());
        AppExecutors.getsInstance().getDiskIO().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    List<User> users=mDb.userDao().retrieveAllUser();
                    for(User user1:users){
                        Log.d("Db working","Username:"+user1.getUsername()+" Password:"+user1.getPassword());
                    }
                    Log.d("DB","Inside db opt");
                    List<User> user1 = mDb.userDao().loadUser(username, password);
                    Log.d("Total Users after query","Tot"+ user1.size());
                    //ToastMessage.toastMessage(context,user.getUserID()+user.getEmail());
                    isAuthenticate = true;
                }catch (NullPointerException n){
                    isAuthenticate=false;
                }
                Log.d("isAuthenticate Variable", isAuthenticate.toString());
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

尝试使用您的代码进行以下查询。

SELECT * FROM user_table WHERE username like :username and password like :password

在查询数据库工作之前在初始化mDb的注释中提到。

答案 1 :(得分:0)

请改用' '

@Query("Select * from user_table where username='username' and password= 'password'")