2017年3月18日 星期六

SQLite Database

SQLite Database



SQLite Database:SQLite資料庫。



Database - Package


     主要包裹android.database.sqlite。

Database - Creation


       建立SQLite資料庫需要呼叫openOrCreateDatabase 方法,資料庫名稱和模式作為參數。

     例如:

         SQLiteDatabase mydatabase = openOrCreateDatabase("your database name",MODE_PRIVATE,null);

  在資料庫包裹還有其他方法


      openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler):

       openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)

       openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)

     openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)


  

Database - Insertion(插入資料)


    如果要建立資料表表格或者插入資料,需要 execSQL 方法。
  例如:

       mydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username VARCHAR,Password VARCHAR);"); 

       mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('admin','admin');");

   其他方法

        execSQL(String sql, Object[] bindArgs):不但插入數據,而且還用於更新或使用綁定參數在修改數據庫中已經存在的數據。

    

Database - Fetching(取資料)

    如果要取回資料,需要呼叫 rawQuery 的方法。

   例如:
     
       Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null); //第一個參數是執行的 SQL,第二個參數是對應的符號查詢條件

      resultSet.moveToFirst();//將指標移至第一筆資料

     String username = resultSet.getString(0); //取得resultSet的第0欄位資料。
     String password = resultSet.getString(1);


    Cursor 其他類別

      getColumnCount():取得資料表欄位總數。

        getColumnIndex(String columnName)取得欄位為name的index。

       getColumnName(int columnIndex)取得index的列名。

         getColumnNames()取得所有字串的列名。
         getCount(int)取得資料表的資料比數。

         getPosition(int)取得游標(cursor)的目前位置。

         isClosed():判斷是否游標(cursor)是否關閉。如為true,代表已關閉。

      

Database - Helper class


      
   管理所有關於資料庫的操作,需要SQLiteOpenHelper。它會自己建立和更新資料庫

     例如:
        public class DBHelper extends SQLiteOpenHelper {
   public DBHelper(){
      super(context,DATABASE_NAME,null,1);
   }
   public void onCreate(SQLiteDatabase db) {}
   public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}


參考文獻


Android - SQLite Database


Android SQLite數據庫教程

Android Tutorial 第三堂(3)Android 內建的 SQLite 資料庫

Android 數據存儲 (一)SQLite

Android高效入門—SQLite資料庫

Android SQLite Database Tutorial

Android SQLite Database Example Tutorial

Android SQLite database and content provider - Tutorial
http://www.vogella.com/tutorials/AndroidSQLite/article.html

Android 之execSQL和rawQuery方法完成資料的添刪改查操作(execSQL)
http://fecbob.pixnet.net/blog/post/35503391-android-%E4%B9%8Bexecsql%E5%92%8Crawquery%E6%96%B9%E6%B3%95%E5%AE%8C%E6%88%90%E8%B3%87%E6%96%99%E7%9A%84%E6%B7%BB%E5%88%AA%E6%94%B9

[Android] Android的資料儲存-SQLite的使用
http://charleslin74.pixnet.net/blog/post/438223288-%5Bandroid%5D-android%E7%9A%84%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98-sqlite%E7%9A%84%E4%BD%BF%E7%94%A8

Android 的 SQLite 學習筆記
http://www.winwu.cc/2016/02/android-sqlite.html

Android開發筆記-使用Cursor讀取SQLite回傳值
https://www.moke.tw/wordpress/computer/advanced/249

Android學習_對SQLite查詢(query方法)取得Cursor
http://wangshifuola.blogspot.tw/2011/06/androidsqlitequerycursor.html

Android中Cursor類的概念和用法
http://fanli7.net/a/bianchengyuyan/_NET/20110908/127803.html

[轉]Android CursorIndexOutOfBoundsException database
http://pcwiki.pixnet.net/blog/post/94066550-%5B%E8%BD%89%5Dandroid-cursorindexoutofboundsexception-database

Android中Cursor類總結-概念和用法
https://read01.com/xDGJJ.html

Cursor
https://developer.android.com/reference/android/database/Cursor.html

[android] 自定義返回鍵功能
http://h124224.pixnet.net/blog/post/28195007-%5Bandroid%5D-%E8%87%AA%E5%AE%9A%E7%BE%A9%E8%BF%94%E5%9B%9E%E9%8D%B5%E5%8A%9F%E8%83%BD

Android Activity生命周期(moveTaskToBack)
https://kknews.cc/zh-tw/tech/9ylzo8.html

Android 利用 ScrollView 來呈現捲軸版面
http://tomkuo139.blogspot.tw/2016/03/android-scrollview.html

[Android] 當顯示內容超過一個頁面時,使用ScrollView
http://charleslin74.pixnet.net/blog/post/437091505-%5Bandroid%5D-%E7%95%B6%E9%A1%AF%E7%A4%BA%E5%85%A7%E5%AE%B9%E8%B6%85%E9%81%8E%E4%B8%80%E5%80%8B%E9%A0%81%E9%9D%A2%E6%99%82,%E4%BD%BF%E7%94%A8scrollv

Android程式設計(十二) Activity互動作業-2(getIntent)
http://nkeegamedev.blogspot.tw/2010/06/android-activity-2.html

[Android] Intent方法getExtras()
http://moneyd.pixnet.net/blog/post/26062603-%5Bandroid%5D-intent%E6%96%B9%E6%B3%95getextras()

Android EditText元件XML之API(setFocusable)
http://mylinandroidcode.blogspot.tw/2014/05/android-edittextxmlapi.html

Android之CharSequence類型
https://read01.com/Qejxe.html

View
https://developer.android.com/reference/android/view/View.html



沒有留言:

張貼留言