如何从Kotlin的数据库SQLite中读取登录名和密码?

时间:2019-12-11 08:39:50

标签: android sqlite kotlin

我在Kotlin并尝试创建一些应用。我为Android应用程序创建了SQLite数据库。在数据库中,我有“登录”和“密码”。如何阅读以进行登录?

我的数据库.kt

fun insertIntoPartner(plogin: String?, ppassword: String?): Boolean {
        var contentValues = ContentValues()
        contentValues.put("plogin", plogin)
        contentValues.put("ppassword", ppassword)

        val rowId = writableDatabase.insert(PARTNER, null, contentValues)
        return rowId>0
    }

我的登录代码:

bin.setOnClickListener {

                if (login.text.toString() == // here i read database login 
                          && password.text.toString() == here i read database password) {

                    val i = Intent(this, NavigationMenu::class.java)
                    startActivity(i)

                } else toast("No login or password")

感谢所有建议。

UPD 1:

fun checkUser(plogin: String?, ppassword: String?): Boolean {

        val columns = arrayOf(pid)
        val db = this.readableDatabase
        val selection = "$plogin = ? AND $ppassword = ?"
        val selectionArgs = arrayOf(plogin, ppassword)

        val cursor = db.query(PARTNER, columns, selection, selectionArgs,
            null, null, null)

        val cursorCount = cursor.count
        cursor.close()
        db.close()

        if (cursorCount > 0) return true
        else return false
    }

我的理解我必须在字段中替换“ USER_ID ”,“ TABLE_NAME ”等。但是,如果我的ID为“ pid(整数主键)”怎么办?当我写“ pid”时,它是红色的,并告诉“未解决的参考”。我怎么了?

我的 CREATE_TABLE 如下:

companion object {
        val PARTNER = "partner"
        val DB_VERSION = 1
        val CREATE_TABLE_PARTNER = "CREATE TABLE partner(pid INTEGER PRIMARY KEY, plogin TEXT, ppassword TEXT," +
                " pname TEXT, psale TEXT, pasale TEXT, pengsale TEXT, prusdescription TEXT, pengdescription TEXT," +
                " pinn TEXT, pemail TEXT, pphonenumber TEXT, psitecompany TEXT, pstreet TEXT, pcity TEXT," +
                " pregion TEXT, pcountry TEXT, pindex TEXT)"
        val SELECT_PARTNER = "SELECT * from partner"
    }

1 个答案:

答案 0 :(得分:0)

在您的 MyDatabase.k t类中:

/**
     * This method to check user exist or not
     *
     * @param email
     * @param password
     * @return true/false
     */
    fun checkUser(email: String, password: String): Boolean {

        // array of columns to fetch
        val columns = arrayOf(COLUMN_USER_ID)

        val db = this.readableDatabase

        // selection criteria
        val selection = "$COLUMN_USER_EMAIL = ? AND $COLUMN_USER_PASSWORD = ?"

        // selection arguments
        val selectionArgs = arrayOf(email, password)

        // query user table with conditions
        /**
         * Here query function is used to fetch records from user table this function works like we use sql query.
         * SQL query equivalent to this query function is
         * SELECT user_id FROM user WHERE user_email = 'viral@exmaple.com' AND user_password = 'qwerty';
         */
        val cursor = db.query(TABLE_USER, //Table to query
                columns, //columns to return
                selection, //columns for the WHERE clause
                selectionArgs, //The values for the WHERE clause
                null,  //group the rows
                null, //filter by row groups
                null) //The sort order

        val cursorCount = cursor.count
        cursor.close()
        db.close()

        if (cursorCount > 0)
            return true

        return false

    }

然后 MyLogin.kt 类:

   private fun verifyFromSQLite() {
   // Make changes as per your requirement 
        if (MyDatabase!!.checkUser(textInputEditTextEmail!!.text.toString().trim { it <= ' ' }, textInputEditTextPassword!!.text.toString().trim { it <= ' ' })) {


            val accountsIntent = Intent(activity, UserActivity::class.java)
            emptyInputEditText()
            startActivity(accountsIntent)


        } else {

            // Snack Bar to show success message that record is wrong
            Snackbar.make(nestedScrollView!!, getString(R.string.error_email_password), Snackbar.LENGTH_LONG).show()
        }
    }

已编辑:看到创建表的样子:

// create table sql query
    private val CREATE_USER_TABLE = ("CREATE TABLE " + TABLE_USER + "("
            + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT,"
            + COLUMN_USER_EMAIL + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")")