SQLite for Android-copying tables between different databases

Source: Internet
Author: User

Requirements:

There are two different SQLite databases A and B. You need to copy the tables in database B to database.

Prerequisites:

Install ADB.

Principle:

First, you will use sqlite3 to open a database, and then use the attach command to import another database. Then, you can access the tables in the introduced database with a special name.

Case:


Dictionary has a t_words table. I need to copy it to sentences. DB.

Procedure:

1. First enable ADB on the terminal. The command is ADB shell and the corresponding file is located.

2. sqlite3 dictionary: Open the dictionary database. (Remove the dictionary extension in advance, otherwise there will be a problem later ).

. Schema command to view the table creation.

 
# 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>


Record the command for creating t_words tables, which will be used later.


3. Run the. Q command to exit the database, and sqlite3 sentences. dB to enter the sentences database. Use the command just recorded to create a t_words table identical to the dictionary. schema to check whether the table is successfully created;

4. Execute attach database dictionary as DIC. Then you can use DIC to reference the dictionary database;

5. Execute insert into t_words select * from DIC. t_words, wait for 1 S, and the copy is complete.


Note:

1) create a table first and then insert it instead of directly using

 
Create Table T2 as select *From db1.t1;

Because the directly created table is different from the original table!

2) The command executed in SQLite ends with ";". Remember to repeat it.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.