如何检查来自2个数据库

时间:2017-10-16 17:36:57

标签: java android sqlite

所以我目前正在创建2个数据库,即客户端和助产士。所以在登录页面代码时,我应该如何修改代码,以便我可以检查插入的用户名和密码是来自客户端还是助产士?先感谢您。

databasehelper = client // databasehelper1 = midwife

这是我在代码中的签到:

    _btnsignin.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {

            _username = (EditText) findViewById(R.id.editusername);
            _password = (EditText) findViewById(R.id.editpassword);
            String str1 = _username.getText().toString();
            String str2 = _password.getText().toString();
            cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_2 + "=? AND " + DatabaseHelper.COL_9 + "=?", new String[]{str1, str2});
            //cursor1 = db1.rawQuery("SELECT * FROM " + DatabaseHelper1.TABLE_NAME + " WHERE " + DatabaseHelper1.COL_2 + "=? AND " + DatabaseHelper1.COL_9 + "=?", new String[]{str1, str2});
            if (cursor != null)
            {
                if (cursor.getCount() > 0)
                {
                    Intent gotowelcome = new Intent(SIGN_IN.this, WELCOME.class);
                    gotowelcome.putExtra("Username", str1);
                    SIGN_IN.this.startActivity(gotowelcome);
                } else
                    {
                    Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
                    }
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

对于具有用户类型指示符(助产士或客户端)的用户,最好的选择是没有两个数据库,而是数据库中的1个表。

但是,您可以采取以下措施: -

    cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_2 + "=? AND " + DatabaseHelper.COL_9 + "=?", new String[]{str1, str2});
    cursor1 = db1.rawQuery("SELECT * FROM " + DatabaseHelper1.TABLE_NAME + " WHERE " + DatabaseHelper1.COL_2 + "=? AND " + DatabaseHelper1.COL_9 + "=?", new String[]{str1, str2});

    if(cursor.getCount() == 1 || cursor1.getCount() == 1) {
        boolean fromclient = (cursor.getCount == 1); //<<< check which type

        Intent gotowelcome ........        
    } else {
        Toast.makeText(getApplicationContext(), "ERROR",........
    }

其中....表示您已有的代码。上面的确假设了一个唯一的用户/密码组合,尽管您可以通过以下方式确定: -

    boolean sameusernamepasswordforboth = (cursor.getCount() == 1 && cursor1.getCount() == 1);

    boolean sameusernamepasswordforboth = ((cursor.getCount() + cursor1.getCount()) > 1);