在linux下使用sqlite

2022-04-02 11:15:42 字數 3727 閱讀 8595

資料匯入的**可以是其他應用程式的輸出,也可以是指定的文字檔案,這裡採用指定的文字檔案。

1. 首先,確定匯入的資料來源,這裡是待匯入的,按固定格式的文字檔案。

2. 然後,依照匯入的檔案格式,確定想匯入的目標資料表,這個資料表如果沒有,可以依照待匯入的文字檔案格式,建立乙個相對應的資料表。

3. 最後,執行.import命令,將文字檔案中資料匯入資料表中。

1. 資料來源

在/home/ywx/yu/sqlite/下,建立乙個名為data.txt的文字檔案,並輸入以下資料,資料之間採用逗號隔開

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. 目標資料表

這裡建立一張目標資料表,通過分析文字格式,這裡需要3個字段,分別是id,name,age。但在資料型別選擇時存在乙個問題,id和age在文字檔案中是按字元型儲存的,而其實際在資料表中,最好要表示成整型,因此這裡要涉及到乙個字元型資料型別向整型資料型別轉換的問題。

在建立表時,將id和age的型別定義為整型,進行強制轉換,如果在資料匯入時,發現轉換失敗,可以將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>

3. 匯入命令

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>

這裡需要注意一點,在資料匯入之前,先要根據資料的具體分的格式,設定資料匯入的間隔符,例如在文字資料中採用的是『,』來間隔資料,因此應先呼叫.seperator 設定『,』 為間隔符。

2. 檢視命令

.schema 命令來檢視指定的資料表的結構

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>

2. .tables 命令用來檢視當前資料庫的所有資料表

sqlite>

.tables

data_txt_table

sqlite>

3. databases 命令用來檢視當前所有資料庫

sqlite>

.databases

seq name file --

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

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

1 temp

3. 資料匯出

資料匯出也是乙個常用到的操作,可以將指定表中的資料匯出成sql指令碼,供其他資料庫使用,還可以將指定的資料表中的資料完整定位到標準輸出,也可以將指定資料庫中的資料完整的匯入到另乙個指定資料庫等,

1. 匯出成指定的sql指令碼

將sqlite中指定的資料表以sql建立指令碼的形式匯出,具體命令

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

總計 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. 資料庫匯出

data.sql test.db

ywx@ywx:

~/yu/sqlite$ sqlite3 test.db ".dump"

| sqlite3 test2.db

ywx@ywx:

~/yu/sqlite$ ll

總計 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

SQLite在VC下的使用

一 sqlite簡介 sqlite 是用c語言編寫的開源資料庫,主要用於嵌入式,你也可以把它整合在自己的桌面程式中,也有人將其替代access,用作後台資料庫。sqlite 支援多數sql92標準,例如 索引 限制 觸發和檢視支援。支援 null integer real text 和 blob 資...

SQLite在VC下的使用

一 sqlite簡介 sqlite 是用c語言編寫的開源資料庫,主要用於嵌入式,你也可以把它整合在自己的桌面程式中,也有人將其替代access,用作後台資料庫。sqlite 支援多數sql92標準,例如 索引 限制 觸發和檢視支援。支援 null integer real text 和 blob 資...

SQLite在VC下的使用

自 http hi.baidu.com yeetoo blog item 2fde8813346efe836538db87.html 在vc中使用sqlite的例子 2007年09月26日 星期三 14 06 我打算在ponyse上把sqlite做為第乙個儲存 轉換資料 的資料庫,所以今天小試了一把...