1. 程式人生 > 實用技巧 >AndroidStudio製作個人資料介面模組以及SQLite資料庫的使用

AndroidStudio製作個人資料介面模組以及SQLite資料庫的使用

前言

大家好,給大家帶來AndroidStudio製作個人資料介面模組以及SQLite資料庫的使用的概述,希望你們喜歡

學習目標

  1. 掌握SQLite資料庫的使用,能夠實現用資料庫來儲存使用者的資訊;
  2. 學會運用好個人資料,以及個人資料的修改功能實現;
  3. 個人資料包括使用者名稱,暱稱,性別,簽名,QQ號或個人社交賬號的記錄等。

資料庫的建立

資料庫類

該類繼承 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});
}

嗯,接下來好像沒什麼了。就這樣!大概介面可設計自行設計:

如果覺得不錯,那就點個贊吧!❤️

總結

  • 本文講了AndroidStudio製作個人資料介面模組以及SQLite資料庫的使用,如果您還有更好地理解,歡迎溝通
  • 定位:分享 Android&Java知識點,有興趣可以繼續關注