大家好,我是 Vic
,今天给大家带来AndroidStudio制作个人资料界面模块以及SQLite数据库的使用
的概述,希望你们喜欢
数据库类
该类继承 extends SQLiteOpenHelper
//核心代码
private static final int DB_VERSION = 1;
public static String DB_NAME = "bxg.db";
public static final String U_USER_INFO = "userInfo";
public SQLiteHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
/**
* 当这个SQLiteOpenHelper的子类类被实例化时会创建指定名的数据库,在onCreate中创建个人信息表
* **/
db.execSQL("CREATE TABLE IF NOT EXISTS " + U_USER_INFO + "( "
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "userName VARCHAR, "
+ "nickName VARCHAR, "
+ "sex VARCHAR, "
+ "signature VARCHAR, "
+ "qq VARCHAR "
+ ")");
}
/**
* 当数据库版本号增加才会调用此方法
**/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + U_USER_INFO);
onCreate(db);
}
创建一个数据库工具类,用于读取、保存、修改用户信息
/**
* 构造方法,只有当类被实例化时候调用
* 实例化SQLiteHelper类,从中得到一个读写的数据库
**/
public DBUtils(Context context) {
helper = new SQLiteHelper(context);
db = helper.getWritableDatabase();
}
/**
* 得到这个类的实例
**/
public static DBUtils getInstance(Context context) {
if (instance == null) {
instance = new DBUtils(context);
}
return instance;
}
//保存个人资料信息
public void saveUserInfo(UserBean bean) {
ContentValues cv = new ContentValues();
cv.put("userName", bean.userName);
cv.put("nickName", bean.nickName);
cv.put("sex", bean.sex);
cv.put("signature", bean.signature);
cv.put("qq",bean.qq);
//Convenience method for inserting a row into the database.
//注意,我们是从数据库使用插入方法,传入表名和数据集完成插入
db.insert(SQLiteHelper.U_USER_INFO, null, cv);
}
//获取个人资料信息
public UserBean getUserInfo(String userName) {
String sql = "SELECT * FROM " + SQLiteHelper.U_USER_INFO + " WHERE userName=?";
//?和下面数组内元素会逐个替换,可以多条件查询=?and =?
//You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs.
Cursor cursor = db.rawQuery(sql, new String[]{userName});
UserBean bean = null;
//Move the cursor to the next row.
while (cursor.moveToNext()) {
bean = new UserBean();
//根据列索引获取对应的数值,因为这里查询结果只有一个,我们也不需要对模型UserBean进行修改,
//直接将对应用户名的所有数据从表中动态赋值给bean
bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
bean.nickName = cursor.getString(cursor.getColumnIndex("nickName"));
bean.sex = cursor.getString(cursor.getColumnIndex("sex"));
bean.signature = cursor.getString(cursor.getColumnIndex("signature"));
bean.qq = cursor.getString(cursor.getColumnIndex("qq"));
}
cursor.close();
return bean;
}
//修改个人资料信息,这里的key指代表字段,value表示数值
public void updateUserInfo(String key, String value, String userName) {
ContentValues cv = new ContentValues();
cv.put(key, value);
//Convenience method for updating rows in the database.
db.update(SQLiteHelper.U_USER_INFO, cv, "userName=?", new String[]
{userName});
}
嗯,接下来好像没什么了。就这样!大概界面可设计自行设计:
示意图
如果觉得不错,那就点个赞吧!❤️
Android
&Java
知识点,有兴趣可以继续关注