資料儲存方式-SQLite

來源:互聯網
上載者:User

標籤:

SQLite 是用C語言編寫的開源嵌入式資料庫引擎。支援太多數的SQL92biaozhun,並且可以再所有主要的作業系統上運行。
  • 支援高達2TB大小的資料庫
  • 以單個檔案的形式存在
  • 以B-Tree的資料結構形式儲存在磁碟裡

特點:
  1. 輕量級 一個動態庫、單檔案
  2. 獨立性 沒有依賴、無需安裝
  3. 隔離性 全部在一個檔案夾中
  4. 跨平台 支援眾多作業系統
  5. 多語言介面 支援眾多程式設計語言
  6. 安全性 事務

交易處理的安全性問題:
  • 通過資料庫上的獨佔性和共用鎖定實現獨立交易處理
  • 多個進程可以再同一時間從同一資料庫讀取資料,但是只能有一個可以寫入資料

SQLite的資料類型:
  • NULL 、INTEGR、REAL、TEXT和BLOB資料類型
  • 空值、整形值、浮點值、字串值、二進位值
動態資料類型(弱引用)     當某個值插入到資料庫,SQLite將會檢查它的類型,如果該類型與關聯的列不匹配,SQLite則會嘗試將該值轉換成該列的類型,如果不能轉換,則該值將作為本身的類型儲存。

SQLiteDatabase:提供一些管理SQLite資料庫的類提供建立,刪除,執行SQL命令,並執行其他常見的資料庫管理工作的方法每個程式的資料庫名字是惟一的  
  • execSQL(sql)
  • insert(table,nullColumnHack,values)
  • delete(table,whereClause,whereArgs)
  • update(table,values,whereClause,whereArgs)
  • query(table,columns,selection,selectionArgs,groupBy,having,orderBy)
  • rawQuery(sql,selectionArgs)

example:-------------------------------------------------------------------------------------------- //每個程式都有自己的資料庫,一般情況下互不干擾
  //建立一個資料庫並且開啟
  SQLiteDatabase db =  openOrCreateDatabase("user.db",MODE_PRIVATE, null);
  db.execSQL("create table if not exites usertb(_id integer primary key autoincrement,name text not null,age integer not null,sex text not null)");
  db.execSQL("insert into usertb(name,sex,age)values(‘張三‘,‘男‘,‘18‘)");
  db.execSQL("insert into usertb(name,sex,age)values(‘李四‘,‘女‘,‘19‘)");
  db.execSQL("insert into usertb(name,sex,age)values(‘王五‘,‘男‘,‘20‘)");
  Cursor c=db.rawQuery("select * form usertb", null);
  if(c!=null)
  {
   while(c.moveToNext())
   {
    Log.i("info",""+c.getInt(c.getColumnIndex("_id")));
    Log.i("info",""+c.getString(c.getColumnIndex("name")));
    Log.i("info",""+c.getInt(c.getColumnIndex("age")));
    Log.i("info",""+c.getString(c.getColumnIndex("sex")));
   }
   c.close();
  }
  db.close();---------------------------------------------------------------------------------------------------------------

ContentValues:
  • 用來儲存一組可以被ContentResolver處理的值。
  • ContentValues value = newContentValues();
    • //類似 hashMap key value
  • values.put (“key”,“values”);
example:---------------------------------------------------------------------------------------------------------------  SQLiteDatabase db= openOrCreateDatabase("stu.db", MODE_PRIVATE, null);
  db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integet not null)");
  ContentValues values = new ContentValues();
  values.put("name", "王尼瑪");
  values.put("sex", "男");
  values.put("age", 20);
  db.insert("stutb", null, values);
  values.clear();
  values.put("name", "李尼瑪");
  values.put("sex", "男");
  values.put("age", 22);
  db.insert("stutb", null, values);
  values.clear();
  values.put("sex", "女");
  db.update("stutb", values, "_id>?", new String[]{"0"});
  db.delete("stutb", "name like ?", new String[]{"%王%"});
  Cursor c =db.query("stutb", null, "_id>?", new String[]{"0"}, null, null, "name");
  if(c!=null)
  {
   String []columns= c.getColumnNames();
   while(c.moveToNext())
   {
    for(String columnName :columns)
    {
     Log.i("info", c.getString(c.getColumnIndex(columnName)));
    }
   }
  }
 }-------------------------------------------------------------------------------------------------------------------------------------------SQLiteOpenHelper:
  • SQLiteDatabase的協助類,用於管理資料庫的建立和版本更新
  • 一般用法是建立一個類繼承它,並且重寫onCreate()和onUpgrade()方法
  • onCreate(SQLiteDatabase db) 建立資料庫調用,
  • inUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) 版本更新時調用
  • getReadableDatabase()建立或開啟一個唯讀資料庫
  • getWriteDatabase()建立或者開啟一個讀寫資料庫




資料儲存方式-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.