Android之SQLite—不同資料庫間複製表

來源:互聯網
上載者:User

需求:

  有兩個不同的SQLite資料庫 A、B,需要將B資料庫中的表複製到A資料庫中去。

準備條件:

安裝好adb。

 原理:

  首先將用sqlite3開啟一個資料庫,然後用attach命令將另一個資料庫引進來,之後就可以就可以用別稱來訪問被引入的資料庫中的表。

案例:


 dictionary中有一個t_words表,我需要將其完整複製到sentences.db中。

操作步驟:

1.首先在終端啟用adb,命令是adb shell,定位到相應的檔案。

2.sqlite3 dictionary,開啟dictionary資料庫。(提前將dictionary的副檔名去掉,不然後面會出一點問題)。

.schema查看建表的命令。

# sqlite3 dictionarySQLite version 3.7.4Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> .schemaCREATE TABLE android_metadata (locale TEXT);CREATE TABLE [t_words] ([english] VARCHAR(30) NOT NULL ON CONFLICT FAIL,[chinese] VARCHAR(100) NOT NULL ON CONFLICT FAIL,CONSTRAINT [english_primary] PRIMARY KEY ([english]));sqlite> 


記錄下t_words建表的命令,後面會用到。


3..q命令退出資料庫,sqlite3 sentences.db進入sentences資料庫,用剛才記錄下的命令建一個dictionary中一模一樣的t_words表,.schema看一下是否成功建立;

4.執行attach database dictionary as dic;之後就可以用dic來引用dictionary資料庫了;

5.執行insert into t_words select * from dic.t_words,等1s,複製完畢。


注意:

1)先建立表再insert而不是直接用

create table t2 as select * from db1.t1;

因為,直接用建立出來的表和原始的表不一樣!

2)sqlite中執行的命令以“;”結束,要記得敲。

參考:http://www.cnblogs.com/nmj1986/archive/2012/09/17/2688827.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.