国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

條件數(shù)據(jù)庫Android:sqllite的簡單使用

2020-04-11 12:24:14
字體:
供稿:網(wǎng)友

    SQLite分析

    SQLite是輕量級的、嵌入式的、關(guān)系型數(shù)據(jù)庫,現(xiàn)在已經(jīng)在iPhone、Android等手機(jī)系統(tǒng)中應(yīng)用,SQLite可移植性好,很輕易應(yīng)用,很小,高效而且牢靠。SQLite嵌入到應(yīng)用它的應(yīng)用程序中,它們共用雷同的進(jìn)程空間,而不是單獨(dú)的一個進(jìn)程。從外部看,它并不像一個RDBMS,但在進(jìn)程內(nèi)部,它倒是完整的,自包括的數(shù)據(jù)庫引擎。

    在android中當(dāng)須要操作SQLite數(shù)據(jù)庫的時候須要失掉一個SQLiteOpenHelper對象,而SQLiteOpenHelper是一個抽象類,用戶須要繼承這個類,并實(shí)現(xiàn)該類中的一些方法。

   

    1、繼承SQLiteOpenHelper之后就擁有了以下兩個方法:

    ◆getReadableDatabase() 創(chuàng)立或者打開一個查詢數(shù)據(jù)庫

    ◆getWritableDatabase()創(chuàng)立或者打開一個可寫數(shù)據(jù)庫

    ◆他們都市返回SQLiteDatabase對象,用戶通過失掉的SQLiteDatabase對象進(jìn)行后續(xù)操作

    2、同時用戶還可以覆蓋以下回調(diào)函數(shù),再對數(shù)據(jù)庫進(jìn)行操作的時候回調(diào)以下方法:

    ◆onCreate(SQLiteDatabase):在數(shù)據(jù)庫第一次創(chuàng)立的時候會調(diào)用這個方法,一般我們在這個方法里邊創(chuàng)立數(shù)據(jù)庫表。

    ◆onUpgrade(SQLiteDatabase,int,int):當(dāng)數(shù)據(jù)庫須要修改的時候,Android系統(tǒng)會主動的調(diào)用這個方法。一般我們在這個方法里邊刪除數(shù)據(jù)庫表,并建立新的數(shù)據(jù)庫表,當(dāng)然是否還須要做其他的操作,完整取決于應(yīng)用程序的需求。

    ◆onOpen(SQLiteDatabase):這是當(dāng)打開數(shù)據(jù)庫時的回調(diào)函數(shù),一般也不會用到。

    須要注意

    1、在SQLiteOepnHelper的子類當(dāng)中,必須有以下該構(gòu)造函數(shù)

復(fù)制代碼 代碼如下:

public DatabaseHelper(Context context, String name, CursorFactory factory, 
   int version) { 
  //必須通過super調(diào)用父類當(dāng)中的構(gòu)造函數(shù) 
  super(context, name, factory, version); 
}    為了便利,也可以創(chuàng)立其它的構(gòu)造函數(shù),含二個參數(shù)或者三個參數(shù)的。

    2、函數(shù)public void onCreate(SQLiteDatabase db)是在調(diào)用getReadableDatabase()或者是getWritableDatabase()第一次創(chuàng)立數(shù)據(jù)庫的時候執(zhí)行,實(shí)際上是在第一次失掉SQLiteDatabse對象的時候,才會調(diào)用這個方法.

public void onCreate(SQLiteDatabase db) { 
  System.out.println("create a Database"); 
  //execSQL函數(shù)用于執(zhí)行SQL語句 
  db.execSQL("create table user(id int,name varchar(20))"); 
}   

 上面是寫好的例子,可以參考下
 

