如何在共享首选项中存储用户名密码

时间:2014-11-26 14:41:09

标签: android

如何使用name检索数据库中的数据,其中name是字符串形式,但其编号如name =" 111000" thi是字符串形式的名称

  String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USER + "("
    + KEY_USER_ID + " INTEGER PRIMARY KEY, " + KEY_USER_NAME + " TEXT, " + KEY_USER_PASSWORD + "      
   TEXT " + ")";



         private static final String KEY_USER_ID = "id";
private static final String KEY_USER_NAME = "user_name";
private static final String KEY_USER_PASSWORD = "user_password";

       public String SerchUserName(String UserName) {

    String result = null;


    String selectQuery = "SELECT  * FROM " + TABLE_USER + "  WHERE " +KEY_USER_NAME+ " = '"+String.valueOf(UserName)+"'";
    String result_0;
    String result_1;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list

    while (cursor.moveToNext()) {
        result_0 = cursor.getString(0);
        result_1 = cursor.getString(1);
        // and so on
        result = result_0 + "> " + result_1;
    }
    cursor.close();

    db.close();

    return result;

}

1 个答案:

答案 0 :(得分:0)

您的标题和问题无关紧要...如果您想知道如何在共享首选项中存储用户名密码,请使用以下代码

我希望它有所帮助

以共享首选项保存用户和密码

SharedPreferences preferences = this.getSharedPreferences("com.example.sample.prefs",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("USER_NAME","your user name");
editor.putString("PASSWORD","user password");
editor.commit();

从共享首选项中检索用户和密码

SharedPreferences preferences = this.getSharedPreferences("com.example.sample.prefs",0);
String user_name=preferences.getString("USER_NAME",null);
String password=preferences.getString("PASSWORD",null);