public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "t.db";
private static final int DATABASE_VERSION = 1;
private final String CTEATE_TABLE_USER_INFO = "CREATE TABLE" + UserProfile.Users.TABLE_USER_INFO + "(" + UserProfile.Users._ID + "INTEGER PRIMARY KEY," + UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_GENDER + "TEXT," + UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD + "TEXT)";
private final String DROP_TABLE_USER_INFO = "DROP TABLE IF EXISTS" + UserProfile.Users.TABLE_USER_INFO;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CTEATE_TABLE_USER_INFO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE_USER_INFO);
onCreate(db);
}
public boolean addInfor(String username, String dateOfBirth, String gender, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME, username);
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD, password);
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH, dateOfBirth);
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_GENDER, gender);
long row = db.insert(UserProfile.Users.TABLE_USER_INFO, null, contentValues);
db.close();
if (row == 1)
return false;
else
return true;
}
public boolean upgradeInfor(String username, String dateOfBirth, String gender, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD, password);
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH, dateOfBirth);
contentValues.put(UserProfile.Users.TABLE_USER_INFO_COL_GENDER, gender);
String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=?";
String[] whereClauseArgs = {username};
int i;
i = db.update(UserProfile.Users.TABLE_USER_INFO, contentValues, whereClause, whereClauseArgs);
if (i == 1)
return true;
else
return false;
}
public Collection<User> readAllInfor(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor result=db.query(UserProfile.Users.TABLE_USER_INFO,null,null,null,null,null,null);
if(result.getCount()==0){
result.close();
db.close();
return null;
}
User user;
Collection<User> userList=new ArrayList<>();
while(result.moveToNext()){
user=new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)), result.getString(result.getColumnIndexOrThrow
(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)), result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH));
userList.add(user);
}
result.close();
db.close();
return userList;
}
public User readAllInfor(String username) {
SQLiteDatabase db = this.getReadableDatabase();
String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=?";
Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, null, whereClause, new String[] {username}, null, null, null);
if(result.getCount() == 0) {
db.close();
result.close();
return null;
}
User user = null;
while(result.moveToNext()) {
if(result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)).equals(username)) {
user = new User(result.getInt(result.getColumnIndexOrThrow(UserProfile.Users._ID)),
username,
result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_DATE_OF_BIRTH)),
result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)),
result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_GENDER)));
}
}
db.close();
result.close();
return user;
}
public boolean deleteInfo(String username) {
SQLiteDatabase db = this.getWritableDatabase();
String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + " = ?";
String[] whereClauseArgs = {username};
int i;
i = db.delete(UserProfile.Users.TABLE_USER_INFO, whereClause, whereClauseArgs);
db.close();
if(i == 1)
return true;
else
return false;
}
public boolean login(String username, String password) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {UserProfile.Users.TABLE_USER_INFO_COL_USERNAME, UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD};
String whereClause = UserProfile.Users.TABLE_USER_INFO_COL_USERNAME + "=? AND " + UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD + "=?";
Cursor result = db.query(UserProfile.Users.TABLE_USER_INFO, columns, whereClause, new String[] {username, password}, null, null, null);
boolean bool = false;
while(result.moveToNext()) {
if(result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_USERNAME)).equals(username) && result.getString(result.getColumnIndexOrThrow(UserProfile.Users.TABLE_USER_INFO_COL_PASSWORD)).equals(password)) {
bool = true;
break;
}
}
return bool;
}
}