在本文中,我們將了解如何安裝 SQLite(關係型數據庫)及其基本操作。 如果我告訴您 SQLite 的使用量可能超過所有其他數據庫引擎的總和,該怎麼辦? 是的,你沒聽錯。 它是世界上部署最廣泛的數據庫,擁有數百萬和數十億的副本。
這種流行背後的原因是一些 獨特的特徵 這是不尋常的,這使得它不同於許多其他 SQL 數據庫引擎,如 MySQL、PostgreSQL、Oracle、Microsoft SQL Server 等。讓我們開始吧。 首先,我們將在 Linux 上安裝它,然後再介紹基本的數據庫操作。
安裝 Sqlite
要在基於 Debian(Ubuntu、Debian 等)的機器上安裝,請執行以下命令。
$ sudo apt update
$ sudo apt install sqlite3
在基於 RPM 的(RHEL、CentOS、 Fedora 等)機器執行以下命令。
$ sudo yum update
$ sudo yum install sqlite
您還可以使用 DNF 包管理器安裝 SQlite:
sudo dnf update sudo dnf install sqlite
To access SQLite databases from various programming languages (C, Tcl, Java), the language bindings need to be installedsudo dnf install sqlite-devel sqlite-tcl sqlite-jdbc
現在打開一個終端並執行“sqlite3”,您將看到以下帶有提示的行。
$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
第一行顯示了 sqlite3 的版本和發布日期和時間。
第二行告訴輸入“.help”以獲取說明。
。幫助 命令列出所有元命令及其描述。 這些元命令也稱為“點”命令,因為它們前面有一個點。
輸入“.help”提示
sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices ?TABLE? Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY? Load an extension library
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML table code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Use STRING in place of NULL values
.open ?FILENAME? Close existing database and reopen FILENAME
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.print STRING... Print literal STRING
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.stats ON|OFF Turn stats on or off
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.timeout MS Try opening locked tables for MS milliseconds
.trace FILE|off Output each SQL statement as it is run
.vfsname ?AUX? Print the name of the VFS stack
.width NUM1 NUM2 ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off
在解釋這些點命令之前,讓我們先看看一些基本的數據庫操作。
首先運行“.quit”命令結束會話。
創建數據庫
在 shell 提示符下執行以下命令。
它將創造“example.db”(您可以將您選擇的名稱代替“example.db”) 數據庫文件(如果不存在)。如果存在,則打開文件中包含的數據庫。
$ sqlite3 example.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
“sqlite3”和“sqlite3 dbname.db”命令之間的區別在於第一個命令將為會話創建一個臨時數據庫,並在會話關閉時過期。
創建表
讓我們創建一個帶有屬性的學生表:
reg_no int(4)
名稱 varchar(20)
標記 int(3)
在終端輸入以下命令
sqlite> create table student (
...> reg_no int(4),
...> name varchar(20),
...> marks int(3)
...> );
它將創建一個具有上述屬性的學生表。
您可以通過以下方式查看所選數據庫中的表列表 。桌子 命令
sqlite> .table
student
在我的情況下只有一名學生。
您還可以使用查看表的架構 .schema 表名
sqlite> .schema student
CREATE TABLE student (
reg_no int(4),
name varchar(20),
marks int(3)
);
插入數據
讓我們在表中插入 3 條記錄
101, 普拉迪普, 87
102,阿維納什,86
103,拉克什,90
Enter 下面的命令一一
sqlite> insert into student (reg_no, name, marks) values (101, 'Pradip', 87);
> insert into student (reg_no, name, marks) values (102, 'Avinash', 86);
> insert into student (reg_no, name, marks) values (103, 'Rakesh', 91);
獲取數據
您可以使用 select 語句從表中獲取數據
sqlite> select * from student;
101|Pradip|87
102|Avinash|86
103|Rakesh|91
它以默認模式(列表)顯示結果。
你可以改變模式 。模式 命令
Enter .mode 列提示然後執行選擇查詢。 它將以列格式顯示結果。
sqlite> .mode column
> select * from student;
reg_no name marks
---------- ---------- ----------
101 Pradip 87
102 Avinash 86
103 Rakesh 91
Delete、Alter、Drop 等具有與 sql 相同的語法。
sqlite3 的特殊命令(dot -commands)
我們看到“.help”命令列出了所有的點命令。 讓我們了解一些其他重要的命令。
1) .databases
顯示所選數據庫的名稱。
sqlite> .database
seq name file
--- --------------- --------------------
0 main /home/avi/example.db
- 。桌子
列出所選數據庫中的表。
sqlite> .table
student
2) .show
顯示當前設置
sqlite> .show
echo: off
explain: off
headers: off
mode: list
nullvalue: ""
output: stdout
separator: "|"
stats: off
width:
3) .header ON|OFF
您可以通過運行 .header ON|OFF 命令在查詢結果中顯示屬性名稱
sqlite> .header ON
> select * from student;
reg_no name marks
---------- ---------- ----------
101 Pradip 87
102 Avinash 86
103 Rakesh 91
4).模式
該程序能夠以八種不同的格式顯示查詢結果:“csv”、“column”、“html”、“insert”、“line”、“list”、“tabs”、“tcl”..mode命令用於在這些輸出格式之間切換。
sqlite> .mode csv
> select * from student;
reg_no,name,marks
101,Pradip,87
102,Avinash,86
103,Rakesh,91
您可以使用 。分隔器 命令更改分隔符。
sqlite> .separator –
> 從學生中選擇 *;
reg_no-name-marks
101-Pradip-87
102-Avinash-86
103-拉克什-91
將結果寫入文件
默認情況下,它將查詢結果發送到標準輸出。
您可以使用“.output”和“.once”命令更改此設置。
只需將輸出文件的名稱作為 .output 的參數,所有後續查詢結果都將寫入該文件。
sqlite> .output ex.txt
或者使用 。一次 如果您只想重定向下一個查詢的結果,請使用帶有文件名的命令。
我們已經通過基本操作在 Linux 上成功安裝了 SQLite。 這些操作只是其中的一小部分 全部可用. 我們無法在本文中涵蓋所有這些。 如果您在安裝或任何命令中發現任何困難,請在評論部分告訴我。