SQLite基礎(chǔ)知識(shí)

一、SQLite簡(jiǎn)介:

SQLite是一款嵌入式的輕量關(guān)系型數(shù)據(jù)庫。

它占用資源非常低。在嵌入式設(shè)備中,可能只需要幾百K內(nèi)存;

處理速度比Mysql、PostgreSQL這兩款開源的世界著名數(shù)據(jù)庫管理系統(tǒng)還要快。

二、SQL語句:

SQL是專門操作數(shù)據(jù)庫的一種特定的語言;

SQL語句不區(qū)分大小寫;

在程序運(yùn)行中,想要操作(增刪改查,CRUD)數(shù)據(jù)庫,必須使用SQL語句。

1、數(shù)據(jù)定義語句(DDL: Data Definition Language):

1.1 創(chuàng)建表:

create table if not exists t_student (id integer primary key autoincrement,?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name varchar(128),?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? age integer,?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? class integer default 0,?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? register_time date time,?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? money float default 0,?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? birthday date);

整數(shù)數(shù)據(jù)類型:

integer: 整型數(shù)據(jù),大小為4個(gè)字節(jié);

bigint: 整型數(shù)據(jù),大小為8個(gè)字節(jié);

smallint: 整型數(shù)據(jù),大小為2個(gè)字節(jié);

tinyint: 從0到255的整數(shù)數(shù)據(jù),存儲(chǔ)大小為1個(gè)字節(jié);

float: 4字節(jié)浮點(diǎn)型;

double: 8字節(jié)浮點(diǎn)型;

real: 8字節(jié)浮點(diǎn)型。

字符串?dāng)?shù)據(jù)類型:

char(n): n長(zhǎng)度的字符串,n不超過254;

varchar(n): 長(zhǎng)度不固定且最大長(zhǎng)度為n的字符串,n不超過4000;

text: 存儲(chǔ)可變長(zhǎng)的unicode數(shù)據(jù),存更大字符串。

日期類型:

date: 包含年月日;

time: 包含時(shí)分秒;

datetime: 包含年月日時(shí)分秒,格式:'2016-05-25 11:11:11';

timestamp: 包含年月日時(shí)分秒和毫秒。

所有字符串和日期需要加單引號(hào)。

其他類型

null: 空值;

blob: 二進(jìn)制數(shù)據(jù);

default: 缺省值;

not null: 規(guī)定字段的值不能為空

unique:規(guī)定字段的值必須唯一

primary key: 主鍵;

autoincrement: 主鍵自動(dòng)增長(zhǎng)。

外鍵約束:

當(dāng)學(xué)生表中有個(gè)class_id字段時(shí),class_id字段的內(nèi)容應(yīng)該對(duì)應(yīng)班級(jí)表中的id字段,不能隨意填寫,這時(shí),應(yīng)當(dāng)把學(xué)生表中的class_id字段設(shè)置為外鍵

create table if not exists t_student (id integer primary key autoincrement, name varchar(128), class_id integer,

constraint fk_t_student_class_id_t_class_id foreign key (class_id) references t_class(id));

1.2 刪除表:

drop table if exists t_student;

2、數(shù)據(jù)操作語句(DML: Data Manipulation Language):

添加記錄(insert):

insert into t_student (name, age) values ('Jack', 20);

修改記錄(update):

update t_student set name = 'Rose', age = 18 where id = 1;

刪除記錄(delete):

delete from t_student; // 刪除所有記錄

delete from t_student where id = 2;

3、數(shù)據(jù)查詢語句(DQL: Data Query Language):

查詢記錄(select):

select * from t_student; // 查詢所有記錄

select name, age from t_student;

select name from t_student where id > 10 and age < 20;

select * from t_student where name like '%Ja%'; // 模糊查詢,查詢所有名字中含有 Ja 的記錄

起別名:

select name myname, age myage from t_student;

select s.name, s.age from t_student s;

select統(tǒng)計(jì)語句:

select count(*) from t_student; // 統(tǒng)計(jì)記錄的條數(shù)

select avg(age) from t_student: // 年齡的平均值

select sum(age) from t_student: // 年齡的和

排序:

select * from t_student order by age; // 根據(jù)年齡排序,默認(rèn)升序

select * from t_student order by age desc; // 降序

select * from t_student order by age asc; // 升序

select * from t_student order by age asc, height desc; // 先按年齡升序,年齡相同時(shí)按身高降序

limit(分頁查詢):

select * from t_student limit 4, 8; // 從第4條開始,查詢8條記錄。(記錄從第0條開始)

多表查詢:

select s.name, s.age from t_student s, t_class c where s.class_id = c.id and c.name = 'iOS';

iOS中sqlite的基本用法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評(píng)論 18 399
  • iOS中的數(shù)據(jù)存儲(chǔ)方式 Plist(NSArray\NSDictionary) Preference(偏好設(shè)置\N...
    JonesCxy閱讀 686評(píng)論 0 3
  • iOS中的數(shù)據(jù)存儲(chǔ)方式 Plist(NSArray\NSDictionary),只能存儲(chǔ)數(shù)組,字典,但是數(shù)組和字典...
    AlanGe閱讀 587評(píng)論 0 0
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,835評(píng)論 0 11
  • iphone"此證書是由未知頒發(fā)機(jī)構(gòu)簽名的"的解決辦法 由于誤刪除,將開發(fā)證書給弄沒了,導(dǎo)致Certificate...
    ice_backy閱讀 177評(píng)論 0 0