sqlite-(TABLE = = FILE)

Source: Internet
Author: User
Tags sqlite

———————————————————

1. Importing. Import command (FILE ==> TABLE)

———————————————————

Sqlite>.import file TABLE//import data from file to table

----------Example:

Target: Import the data contents from Data.txt into the Table_im table in the Test_database.db database.

    • I'll see what's in the Data.txt text first.

    • Open the database file and import the contents of the Data.txt text into a table

my actions: in the target table Table_im The case is not created,

Result: Executing sqlite>.import data.txt table_im, the first row of the Data.txt data is treated as a table_im field name Col_name, and the data for the subsequent 2~5 row is imported as the field column content Table_ Im The details are as follows (see note):

[Email protected]:~/sqlite_c# sqlite3 test_database.db
SQLite version 3.8.7.4 2014-12-09 01:34:36
Enter '. Help ' for usage hints.
Sqlite>. Table
Company//Start only company This table
sqlite>. Dump
PRAGMA Foreign_keys=off;
BEGIN TRANSACTION;
CREATE TABLE Company (ID INT PRIMARY KEY isn't null, NAME TEXT NOT NULL, age INT not NULL, ADDRESS TEXT, SALARY REAL);
INSERT into "Company" VALUES (1, ' LiA ', +, ' China ', 100000.0);
INSERT into "Company" VALUES (2, ' LiB ', +, ' America ', 200000.0);
INSERT into "Company" VALUES (3, ' LiC ', +, ' earth ', 300000.0);
INSERT into "Company" VALUES (5, ' LIE ', +, ' Ground ', 500000.0);
INSERT into "Company" VALUES (6, ' LiF ', ' 600000.0 ', ' Home ');
COMMIT;
sqlite>. Import data.txt table_im//import operation (before this table_im was not create)
Sqlite>. Table
Company Table_im//Can be seen here, Table_im was a new operation,
sqlite>. Dump//Then I look at how Table_im is created in the database, what is the table structure?
PRAGMA Foreign_keys=off;
BEGIN TRANSACTION;
CREATE TABLE Company (ID INT PRIMARY KEY isn't null, NAME TEXT NOT NULL, age INT not NULL, ADDRESS TEXT, SALARY REAL);
INSERT into "Company" VALUES (1, ' LiA ', +, ' China ', 100000.0);
INSERT into "Company" VALUES (2, ' LiB ', +, ' America ', 200000.0);
INSERT into "Company" VALUES (3, ' LiC ', +, ' earth ', 300000.0);
INSERT into "Company" VALUES (5, ' LIE ', +, ' Ground ', 500000.0);
INSERT into "Company" VALUES (6, ' LiF ', ' 600000.0 ', ' Home ');
CREATE TABLE Table_im (
"1" TEXT,
"LiA" TEXT,
"TEXT",
"China" TEXT,
"100000.0" text//can see that the first row of data.txt data is treated as a table_im field name, and the field type of the table structure is automatically generated, exclusively TEXT type.

);
INSERT into "Table_im" VALUES (' 2 ', ' LiB ', ' + ', ' America ', ' 200000.0 ');
INSERT into "Table_im" VALUES (' 3 ', ' LiC ', ' + ', ' earth ', ' 300000.0 ');
INSERT into "Table_im" VALUES (' 5 ', ' LIE ', ' + ', ' Ground ', ' 500000.0 ');
INSERT into "Table_im" VALUES (' 6 ', ' LiF ', ' I ', ' Home ', ' 600000.0 ');
COMMIT;

Sqlite> select * from Table_im; So the Table_im field starts from the second line of Data.txt.
2| lib|25| america|200000.0
3| lic|30|earth|300000.0
5| lie|40| ground|500000.0
6| lif|45| home|600000.0

So, suggest: create a table, define the table structure, and then import the text file into the table

Correct import Method:

sqlite> CREATE TABLE table_test (ID INT PRIMARY KEY not NULL, NAME TEXT not NUL L, age int. Not NULL, ADDRESS TEXT, SALARY REAL);
Sqlite>. TABLE

Error:unknown command or invalid arguments: "TABLE". Enter '. Help ' for help
Sqlite>. Table
Company Table_test
sqlite>. Import data.txt table_test
sqlite>. Dump Table_test
PRAGMA Foreign_keys=off;
BEGIN TRANSACTION;
CREATE TABLE table_test (ID INT PRIMARY KEY not NULL, NAME TEXT NOT NULL, AG E INT not NULL, ADDRESS TEXT, SALARY REAL);
INSERT into "Table_test" VALUES (1, ' LiA ', +, ' China ', 100000.0);
INSERT into "Table_test" VALUES (2, ' LiB ', +, ' America ', 200000.0);
INSERT into "Table_test" VALUES (3, ' LiC ', +, ' earth ', 300000.0);
INSERT into "Table_test" VALUES (5, ' LIE ', +, ' Ground ', 500000.0);
INSERT into "Table_test" VALUES (6, ' LiF ', A, ' Home ', 600000.0);
COMMIT;

_________________________________________

2. Export. Output command (table==>file)

_________________________________________

Sqlite>.output file_name //send OutputTo FILENAME

----------Example:

[Email protected]:~/sqlite_c# sqlite3 test_database.db
SQLite version 3.8.7.4 2014-12-09 01:34:36
Enter '. Help ' for usage hints.
sqlite>. Output output.txt//locating result set outputs to Output.txt
Sqlite> SELECT * from company; Search output
Sqlite>. Exit
[Email protected]:~/sqlite_c# ls
Backupdb.db gettable mydata.db selectdata.c sqlite3.h.gch
CONNECT_DB.C gettable.c output.txt selectdata_pro.c sqlite_test.c
createtable.c insertdata.c readme.md sqlite3.h test_database.db
[Email protected]:~/sqlite_c# vim output.txt

From the resulting file, the exported data contains the contents of each field, and does not contain the field name!

sqlite-(TABLE = = FILE)

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.