SQLite學習手冊 資料庫和事務

2022-04-07 18:02:31 字數 1882 閱讀 2828

一、attach資料庫:

attach database語句新增另外乙個資料庫檔案到當前的連線中,如果檔名為":memory:",我們可以將其視為記憶體資料庫,記憶體資料庫無法持久化到磁碟檔案上。如果操作attached資料庫中的表,則需要在表名前加資料庫名,如dbname.table_name。最後需要說明的是,如果乙個事物包含多個attached資料庫操作,那麼該事物仍然是原子的。見如下示例:

sqlite> create table testtable (first_col integer);

sqlite> insert into testtable values(1);

sqlite> .backup 'd:/mydb.db'   --將當前連線中的主資料庫備份到指定檔案。

sqlite> .exit

--重新登入sqlite命令列工具:

sqlite> create table testtable (first_col integer);

sqlite> insert into testtable values(2);

sqlite> insert into testtable values(1);

sqlite> attach database 'd:/mydb.db' as mydb;   

sqlite> .header on            --查詢結果將欄位名作為標題輸出。

sqlite> .mode column        --將每列都分開顯示。

sqlite> select t1.first_col from testtable t1, mydb.testtable t2 where t.first_col = t2.first_col;

first_col

----------

1   

二、detach資料庫:

解除安裝將當前連線中的指定資料庫,注意main和temp資料庫無法被解除安裝。見如下示例:

--該示例承載上面示例的結果,即mydb資料庫已經被attach到當前的連線中。

sqlite> detach database mydb;

sqlite> select t1.first_col from testtable t1, mydb.testtable t2 where t.first_col = t2.first_col;

error: no such table: mydb.testtable

三、事物:

在sqlite中,如果沒有為當前的sql命令(select除外)顯示的指定事物,那麼sqlite會自動為該操作新增乙個隱式的事物,以保證該操作的原子性和一致性。當然,sqlite也支援顯示的事物,其語法與大多數關係型資料庫相比基本相同。見如下示例:

sqlite> begin transaction;

sqlite> insert into testtable values(1);

sqlite> insert into testtable values(2);

sqlite> commit transaction;      --顯示事物被提交,資料表中的資料也發生了變化。

sqlite> select count(*) from testtable;

count(*)

----------

2sqlite> begin transaction;

sqlite> insert into testtable values(1);

sqlite> rollback transaction;  --顯示事物被回滾,資料表中的資料沒有發生變化。

sqlite> select count(*) from testtable;

count(*)

----------

摘自stephen_liu

SQLite學習手冊 資料庫和事務

一 attach資料庫 attach database語句新增另外乙個資料庫檔案到當前的連線中,如果檔名為 memory 我們可以將其視為記憶體資料庫,記憶體資料庫無法持久化到磁碟檔案上。如果操作attached資料庫中的表,則需要在表名前加資料庫名,如dbname.table name。最後需要說...

SQLite學習手冊 資料庫和事務

一 attach資料庫 attach database語句新增另外乙個資料庫檔案到當前的連線中,如果檔名為 memory 我們可以將其視為記憶體資料庫,記憶體資料庫無法持久化到磁碟檔案上。如果操作attached資料庫中的表,則需要在表名前加資料庫名,如dbname.table name。最後需要說...

SQLite學習手冊 資料型別

一 儲存種類和資料型別 sqlite將資料值的儲存劃分為以下幾種儲存型別 null 表示該值為null值。integer 無符號整型值。real 浮點值。text 文字字串,儲存使用的編碼方式為utf 8 utf 16be utf 16le。blob 儲存blob資料,該型別資料和輸入資料完全相同。...