The data import source can be output by other applications or a specified text file. The specified text file is used here.
1. First, determine the data source to be imported, which is a text file in a fixed format.
2. Then, determine the target data table to be imported according to the imported file format. If the data table does not exist, you can create a corresponding data table according to the text file format to be imported.
3. Finally, run the. Import command to import data from the text file to the data table.
1. Data Source
Create a text file named data.txt under/home/ywx/YU/SQLite/, and enter the following data, separated by commas
- ID, name, age, address, holobby
- 1, Tom, 24, Beijing, football
- 2, Liu, 27, Heibei, Fotball
- 3, Jim, 26, Shandong, football
- 4, Han, 28, Beijing, football
- 5, Meng, 25, Beijing, tennis
2. Target data table
Create a target data table and analyze the text format. Three fields are required: ID, name, and age. However, there is a problem when selecting the data type. The ID and age are stored in the text file according to the sequence type, and it is best to represent an integer in the actual data table, therefore, the conversion from the dense data type to the integer data type is involved.
When creating a table, define the ID and age types as integer type for forced conversion. If the conversion fails during data import, you can change the ID and age types to text type.
- Ywx @ ywx :~ /YU/SQLite $ sqlite3 test. DB
- SQLite version 3.7.7.1 17:39:05
- Enter ". Help" for instructions
- Enter SQL statements terminated with ";"
- SQLite> Create Table data_txt_table (ID char (10), name char (10), age char (10), address varchar (15), holobby varchar (15 ));
- SQLite>
3. Import command
- SQLite>. separator ","
- SQLite>. Import data.txt data_txt_table
- SQLite> select * From data_txt_table;
- ID, name, age, address, holobby
- 1, Tom, 24, Beijing, football
- 2, Liu, 27, Heibei, Fotball
- 3, Jim, 26, Shandong, football
- 4, Han, 28, Beijing, football
- 5, Meng, 25, Beijing, tennis
- SQLite>
Note that before importing data, you must set the data import delimiter based on the specific shard format. For example, if you use ', 'To separate data, so you should call it first. seperator setting ',' is the delimiter.
2. View commands
. Schema command to view the structure of the specified data table
- SQLite>. schema data_txt_table
- Create Table data_txt_table (ID char (10), name char (10), age char (10), address varchar (15), holobby varchar (15 ));
- SQLite>
2. The tables command is used to view all data tables of the current database.
- SQLite>. Tables
- Data_txt_table
- SQLite>
3. The databases command is used to view all current databases.
- SQLite>. Databases
- SEQ name file
- ----------------------------------------------------------------------------
- 0 main/home/ywx/YU/SQLite/test. DB
- 1 temp
3. Export data
Data export is also a common operation. You can export data from a specified table to an SQL script for use by other databases. You can also completely locate the data in the specified data table to the standard output, you can also import data from a specified database to another specified database,
1. Export to the specified SQL script
Export the specified data table in SQLite in the form of an SQL creation script. Specific commands
- Ywx @ ywx :~ /YU/SQLite $ sqlite3 test. DB
- SQLite version 3.7.7.1 17:39:05
- Enter ". Help" for instructions
- Enter SQL statements terminated with ";"
- SQLite>. output data. SQL
- SQLite>. Dump
- SQLite>
- Ywx @ ywx :~ /YU/SQLite $ LL
- Total 16
- Drwxr-XR-x 2 ywx 4096 ./
- Drwxr-XR-x 7 ywx 4096 ../
- -RW-r -- 1 ywx 602 data. SQL
- -RW-r -- 1 ywx 2048 test. DB
2. Export the database
- Data. SQL test. DB
- Ywx @ ywx :~ /YU/SQLite $ sqlite3 test. DB ". Dump" | sqlite3 test2.db
- Ywx @ ywx :~ /YU/SQLite $ LL
- Total 20
- Drwxr-XR-x 2 ywx 4096 ./
- Drwxr-XR-x 7 ywx 4096 ../
- -RW-r -- 1 ywx 602 data. SQL
- -RW-r -- 1 ywx 2048 test2.db
- -RW-r -- 1 ywx 2048 test. DB
3. Other formats, such as HTM output
- Ywx @ ywx :~ /YU/SQLite $ sqlite3-HTML test. DB "select * From data_txt_table"> liu.htm
- Ywx @ ywx :~ /YU/SQLite $ ls
- Data. SQL liu.htm test2.db test. DB
- Http://blog.chinaunix.net/uid-22666248-id-2182334.html