Android中資料存放區(三)——SQLite資料庫儲存資料

來源:互聯網
上載者:User

標籤:getc   是你   不同   ast   上進   open   bsp   sqlite資料庫   關係   

當一個應用程式在Android中安裝後,我們在使用應用的過程中會產生很多的資料,應用都有自己的資料,那麼我們應該如何儲存資料呢?

資料存放區方式

Android 的資料存放區有5種方式:

1. SharedPreferences儲存資料 
   SharedPreferences資料存放區,也叫作xml儲存。這是將資料存放區“data/data/程式包名/share_prefs”路徑下的到xml檔案中。 
相關串連:《Android中資料存放區(一)——SharedPreferences儲存資料》 
2. 檔案儲存體資料 
   分為內部儲存和外部儲存。內部儲存是應用程式使用Android為自己分配的記憶體空間,資料存放區到“/data/data/程式包名/files”路徑下的相應檔案中。外部儲存是使用手機sdcard的記憶體(這個sdcard並不是我們經常說的那個可以拆卸替換的SD卡,那個SD卡我們稱之為擴充卡),使用這部分記憶體要聲明相應的許可權。 
相關串連: 《Android中資料存放區(二)——檔案儲存體資料 》 
3. SQLite資料庫儲存資料 
  使用資料庫進行儲存,這個一般資料量比較大的時候。   
相關串連:《Android中資料存放區——SQLite資料庫儲存資料》 
4. 使用ContentProvider儲存資料 
  這個比較眼熟,ContentProvider也是Android的四大組件之一。ContentProvider一般是第三方提供的資料存放區方式,向我們手機中的通訊錄連絡人,照片,音樂等…… 
相關串連:《Android中資料存放區——ContentProvider儲存資料 》   
5. 網路儲存資料 
   這個是將資料上傳到網路上進行儲存。

  下面進入我們今天的主要內容,使用SQLite資料庫儲存資料。

SQLite資料庫儲存資料

  SQLite是一個輕量級關係型資料庫,既然是關係型資料庫,那操作起來其實跟mysql、sql server差不多的。 
  SQLite 和其他資料庫最大的不同就是對資料類型的支援,建立一個表時,可以在 CREATE TABLE 語句中指定某列的資料類型,但是你可以把任何資料類型放入任何列中。當某個值插入資料庫時,SQLite 將檢查它的類型。如果該類型與關聯的列不匹配,則 SQLite 會嘗試將該值轉換成該列的類型。如果不能轉換,則該值將作為其本身具有的類型儲存。比如可以把一個字串(String)放入 INTEGER 列。SQLite 稱這為“弱類型”(manifest typing.)。 
  對資料庫SQLite進行操作,我們要藉助於SQLiteOpenHelper類進行操作。對資料庫的操作也就是”增、刪、改、查“。在學習資料庫的操作之前我們首先要學會如何建立資料庫……

建立資料庫

  資料庫的操作藉助於SQLiteOpenHelper,SQLiteOpenHelper是一個抽象類別,我們我們在使用SQLiteOpenHelper時要先建立一個MySQLiteOpenHelper繼承SQLiteOpenHelper類。 
  SQLiteOpenHelper有兩個非常重要的方法:getReadableDatabase()方法返回資料庫是一個唯讀;getWriteableDatabase()方法獲得是一個可讀寫的資料庫物件。這裡我們使用getWriteableDatabase()方法獲得資料庫Database對象。 
   
建立一個MySQLiteOpenHelper繼承SQLiteOpenHelper類:

 1 public class MySQLiteOpenHelper extends SQLiteOpenHelper { 2  3      //構造器,傳入四個參數Context對象,資料庫名字name,操作資料庫的Cursor對象,版本號碼version。  4     public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 5         super(context, name, factory, version); 6     } 7     //自訂的構造器 8     public MySQLiteOpenHelper(Context context, String name) { 9         this(context, name, null, 1);//傳入Context和資料庫的名稱,調用上面那個構造器10     }11 12     @Override13     public void onCreate(SQLiteDatabase sqLiteDatabase) {14         //在建立資料庫時,建立一個資料表table15         String sql = "create table if not exists user(id integer primary key  autoincrement, name varchar(20), passwords varchar(20))";16         sqLiteDatabase.execSQL(sql);17 18     }19     @Override20     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {21     //用於升級資料庫,只需要在建立本類對象時傳入一個比之前建立傳入的version大的數即可。22     }23 }

 

建立資料庫:

1     //建立資料庫2         MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(getApplicationContext(), "create_db");//資料庫名稱為create_db。3         SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();

 

  使用SQLiteDatabase的insert(String table, String nullColumnHack, ContentValues values)方法插入資料。這個方法包含三個參數: 
  我們先列舉一條SQLite中的插入語句:INSERT INTO user (name, passwords)] VALUES ("張三", "123456"); 