復(fù)制代碼 代碼如下:

 public class DBHelper {

    private static DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;

    private static final String   DATABASE_NAME    = "life";

    private static final int      DATABASE_VERSION = 1;

    private Context               mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //創(chuàng)立表結(jié)構(gòu)
            db.execSQL("CREATE TABLE life_history (id INTEGER PRIMARY KEY AUTOINCREMENT,type TEXT, city TEXT, company TEXT, pucNum TEXT, pucName TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

    public DBHelper(Context ctx) throws SQLException {
        this.mCtx = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
    }

    /**
     * 關(guān)閉數(shù)據(jù)源
     */
    public void closeConnection() {
        if (mDb != null && mDb.isOpen())
            mDb.close();
        if (mDbHelper != null)
            mDbHelper.close();
    }

    /**
     * 插入數(shù)據(jù) 參數(shù)
     * @param tableName 表名
     * @param initialValues 要插入的列對應(yīng)值
     * @return
     */
    public long insert(String tableName, ContentValues initialValues) {

        return mDb.insert(tableName, null, initialValues);
    }

    /**
     * 刪除數(shù)據(jù)
     * @param tableName 表名
     * @param deleteCondition 條件
     * @param deleteArgs 條件對應(yīng)的值(如果deleteCondition中有“?”號,將用此數(shù)組中的值替換,一一對應(yīng))
     * @return
     */
    public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {

        return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
    }

    /**
     * 更新數(shù)據(jù)
     * @param tableName 表名
     * @param initialValues 要更新的列
     * @param selection 更新的條件
     * @param selectArgs 更新條件中的“?”對應(yīng)的值
     * @return
     */
    public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {
        return mDb.update(tableName, initialValues, selection, selectArgs) > 0;
    }

    /**
     * 取得一個列表
     * @param distinct 是否去重復(fù)
     * @param tableName 表名
     * @param columns 要返回的列
     * @param selection 條件
     * @param selectionArgs 條件中“?”的參數(shù)值
     * @param groupBy 分組
     * @param having 分組過濾條件
     * @param orderBy 排序
     * @return
     */
    public Cursor findList(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {

        return mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    }

    /**
     * 取得單行記錄
     * @param tableName 表名
     * @param columns 獲取的列數(shù)組
     * @param selection 條件
     * @param selectionArgs 條件中“?”對應(yīng)的值
     * @param groupBy 分組
     * @param having 分組條件
     * @param orderBy 排序
     * @param limit 數(shù)據(jù)區(qū)間
     * @param distinct 是否去重復(fù)
     * @return
     * @throws SQLException
     */
    public Cursor findOne(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {

        Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * 執(zhí)行SQL(帶參數(shù))
     * @param sql
     * @param args SQL中“?”參數(shù)值
     */
    public void execSQL(String sql, Object[] args) {
        mDb.execSQL(sql, args);

    }

    /**
     * 執(zhí)行SQL
     * @param sql
     */
    public void execSQL(String sql) {
        mDb.execSQL(sql);

    }

    /**
     * 判斷某張表是否存在
     * @param tabName 表名
     * @return
     */
    public boolean isTableExist(String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "'";
            cursor = mDb.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

            cursor.close();
        }
        catch (Exception e) {
        }
        return result;
    }

    /**
     * 判斷某張表中是否存在某字段(注,該方法無法判斷表是否存在,因此應(yīng)與isTableExist一起應(yīng)用)
     * @param tabName 表名
     * @param columnName 列名
     * @return
     */
    public boolean isColumnExist(String tableName, String columnName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "' and sql like '%" + columnName.trim() + "%'";
            cursor = mDb.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

            cursor.close();
        }
        catch (Exception e) {
        }
        return result;
    }

}
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江陵县| 武城县| 岫岩| 沁阳市| 富阳市| 嵊州市| 兴文县| 宣汉县| 丰城市| 京山县| 绩溪县| 洞头县| 洪泽县| 周至县| 永福县| 南平市| 元朗区| 建德市| 南涧| 波密县| 乳源| 元氏县| 乐清市| 黎城县| 巴东县| 正定县| 绩溪县| 山东省| 铜鼓县| 革吉县| 新昌县| 罗山县| 遂平县| 扎囊县| 宁明县| 桑日县| 马龙县| 永胜县| 遂昌县| 和平县| 甘肃省|