我正在创建一个应用,用户可以登录到自己的帐户,通过EditText
中的内容,然后它会显示在TextView
中。我的观点是保存并显示每个用户自己的文本。这是我到目前为止所得到的
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "UserManager.db";
// User table name
private static final String TABLE_USER = "user";
// User Table Columns names
private static final String COLUMN_USER_ID = "userid";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_PASSWORD = "user_password";
private static final String COLUMN_USER_HOURS = "user_hours";
// create table sql query
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT,"
+ COLUMN_USER_PASSWORD + " TEXT," + COLUMN_USER_HOURS + " TEXT" + ")";
// drop table sql query
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
SQLiteDatabase db;
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 3);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
this.db = sqLiteDatabase;
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop User Table if exist
db.execSQL(DROP_USER_TABLE);
// Create tables again
onCreate(db);
}
public void addUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME, user.getName());
values.put(COLUMN_USER_PASSWORD, user.getPassword());
// Inserting Row
db.insert(TABLE_USER, null, values);
db.close();
}
public boolean checkUser(String name, String password) {
// array of columns to fetch
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = COLUMN_USER_NAME + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
// selection arguments
String[] selectionArgs = {name, password};
Cursor 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
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}
public void updateData(UserData userData) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_HOURS, userData.getHours() );
// updating row
db.update(TABLE_USER, values, COLUMN_USER_HOURS + " = ?",
new String[]{String.valueOf(userData.getHours())});
db.close();
}
我想要从列COLUMN_USER_HOUR
存储和检索用户的EditText值,遗憾的是我不知道如何将它们全部设置在一起
,我假设应该为每个用户的ID识别COLUMN_USER_HOUR
。感谢帮助 ;)
这是UserData
public class UserData {
private String hours;
public String getHours(){
return hours;
}
public void setHours(String hours){
this.hours = hours;}
}
答案 0 :(得分:0)
您可以将upDateData
方法更改为: -
public int updateData(long userid, UserData userData) {
int rv;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_HOURS, userData.getHours() );
// updating row
rv = db.update(TABLE_USER, values, COLUMN_USER_ID + " = ?",
new String[]{Long.toString(userid)});
db.close();
return rv;
}
因此,您传递 userid 以及要更改的数据,该方法将返回所做的更新次数(如果有效则为1,如果没有更新则为0)。
我知道,但是现在我只想知道如何保存和 检索唯一用户的数据(当用户登录我想要的帐户时) 在textview中显示他通过edittext存储在数据中的内容i 知道这听起来很奇怪,但我试图了解如何 构建数据:)
类似的更改,而不是返回布尔值返回用户ID(如果失败则返回0),例如: -
public long checkUser(String name, String password) {
long rv = 0;
// array of columns to fetch
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = COLUMN_USER_NAME + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
// selection arguments
String[] selectionArgs = {name, password};
Cursor 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
if (cursor.moveToFirst) {
rv = Long.toString(cursor.getLong(csr.getColumnIndex(COLUMN_USER_ID);
}
cursor.close();
db.close();
return rv;
}
你可以使用getHours
方法: -
public String getHours(long id) {
String rv = "";
SQLiteDatabase db = this.getWriteableDatabase();
Cursor cursor = db.query(TABLE_USER,
null,
COLUMN_USER_ID = "+?",
new String[]{Long.toString(id)},
null,null,null
);
if (cursor.moveToFirst()) {
rv = cursor.getString(cursor.getColumnIndex(COLUMN_USER_HOURS));
}
cursor.close();
db.close();
return rv;
}
您的活动可能包含以下内容: -
DatabaseHelper dbhlpr = new DatabaseHelper(this);
long current_userid = dbhlpr.checkUser(
username_EditText.getText().toString(),
userpassword_EditText.gettext.toString());
if (current_userid > 0) {
hours_TextView.settext(dbhlpr.getHours(current_userid));
} else {
//.... handle invalid login attempt here.
}