溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SQLite3 sql命令行如何使用

發(fā)布時間:2020-09-25 11:44:19 來源:億速云 閱讀:161 作者:小新 欄目:MySQL數(shù)據(jù)庫

小編給大家分享一下SQLite3 sql命令行如何使用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

SQLite是一個輕量級的SQL數(shù)據(jù)庫,它實現(xiàn)了一個獨立的、無服務(wù)器的、零配置的事務(wù)性SQL數(shù)據(jù)庫引擎。除了一些命令外,sqlite使用的命令語法與mysql、oracle使用的類似,本篇文章將介紹如何使用命令行來使用sqlite數(shù)據(jù)庫。

1、創(chuàng)建SQLite數(shù)據(jù)庫

SQLite提供了一個簡單的命令來創(chuàng)建數(shù)據(jù)庫。使用以下命令創(chuàng)建sqlite數(shù)據(jù)庫。

# sqlite3 admin.db

基本上,sqlite數(shù)據(jù)庫是在當前工作目錄中創(chuàng)建的文件。

# ls -l admin.db
-rw-r--r--. 1 root root 3072 May 11 14:32 admin.db

2.在SQLite數(shù)據(jù)庫中創(chuàng)建表

創(chuàng)建數(shù)據(jù)庫后,我們創(chuàng)建表。使用以下查詢在數(shù)據(jù)庫admin.db中創(chuàng)建兩個表(users, posts )。

# sqlite3 admin.db
sqlite> create table users(uid integer,uname varchar(60),category varchar(50));
sqlite> create table posts(postid integer,postname varchar(50),content varchar(1000));
sqlite> create table tmp(id integer,tname varchar(50);
sqlite> .quit

3.在SQLite中列出或刪除表

要僅在SQLite數(shù)據(jù)庫中列出表名,只需使用以下命令。

sqlite> .tables
posts  tmp    users

如果需要刪除任何表,可以使用以下命令執(zhí)行此操作,如下所示。

#drop table <tablename>; 
#drop table if exists <tablename>;
#drop table tmp; 
#drop table if tmp;

4.在表格中插入數(shù)據(jù)

以下命令用于通過SQLite提示在SQLite數(shù)據(jù)庫中插入數(shù)據(jù)。

sqlite> INSERT INTO posts VALUES(1, 'Post 1','this is demo post 1');
sqlite> INSERT INTO posts VALUES(2, 'Post 2','this is demo post 2');
sqlite> INSERT INTO users VALUES(1,'Harry','staff');
sqlite> INSERT INTO users VALUES(2,'Rahul','Admin');

還可以執(zhí)行文件中包含的一組命令。

# vi data.sql
INSERT INTO posts VALUES(10, 'Sample Post 10','this is sample post 10');
INSERT INTO posts VALUES(11, 'Sample Post 11','this is sample post 11');
INSERT INTO users VALUES(10,'Sarah','Support');
INSERT INTO users VALUES(11,'Nick','Sales');

以下命令將執(zhí)行admin.db數(shù)據(jù)庫中data.sql的所有命令。

# sqlite3 admin.db < data.sql

5.從表中獲取數(shù)據(jù)

使用SELECT命令查看SQLite數(shù)據(jù)庫中表的數(shù)據(jù),如下例所示。

sqlite> SELECT * FROM users;
1|Harry|staff
2|Rahul|Admin
10|Sarah|Support
11|Nick|Sales

sqlite> SELECT * FROM posts;
1|Post 1|this is demo post 1
2|Post 2|this is demo post 2
10|Sample Post 10|this is sample post 10
11|Sample Post 11|this is sample post 11

sqlite> SELECT * FROM posts WHERE postid = 1;
1|Post 1|this is demo post 1

6.更改輸出格式

SQLite3以八種不同的格式顯示查詢結(jié)果:“csv”,“column”,“html”,“insert”,“l(fā)ine”,“l(fā)ist”,“tabs”和“tcl”。使用“.mode”命令可以更改輸出格式。默認輸出格式為“l(fā)ist”。

sqlite> .mode line
sqlite> select * from users;
     uid = 1
   uname = Harry
category = staff

     uid = 2
   uname = Rahul
category = Admin
sqlite> .mode column
sqlite> select * from users;
1           Harry       staff
2           Rahul       Admin
10          Sarah       Support
11          Nick        Sales

7.將SQLite數(shù)據(jù)庫轉(zhuǎn)換為ASCII文本文件

可以使用“.dump”命令將SQLite數(shù)據(jù)庫簡單地轉(zhuǎn)換為純文本文件。使用以下命令執(zhí)行。

# sqlite3 admin.db '.dump' > backup.dump

要從ASCII文件backup.dump重建SQLite數(shù)據(jù)庫,只需輸入:

#cat backup.dump | sqlite3 admin-1.db

以上是SQLite3 sql命令行如何使用的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI