1. 程式人生 > >Android學習之SQLite資料庫儲存

Android學習之SQLite資料庫儲存

  今天學習sqlite資料庫儲存,sqlite資料庫是輕量級的,非常小,只有幾百K大小,非常

移動裝置使用,幾乎所有的手機使用的都是sqlite資料庫。

  sqlite儲存的資料型別:.db

  資料儲存的路徑:/data/data/packageName/databases/xxx.db

  然後是最重要的API的學習:SQLiteOpenHelper,是一個抽象類

  public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version):構造方法

  abstract void onCreate(SQLiteDatabase db):用於建立表

  abstract void onUpgrade():用於版本更新

  SqliteDatebase getReadableDatebase():得到資料庫連線

  SqliteDatebase:代表與資料庫的連線的類

  方法:

    long insert():用於執行insert SQL,返回id值

    int update():用於執行 updata SQL

    int delete():用於執行 delete SQL

    Cursor query():用於執行select SQL ,返回包含查詢結果資料的Cursor

    void execSql(sql):執行sql語句

    beginTransaction():開啟事務

    serTransactionSuccessful():設定事務是成功的

    endTransaction():結束事務,可能提交事務或回滾事務

    openDatabase(String path, CursorFactory factory, int flags):得到資料庫連線

Cursor:包含所有查詢結果記錄的結果集物件(游標,遊標)

   int getCount():匹配的總記錄數

   boolean moveToNext():將遊標移動到下一條記錄的前面

   Xxx getXxx(columnIndex):根據欄位下標得到對應的值

   int getColumnIndex(columnIndex):根據欄位名得到對應的下標