文章來(lái)源于:https://blog.csdn.net/weixin_36481059/article/details/114470353
CentOS系統(tǒng)就默認(rèn)安裝了SQLite3
打開(kāi)終端,輸入以下命令可以查看SQLite的版本信息:
[istester@ietester.com idoxu]$ sqlite3 -version
3.6.20
也可以指定一個(gè)完整文件的路徑名壮虫,打開(kāi)或者創(chuàng)建數(shù)據(jù)庫(kù)(文件不存在,則創(chuàng)建),同時(shí)進(jìn)入sqlite后臺(tái)操作程序
$ sqlite3 istester.db
sqlite>
sqlite> 表示在sqlite里面了,常用的sqlite命令如下:
1该押、查看數(shù)據(jù)庫(kù)信息:
sqlite>.database
2穴店、查看所有表:
sqlite>.table
3、查看所有表的創(chuàng)建語(yǔ)句:
sqlite>.schema
4骑篙、查看某個(gè)表的創(chuàng)建語(yǔ)句:
sqlite>.schema table_name
5、最重要的一個(gè)命令森书,執(zhí)行sql語(yǔ)句(必須以分號(hào)結(jié)尾靶端,不加分號(hào)就敲回車(chē)則是分行)
sqlite>select * from table_name;
6、退出SQLite
sqlite>.quit
補(bǔ)充一些常用的命令:
1)建立數(shù)據(jù)表
create table table_name(field1 type1, field2 type1, ...);
table_name是要?jiǎng)?chuàng)建數(shù)據(jù)表名稱(chēng)凛膏,fieldx是數(shù)據(jù)表內(nèi)字段名稱(chēng)杨名,typex則是字段類(lèi)型。
例猖毫,建立一個(gè)簡(jiǎn)單的學(xué)生信息表台谍,它包含學(xué)號(hào)與姓名等學(xué)生信息:
create table student_istester(stu_no interger primary key, name text);
2)添加數(shù)據(jù)記錄
insert into table_name(field1, field2, ...) values(val1, val2, ...);
valx為需要存入字段的值。
例吁断,往學(xué)生信息表添加數(shù)據(jù):
Insert into student_istester(stu_no, name) values(0001, alex);
3)修改數(shù)據(jù)記錄
update table_name set field1=val1, field2=val2 where expression;
where是sql語(yǔ)句中用于條件判斷的命令趁蕊,expression為判斷表達(dá)式
例坞生,修改學(xué)生信息表學(xué)號(hào)為0001的數(shù)據(jù)記錄:
update student_istester set stu_no=0001, name=hence where stu_no=0001;
4)刪除數(shù)據(jù)記錄
delete from table_name [where expression];
不加判斷條件則清空表所有數(shù)據(jù)記錄。
例掷伙,刪除學(xué)生信息表學(xué)號(hào)為0001的數(shù)據(jù)記錄:
delete from student_istester where stu_no=0001;
5)查詢數(shù)據(jù)記錄
select指令基本格式:
select columns from table_name [where expression];
a查詢輸出所有數(shù)據(jù)記錄
select * from table_name;
b限制輸出數(shù)據(jù)記錄數(shù)量
select * from table_name limit val;
c升序輸出數(shù)據(jù)記錄
select * from table_name order by field asc;
d降序輸出數(shù)據(jù)記錄
select * from table_name order by field desc;
e條件查詢
select * from table_name where expression;
select * from table_name where field in ('val1', 'val2', 'val3');
select * from table_name where field between val1 and val2;
f查詢記錄數(shù)目
select count (*) from table_name;
g區(qū)分列數(shù)據(jù)
select distinct field from table_name;
有一些字段的值可能會(huì)重復(fù)出現(xiàn)是己,distinct去掉重復(fù)項(xiàng),將列中各字段值單個(gè)列出任柜。
6)建立索引
當(dāng)說(shuō)數(shù)據(jù)表存在大量記錄赃泡,索引有助于加快查找數(shù)據(jù)表速度。
create index index_name on table_name(field);
例乘盼,針對(duì)學(xué)生表stu_no字段升熊,建立一個(gè)索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在對(duì)該字段查詢時(shí)绸栅,會(huì)自動(dòng)使用該索引级野。
7)刪除數(shù)據(jù)表或索引
drop table table_name;
drop index index_name;
————————————————
版權(quán)聲明:本文為CSDN博主「墻角的吉他」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議粹胯,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明蓖柔。
原文鏈接:https://blog.csdn.net/weixin_36481059/article/details/114470353