學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

linux的sqlite命令

時(shí)間: 佳洲1085 分享

  linux下的sqlite命令并不是經(jīng)常使用的命令,下面由學(xué)習(xí)啦小編為大家搜集整理了linux的sqlite命令的相關(guān)知識(shí),希望對大家有幫助!

  linux的sqlite命令

  數(shù)據(jù)導(dǎo)入的來源可以是其他應(yīng)用程序的輸出,也可以是指定的文本文件,這里采用指定的文本文件。

  1. 首先,確定導(dǎo)入的數(shù)據(jù)源,這里是待導(dǎo)入的,按固定格式的文本文件。

  2. 然后,依照導(dǎo)入的文件格式,確定想導(dǎo)入的目標(biāo)數(shù)據(jù)表,這個(gè)數(shù)據(jù)表如果沒有,可以依照待導(dǎo)入的文本文件格式,創(chuàng)建一個(gè)相對應(yīng)的數(shù)據(jù)表。

  3. 最后,執(zhí)行.import命令,將文本文件中數(shù)據(jù)導(dǎo)入數(shù)據(jù)表中。

  1. 數(shù)據(jù)源

  在/home/ywx/yu/sqlite/下,創(chuàng)建一個(gè)名為data.txt的文本文件,并輸入以下數(shù)據(jù),數(shù)據(jù)之間采用逗號(hào)隔開

  id,name,age,address,hobby

  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. 目標(biāo)數(shù)據(jù)表

  這里創(chuàng)建一張目標(biāo)數(shù)據(jù)表,通過分析文本格式,這里需要3個(gè)字段,分別是id,name,age。但在數(shù)據(jù)類型選擇時(shí)存在一個(gè)問題,id和age在文本文件中是按字符型存儲(chǔ)的,而其實(shí)際在數(shù)據(jù)表中,最好要表示成整型,因此這里要涉及到一個(gè)字符型數(shù)據(jù)類型向整型數(shù)據(jù)類型轉(zhuǎn)換的問題。

  在創(chuàng)建表時(shí),將id和age的類型定義為整型,進(jìn)行強(qiáng)制轉(zhuǎn)換,如果在數(shù)據(jù)導(dǎo)入時(shí),發(fā)現(xiàn)轉(zhuǎn)換失敗,可以將id和age類型改為文本型。

  ywx@ywx:~/yu/sqlite$ sqlite3 test.db

  SQLite version 3.7.7.1 2011-06-28 17:39:05

  Enter ".help" for instructions

  Enter SQL statements terminated with a ";"

  sqlite> create table data_txt_table(id char(10),name char(10),age char(10),address varchar(15),hobby varchar (15));

  sqlite>

  linux的sqlite命令用法——導(dǎo)入命令

  sqlite> .separator ","

  sqlite> .import data.txt data_txt_table

  sqlite> select * from data_txt_table;

  id,name,age,address,hobby

  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>

  這里需要注意一點(diǎn),在數(shù)據(jù)導(dǎo)入之前,先要根據(jù)數(shù)據(jù)的具體分的格式,設(shè)置數(shù)據(jù)導(dǎo)入的間隔符,例如在文本數(shù)據(jù)中采用的是‘,’來間隔數(shù)據(jù),因此應(yīng)先調(diào)用.seperator 設(shè)置‘,’ 為間隔符。

  linux的sqlite命令用法——查看命令

  .schema 命令來查看指定的數(shù)據(jù)表的結(jié)構(gòu)

  sqlite> .schema data_txt_table

  CREATE TABLE data_txt_table(id char(10),name char(10),age char(10),address varchar(15),hobby varchar (15));

  sqlite>

  .tables 命令用來查看當(dāng)前數(shù)據(jù)庫的所有數(shù)據(jù)表

  sqlite> .tables

  data_txt_table

  sqlite>

  databases 命令用來查看當(dāng)前所有數(shù)據(jù)庫

  sqlite> .databases

  seq name file

  --- --------------- ----------------------------------------------------------

  0 main /home/ywx/yu/sqlite/test.db

  1 temp

  linux的sqlite命令用法——數(shù)據(jù)導(dǎo)出

  數(shù)據(jù)導(dǎo)出也是一個(gè)常用到的操作,可以將指定表中的數(shù)據(jù)導(dǎo)出成SQL腳本,供其他數(shù)據(jù)庫使用,還可以將指定的數(shù)據(jù)表中的數(shù)據(jù)完整定位到標(biāo)準(zhǔn)輸出,也可以將指定數(shù)據(jù)庫中的數(shù)據(jù)完整的導(dǎo)入到另一個(gè)指定數(shù)據(jù)庫等,

  1. 導(dǎo)出成指定的SQL腳本

  將sqlite中指定的數(shù)據(jù)表以SQL創(chuàng)建腳本的形式導(dǎo)出,具體命令

  ywx@ywx:~/yu/sqlite$ sqlite3 test.db

  SQLite version 3.7.7.1 2011-06-28 17:39:05

  Enter ".help" for instructions

  Enter SQL statements terminated with a ";"

  sqlite> .output data.sql

  sqlite> .dump

  sqlite>

  ywx@ywx:~/yu/sqlite$ ll

  總計(jì) 16

  drwxr-xr-x 2 ywx ywx 4096 2011-08-13 23:15 ./

  drwxr-xr-x 7 ywx ywx 4096 2011-08-13 20:53 ../

  -rw-r--r-- 1 ywx ywx 602 2011-08-13 23:17 data.sql

  -rw-r--r-- 1 ywx ywx 2048 2011-08-13 22:44 test.db

  2. 數(shù)據(jù)庫導(dǎo)出

  data.sql test.db

  ywx@ywx:~/yu/sqlite$ sqlite3 test.db ".dump" | sqlite3 test2.db

  ywx@ywx:~/yu/sqlite$ ll

  總計(jì) 20

  drwxr-xr-x 2 ywx ywx 4096 2011-08-13 23:20 ./

  drwxr-xr-x 7 ywx ywx 4096 2011-08-13 20:53 ../

  -rw-r--r-- 1 ywx ywx 602 2011-08-13 23:17 data.sql

  -rw-r--r-- 1 ywx ywx 2048 2011-08-13 23:20 test2.db

  -rw-r--r-- 1 ywx ywx 2048 2011-08-13 22:44 test.db

  3. 其他格式,如:htm格式輸出

  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

3632239