歡迎您光臨本站 註冊首頁

· SQLite雜誌閱讀

SQLite資料庫相關知識

admin @ 2020-04-18 reply:0

  1.為什么要用SQLite

1.不需要一個單獨的伺服器程序或操作的系統(無伺服器的)。

2.SQLite 不需要配置,這意味著不需要安裝或管理。

3.一個完整的 SQLite 資料庫是儲存在一個單一的跨平臺的磁碟檔案。

4.SQLite 是非常小的,是輕量級的,完全配置時小於 400KiB,省略可選功能配置時小於250KiB。

5.SQLite 是自給自足的,這意味著不需要任何外部的依賴。

6.SQLite 事務是完全相容 ACID 的,允許從多個程序或執行緒安全訪問。

7.SQLite 支援 SQL92(SQL2)標準的大多數查詢語言的功能。

8.SQLite 使用 ANSI-C 編寫的,並提供了簡單和易於使用的 API。

9.SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中執行。

2.什么是SQLite

SQLite是一個程序內的庫,實現了自給自足的、無伺服器的、零配置的、事務性的 SQL 資料庫引擎。它是一個零配置的資料庫,這意味著與其他資料庫一樣,您不需要在系統中配置。就像其他資料庫,SQLite 引擎不是一個獨立的程序,可以按應用程式需求進行靜態或動態連線。SQLite 直接訪問其儲存檔案。

3.如何建立資料庫和資料表

1.SQLiteOpenHelper幫助類,藉助這個類就可以非常簡單地對資料庫進行建立和升級。 SQLiteOpenHelper 是一個抽象類

2.SQLiteOpenHelper 中有兩個抽象方法,分別是onCreate()和 onUpgrade(),我們必須在自己的幫助類裡面重寫這兩個方法,然後分別在這兩個方法中去實現建立、升級資料庫的邏輯

3.SQLiteOpenHelper中還有兩個非常重要的例項方法, getReadableDatabase()和getWritableDatabase()這兩個方法都可以建立或開啟一個現有的資料庫(如果資料庫已存在則直接開啟,否則建立一個新的資料庫),並返回一個可對資料庫進行讀寫操作的物件。不同的是,當資料庫不可寫入的時候(如磁碟空間已滿)getReadableDatabase()方法返回的物件將以只讀的方式去開啟資料庫,而getWritableDatabase()方法則將出現異常。

1、新建一個MySQLite繼承SQLiteOpenHelper

public class MySQLite extends SQLiteOpenHelper { public static String DB_NAME= "MySQLite.db" ; public MySQLite (Context context, int version) { super (context, DB_NAME, null , version); } @Override public void onCreate (SQLiteDatabase db) { db.execSQL( "create table 表名 (_id integer primary key autoincrement)" ); } @Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) { } }

2、呼叫構造方法建立資料庫例項

MySQLite sqlhelper= new MySQLite( this , 1 );

3.SQLiteDatabase實現增、刪、改(更新)、查

呼叫 SQLiteOpenHelper的getReadableDatabase()或getWritableDatabase()

這兩個方法還都會返回一個SQLiteDatabase物件,藉助這個物件就可以對資料進行 CRUD 操作了

新增資料

MySQLite sqlhelper= new MySQLite( this , 1 ); SQLiteDatabase sd = sqlhelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put( "列名" ,值) sd.insert(表名, null , values);

*SQLiteDatabase 中提供了一個 insert()方法,新增資料。

insert(String table, String nullColumnHack, ContentValues values)

它接收三個引數,

第一個引數是表名,我們希望向哪張表裡新增資料,這裡就傳入該表的名字。

第二個引數用於在未指定新增資料的情況下給某些可為空的列自動賦值,不用直接傳入 null 即可。

第三個引數是一個 ContentValues 物件,它提供了一系列的 put()方法過載,用於向 ContentValues 中新增資料,*

刪除資料

比如刪除id大於 10 的資料 sd.delete(表名, "_id>?" , new String []{ "10" });

*SQLiteDatabase 中提供了一個 delete()方法,刪除資料

delete(String table, String whereClause, String[] whereArgs)

這個方法接收三個引數,

第一個引數是表名,第二、第三個引數又是用於去約束刪除某一行或某幾行的資料,不指定的話預設就是刪除所有行*

修改(更新)資料

*SQLiteDatabase 中也 update()方法用於對資料進行更新 update(String table,ContentValues values, String whereClause, String[] whereArgs)

將要更新的資料新增到ContentValues裡,在通過約束脩改更新資料*

查詢資料

*SQLiteDatabase 中提供了一個 query()方法用於對資料進行查詢

query(String table,查詢的表名

String[] columns,查詢的列名

String selection, 指定 where 的約束條件

String[] selectionArgs,為 where 中的佔位符提供具體的值

String groupBy, 指定需要 group by 的列

String having,對 group by 後的結果進一步約束

String orderBy)指定查詢結果的排序方式

不需要的直接填null*

直接使用SQL執行增刪改查

MySQLite sqlhelper=new MySQLite(this, 1); SQLiteDatabase sd = sqlhelper.getWritableDatabase(); //新增 sd.execSQL(" insert into 表名 values (?,?) ",new String[]{"",""}) //刪除 sd.execSQL(" delete from 表名 where _id=? ",new String[]{""}) //修改(更新) sd.execSQL(" update 表名 set 列更改表示式? where 條件?,new String[]{ "" , "" }) //查詢 sd.rawQuery( "select 列名 from 表名" , null );

*SQLiteDatabase下的execSQL( )方法和rawQuery( )方法

execSQL(String sql, Object[] bindArgs)

rawQuery(String sql,String[] selectionArgs)*

[admin via ] SQLite資料庫相關知識已經有402次圍觀

http://coctec.com/magazine/show-post-item-39.html