String table:操作的資料表的名稱。 
String nullColumnHack:用於我們在未指定添加資料的情況下,為資料表中可以添加null值的資料填入null值。一般這個參數我們傳入null。 
ContentValues values:用於傳遞資料,通常我們通過ContentValues 類的對象的putXXX()方法封裝資料,然後將資料添加進資料庫。 
  ContentValues 類,類似於java中的Map,以索引值對的方式儲存資料。

1          ContentValues value = new ContentValues();2         value.put("name", "張三");//向資料表中欄位名為name的欄位中添加"張三"。3         value.put("passwords", "123456");//向資料表中欄位名為passwords的欄位中添加"123456"。4         //db資料庫物件在前面已經建立,這裡直接使用。5         db.insert("user",null, value);//在資料庫的user資料表中插入:欄位名name為"張三",欄位名passwords為"123456"的資料。

 

  使用SQLiteDatabase的delete(String table, String whereClause, String[] whereArgs)方法刪除資料。這個方法包含三個參數: 
  我們先列舉一條SQLite中的刪除語句:DELETE FROM user WHERE name="張三"。 
String table:操作的資料表的名稱。 
String whereClause:約束刪除行的條件。相當於SQLite語句中“where name=?“內容。 
String[] whereArgs:與前一個參數對應約束刪除行的條件。相當於”where name=”張三““中的”張三“。 
注意:如果參數String whereClause和參數String[] whereArgs都傳null的話,就是刪除所有行。

    //db資料庫物件在前面已經建立,這裡直接使用。    db.delete("user", "name=?", new String[]{"張三"});

 

  使用SQLiteDatabase的 update (String table, ContentValues values, String whereClause, String[] whereArgs)方法刪除資料。這個方法包含四個參數: 
  我們先列舉一條SQLite中的修改語句:UPDATE user SET name= "李四", passwords= "123" WHERE name="張三"。 
String table:操作的資料表的名稱。 
ContentValues values:用於傳遞資料,通常我們通過ContentValues 類的對象的putXXX()方法封裝資料,然後將資料添加進資料庫。 
String whereClause:約束修改行的條件。相當於SQLite語句中“where name=?“內容。 
String[] whereArgs:與前一個參數對應約束刪除行的條件。相當於”where name=”張三““中的”張三“。

1         //db資料庫物件在前面已經建立,這裡直接使用。2         ContentValues values = new ContentValues();3         values.put("passwords", "abcd");4         db.update("user", values, "name=?", new String[]{"張三"});

 

  對於”查“操作,SQLiteDatabase提供了多種方法。 
  我們先列舉一條SQLite中的修改語句:SELECT passwords="123" FROM user。 
(1)使用SQL語句進行查詢。這裡SQLiteDatabase提供了方法:

  • rawQuery (String sql, String[] selectionArgs):該方法返回 Cursor類的對象,用於操作查詢的結果。
1         String sql = "select * from user";2         Cursor cursor = db.rawQuery(sql, null);3         cursor.moveToFirst();//轉移到結果的第一行4         while(!cursor.isAfterLast()){5             String name=cursor.getString(cursor.getColumnIndex("name"));6             String passwords=cursor.getString(cursor.getColumnIndex("passwords"));7             Log.d("data", "name=" + name + "   password=" + passwords);8             cursor.moveToNext();9         }

 

(2)使用SQLiteDatabase內定方法查詢:

  • query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit):這個方法有N多個參數啊……String table是操作的資料表的名稱;String selection是篩選的欄位選項;String[] selectionArgs是欄位選項對應的值;String groupBy是篩選結果的分組依據;String having是在由groupBy子句建立的分組上設定條件;String orderBy是結果的排序方式,String limit是篩選結果的顯示限制,例如“2, 3”是指從篩選結果的第2個開始顯示3個。
1         Cursor cursor=db.query("user", null, null, null, null,null, " id desc", "2,3");//limit語句  offset, num2         cursor.moveToFirst();//轉移到結果的第一行3         while(!cursor.isAfterLast()){4             String name=cursor.getString(cursor.getColumnIndex("name"));5             String passwords=cursor.getString(cursor.getColumnIndex("passwords"));6             Log.d("data", "   name=" + name + "   password=" + passwords);7             cursor.moveToNext();8         }

 

  這樣資料庫的操作和儲存就差不多了哦,對於資料庫的操作也就無非是這四種……

Android中資料存放區(三)——SQLite資料庫儲存資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.