Android SQLiteHelper(數據庫工具)

前言

對於Android SQLite數據庫而言,自身擁有一些操作方法,只是在做項目的過程中不適應我們APP研發的效率問題,程序與也不能光是爲代碼而代碼。代碼不是越寫越多好而是越寫月少、越寫越效率好。對於Android SQLite數據庫而言,自身擁有一些操作方法,只是在做項目的過程中不適應我們APP研發的效率問題,程序與也不能光是爲代碼而代碼。代碼不是越寫越多好而是越寫月少、越寫越效率好。通常我們項目中數據庫無外乎是對象的操作、數據表的操作、數據庫的升級、表的曾刪改查。在網絡上無外乎也有了很多的數據庫工具,但是回想一下你用到的功能又有幾個呢,很多都是Java註解來寫的,本人不提倡什麼都用註解,註解本省會消耗已經的性能和時間,雖然時間可以忽略,但是處女座的我就是很看不下去,所以我自己開始來寫數據庫方便的工具。

使用

SQLiteHelper.jar下載

SQLiteHelper.arr下載

  • jar配置方法:
    jar文件下載後,AndroidStudio中複製到app/libs/, jniLibs文件夾直接複製到app/src/main/,values中文件

  • arr配置方法: arr文件複製到app/libs/,在app/build.gradle 中android裏面配置

repositories {
        flatDir {
            dirs 'libs'
        }
}

dependencies裏面配置:

dependencies {
        compile(name: 'SQLiteHelper', ext: 'aar')//這裏你arr是什麼名字就寫什麼名字
}
  1. 創建數據庫
import android.content.Context;

/**
 1. Created by Relin
 2. on 2018-09-19.<br/>
 3. 繼承SQLiteHelper重寫onCreate和onUpgrade兩個方法
 4. 在onCreate方法裏創建自己的表
 5. 在onUpgrade方法裏面更新自己的表<br/>
 6. inherit the SQLiteHelper to rewrite the onCreate and onUpgrade methods
 7. create your own table in the onCreate method
 8. update your table in the onUpgrade method
 */
public class ISQLite extends SQLiteHelper {

    public ISQLite(Context context) {
        super(context);
    }
	
    public ISQLite(Context context, String databaseName, int databaseVersion) {
        super(context, databaseName, databaseVersion);
    }
        @Override
    public void onCreate(SQLiteDatabase db) {
        super.onCreate(db);
        //create your tables
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onUpgrade(db, oldVersion, newVersion);
        //upgrade your tables
    }
}
  1. 創建表(ISQLite 類)
    @Override
    public void onCreate(SQLiteDatabase db) {
        super.onCreate(db);
        //(1)更具表名、字段創建
        createTable(db,"TableName",new String[]{"id","name"});
        //(2)根據對象創建
        User user = new User();
        user.setId("1");
        user.setName("name");
        createTable(db,user);
    }
  1. 升級數據庫
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onUpgrade(db, oldVersion, newVersion);
        if (newVersion > oldVersion) {
            db.execSQL("ALTER TABLE " + "\"TableName\"" + "RENAME TO " + "\"RE_TABLE_NAME\"" + ";");
        }
    }
  1. 查詢數據
    /**
     * 查詢數據
     * query from databases and find you what you are gong to finding.
     *
     * @param sql 數據庫語句
     * @return
     */
    public List<Map<String, String>> query(String sql)

    /**
     * 查詢數據
     *
     * @param cls       實體類
     * @param sql       sql語句
     * @param <T>       實體類,例如:User.class
     * @return          實體列表
     */
    public <T> List<T> query(Class<T> cls, String sql)
  1. 插入數據
    /**
     * 插入數據
     * insert you want what information.
     *
     * @param sql sql語句
     * @return
     */
    public void insert(String sql)

    /**
     * 插入數據
     * insert you want what information.
     *
     * @param table
     * @param contentValues
     * @return
     */
    public long insert(String table, ContentValues contentValues) 

	 /**
     * 插入對象數據
     *
     * @param obj
     * @return 插入數據
     */
    public long insert(Object obj)
  1. 修改操作
    /**
     * 更新數據
     * update from your table in database.
     *
     * @param table
     * @param contentValues you is gonging to update values
     * @param whereClause   for example "name = ?"
     * @param whereArgs     for example new String[]{"Marry"}
     * @return
     */
    public long update(String table, ContentValues contentValues, String whereClause, String[] whereArgs)

    /**
     * 更新數據
     * update from your table in database.
     *
     * @param obj         data object
     * @param whereClause for example "name = ?"
     * @param whereArgs   for example new String[]{"Marry"}
     * @return
     */
    public long update(Object obj, String whereClause, String[] whereArgs) 


    /**
     * 更新數據
     * update from your table in database.
     * for example update user set user_name = 'Jerry' where user_name = 'Marry'
     *
     * @param sql
     * @return
     */
    public void update(String sql)
  1. 刪除操作
    /**
     * 刪除表中的數據
     *
     * @param table
     */
    public void deleteTable(Class table)

    /**
     * 刪除表
     * drop table
     *
     * @param table 數據表
     */
    public void dropTable(String table) 

    /**
     * 清除表中數據
     * truncate table
     *
     * @param table 數據表
     */
    public void deleteTable(String table)

    /**
     * 刪除數據庫
     * delete database
     */
    public void dropDatabase()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章