Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import tables from another database in sqlite?

Tags:

sqlite

I have SQLite databases named database1 with a table t1 and database2 with a table t2. I want to import table t2 from database2 into database1. What command(s) should I use?

like image 783
sachit Avatar asked Nov 03 '12 06:11

sachit


People also ask

How do I import data into SQLite?

First, from the menu choose tool menu item. Second, choose the database and table that you want to import data then click the Next button. Third, choose CSV as the data source type, choose the CSV file in the Input file field, and choose the ,(comma) option as the Field separator as shown in the picture below.

How do I connect two SQLite databases?

Syntax. Following is the basic syntax of SQLite ATTACH DATABASE statement. ATTACH DATABASE 'DatabaseName' As 'Alias-Name'; The above command will also create a database in case the database is already not created, otherwise it will just attach database file name with logical database 'Alias-Name'.

Which of the following command is used to import a CSV file to a SQLite table?

The ". import" command is used to import CSV data into an SQLite table. The ". import" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.


1 Answers

Open database2 with the sqlite3 command-line tool and read the table definition with the command .schema t2. (Alternatively, use any other tool that allows to read the table definition.)

Then open database1, attach the other database with the command:

ATTACH 'database2file' AS db2;

then create the table t2, and copy the data over with:

INSERT INTO t2 SELECT * FROM db2.t2;
like image 96
CL. Avatar answered Sep 28 '22 06:09

CL.