共計 2345 個字符,預(yù)計需要花費 6 分鐘才能閱讀完成。
本篇文章為大家展示了 SQLlite 數(shù)據(jù)庫中的附加和分離是怎樣的,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
在 SQLlite 數(shù)據(jù)庫中往往一個數(shù)據(jù)文件就是一個 schema,但是在平時的業(yè)務(wù)或者是一些條件中可能是不同的內(nèi)容存放在不同的 schema 中,即不同的數(shù)據(jù)文件,有的場景下需要數(shù)據(jù)關(guān)聯(lián)時就可以使用 SQLlite 的數(shù)據(jù)附加來建立一個臨時的鏈接。如下,在使用 my_test 的 schema 時需要關(guān)聯(lián)查詢一個為 my_test2 的 schema 就可以使用附加:
[root@localhost data]# sqlite3 my_test.db # 在 SQLlite 數(shù)據(jù)庫中缺省 database 名為 main SQLite version 3.6.20 Enter .help for instructions Enter SQL statements terminated with a sqlite .database seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db sqlite ATTACH DATABASE /data/my_test2.db As my_test2 # 在當(dāng)前 schema 下附加上 /data/my_test2.db 中的數(shù)據(jù),并且起一個別名為 my_test2,當(dāng)然也可以起其他的名字 sqlite .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db 2 my_test2 /data/my_test2.db sqlite CREATE TABLE my_test2.test_attach ( ... a int(10), ... b int(10) ... ); sqlite SELECT * FROM my_test2.sqlite_master WHERE type = table AND tbl_name = test_attach # 直接在當(dāng)前 schema 下使用 /data/my_test2.db 中的數(shù)據(jù),并且查看 table|test_attach|test_attach|4|CREATE TABLE test_attach ( a int(10), b int(10) ) sqlite .exit [root@localhost data]# sqlite3 /data/my_test2.db # 切換成 my_test2.db 的 schema 查看驗證下 SQLite version 3.6.20 Enter .help for instructions Enter SQL statements terminated with a sqlite SELECT sql FROM sqlite_master WHERE type = table AND tbl_name = test_attach CREATE TABLE test_attach ( a int(10), b int(10) )
如此就是在 SQLlite 數(shù)據(jù)庫中的附加數(shù)據(jù)庫,它其實是一個鏈接,用于在不同的數(shù)據(jù) schma 數(shù)據(jù)文件下使用其他的 schma 數(shù)據(jù)文件,在這里需要注意的是目前在 SQLlite 數(shù)據(jù)庫中附加是臨時的,在當(dāng)前 session 中創(chuàng)建一個鏈接,如果在退出這個 session 后附加就自動分離:
[root@localhost data]# sqlite3 /data/my_test.db SQLite version 3.6.20 Enter .help for instructions Enter SQL statements terminated with a sqlite .database seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db 當(dāng)然有如果有附件數(shù)據(jù)庫那一定有分離,分離就比較簡單: sqlite .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db 2 my_test2 /data/my_test2.db sqlite DETACH DATABASE my_test2 sqlite .databases seq name file --- --------------- ---------------------------------------------------------- 0 main /data/my_test.db
這樣就成功的主動分離附加在當(dāng)前 schma 下的其他數(shù)據(jù)文件,在這里要特別注意的是如果分離的數(shù)據(jù)庫是在內(nèi)存或臨時空間內(nèi),分離后會銷毀其分離的數(shù)據(jù)。
上述內(nèi)容就是 SQLlite 數(shù)據(jù)庫中的附加和分離是怎樣的,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。