標籤:distinct 查詢方式 erb query 內容 執行 sql語句 order after
目標:是否可以在PC端案頭上使用SQLite資料庫製作一個財務檔案?
目錄:
來源:
實踐:
總結和比較:
SQLite資料簡介
是什麼,內部結構是怎樣的,資料庫和表的關係是什麼
有什麼用
常用的操作是什麼
SQLite資料庫使用
增
刪
改
查
SQLite資料庫實踐上的最佳化措施
對於Android平台來說,系統內建了豐富的API來供開發人員操作SQLite,使我們輕鬆完成對資料的存取。
步驟1,熟悉建立資料庫表,熟悉相關的操作指令,實現對SQLite資料庫的感性認識
建立一個包含簡單內容的資料庫(資料庫名)和對應的表(表名);建立表後,可執行簡單的“增刪改查”指令。
/** * <功能描述> 初始化資料庫,建立資料庫 * * @return void [傳回型別說明] */ private void initDataBase() { mDatabase = openOrCreateDatabase("SqlDemo.db", Context.MODE_PRIVATE, null); // CREATE_TABLE = "CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)"; mDatabase.execSQL(CREATE_TABLE); }
建立資料庫的操作調用了execSQL(...),參數代表的是建立資料庫指令,其結果會在/data/data/packageName/databases/目錄下產生對應的資料庫檔案,同時執行了在資料庫中建立了person的資料表。
步驟2,在資料庫中建立資料表,並執行相關的基本操作
/** * <功能描述> 執行一些資料庫和表的操作 * * @return void [傳回型別說明] */ private void doSomeAction() { Person person = new Person("john", 30); mDatabase.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[] { person.getName(), person.getAge() }); person.setName("David"); person.setAge(33); ContentValues cv = new ContentValues(); cv.put("name", person.getName()); cv.put("age", person.getAge()); mDatabase.insert("person", null, cv); cv = new ContentValues(); cv.put("age", 35); mDatabase.update("person", cv, "name=?", new String[] { "john" }); Cursor cursor = mDatabase.rawQuery( "SELECT * FROM person WHERE age >= ?", new String[] { "33" }); while (cursor != null && cursor.moveToFirst()) { int _id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.d(TAG, "_id>=" + _id + ", name=>" + name + ", age=>" + age); } cursor.close(); mDatabase.delete("person", "age<?", new String[] { "35" }); mDatabase.close(); }
粗略地區分對資料庫的操作,有以下兩種:資料庫操作、資料庫中資料表的操作。
對於資料庫中資料表的操作中,添加、刪除和更新可以使用下述兩種方式:
public void execSQL(String sql) throws SQLException;public void execSQL(String sql, Object[] bindArgs) throws SQLException;
上述可以認為是一種統一的形式,執行的是傳統資料庫操作的指令。
此外,還有對應的可供區分的API方法:
public long insert(String table, String nullColumnHack, ContentValues values);public int update(String table, ContentValues values, String whereClause, String[] whereArgs);public int delete(String table, String whereClause, String[] whereArgs);
參數中的table都表示對應資料庫中對應table;ContentValues執行個體values如下內容:
/** Holds the actual values */private HashMap<String, Object> mValues;
其中Key表示的列名,Values表示的是待寫入的內容。參數String whereClause表示WHERE運算式,而String[]則為上述對應預留位置的實際參數值。
查詢語句相對來說較為複雜,因為工程問題中經常會面對各種各樣的查詢問題,系統也考慮到了這種複雜性,為我們提供了較為豐富的查詢形式:
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)public Cursor queryWithFactory(CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)public Cursor queryWithFactory(CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
此外還有如下四種粗略查詢方式:
public Cursor rawQuery(String sql, String[] selectionArgs)public Cursor rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)public Cursor rawQueryWithFactory(CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)public Cursor rawQueryWithFactory(CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable, CancellationSignal cancellationSignal)
其中sql字串,表示一個SQL語句,可以使用預留位置代替實際值;而selectionArgs就是預留位置的實際參數集合;columns表示要查詢的列的所有名稱集合;selection表示WHERE之後的條件陳述式,可以使用預留位置。
上述API都會返回Cursor執行個體,代表資料集的遊標。
SQL資料庫的靈活使用,更多的是在查詢語句中,包含更多的技巧和方法;因為這也是Android UI展示的內容來源。
Cursor的可用方法如下:
c.move(int offset); //以當前位置為參考,移動到指定行 c.moveToFirst(); //移動到第一行 c.moveToLast(); //移動到最後一行 c.moveToPosition(int position); //移動到指定行 c.moveToPrevious(); //移動到前一行 c.moveToNext(); //移動到下一行 c.isFirst(); //是否指向第一條 c.isLast(); //是否指向最後一條 c.isBeforeFirst(); //是否指向第一條之前 c.isAfterLast(); //是否指向最後一條之後 c.isNull(int columnIndex); //指定列是否為空白(列基數為0) c.isClosed(); //遊標是否已關閉 c.getCount(); //返回總資料項目數(行數) c.getPosition(); //返回當前遊標所指向的行數 c.getColumnIndex(String columnName);//返回某列名對應的列索引值 c.getString(int columnIndex); //返回當前行指定列的值
擷取內容則是使用如下API:
byte[] getBlob(int columnIndex);String getString(int columnIndex);short getShort(int columnIndex);int getInt(int columnIndex);long getLong(int columnIndex);float getFloat(int columnIndex);double getDouble(int columnIndex);
步驟3:實際開發對上述操作做改進,以更方便開發流程。
在實際開發中,為了更好地管理和維護資料庫,會封裝一個繼承自SQLiteOpenHelper類的資料庫操作管理類,然後以這個類為基礎再封裝自己的商務邏輯。
A helper class to manage database creation and version management.
Android資料存放區引擎---SQLite資料庫