1、什麼是SQLite程序員
SQLite是一款輕型的嵌入式數據庫。web
它佔用資源很是的低,在嵌入式設備中,可能只須要幾百K的內存就夠了。sql
它的處理速度比Mysql、PostgreSQL這兩款著名的數據庫都還快。數據庫
2、什麼是數據庫編程
數據庫(Database)是按照數據結構來組織、存儲和管理數據的倉庫數據結構
數據庫能夠分爲2大種類。spa
關係型數據庫(主流)和 對象型數據庫設計
經常使用關係型數據庫excel
PC端:Oracle、MySQL、SQL Server、Access、DB2、Sybaseorm
嵌入式\移動客戶端:SQLite
3、數據庫是如何存儲數據的
數據庫的存儲結構和excel很像,以表(table)爲單位。
4、數據庫存儲數據的步驟
新建一張表(table)
添加多個字段(column,列,屬性)
添加多行記錄(row.record,每行存放多個字段對應的值)
5、Navicat
Navicat是一款著名的數據庫管理軟件,支持大部分主流數據庫(包括SQLite)
6、如何在程序運行過程當中操做數據庫中的數據
那得先學會使用SQL語句。
SQL(structured query language):結構化查詢語言
SQL是一種對關係型數據庫中的數據進行定義和操做的語言
SQL語言簡潔,語法簡單,好學好用
SQL中的經常使用關鍵字:有select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等
數據庫中不可使用關鍵字來命名錶、字段
7、SQL語句的種類
數據定義語句(DDL:Data Definition Language)
包括create和drop等操做
在數據庫中建立新表或刪除表(create table或 drop table)
2. 數據操做語句(DML:Data Manipulation Language)
包括insert、update、delete等操做
上面的3種操做分別用於添加、修改、刪除表中的數據
3.數據查詢語句(DQL:Data Query Language)
能夠用於查詢得到表中的數據
關鍵字select是DQL(也是全部SQL)用得最多的操做
其餘DQL經常使用的關鍵字有where,order by,group by和having
8、創表
格式:
create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
示例
create table t_student (id integer, name text, age inetger, score real) ;
9、字段類型
SQLite將數據劃分爲如下幾種存儲類型:
1.integer:整型值
2.real: 浮點值
3.text:文本字符串
4.blob:二進制數據(好比文件)
實際上SQLite是無類型的
就算聲明爲integer類型,仍是能存儲字符串文本(主鍵除外)
建表時聲明啥類型或者不聲明類型均可以,也就意味着創表語句能夠這麼寫:
create table t_student(name, age);
爲了保持良好的編程規範、方便程序員之間的交流,編寫建表語句的時候最好加上每一個字段的具體類型
10、刪表
格式:
drop table 表名 ;
drop table if exists 表名 ;
示例
drop table t_student ;
11、插入數據
格式
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
示例
insert into t_student (name, age) values (‘wg’, 10) ;
注意
數據庫中的字符串內容應該用單引號 ‘’ 括住
12、更新數據
格式
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
示例
update t_student set name = ‘jack’, age = 20 ;
注意
上面的示例會將t_student表中全部記錄的name都改成jack,age都改成20
十3、刪除數據
格式
delete from 表名 ;
示例
delete from t_student ;
注意
上面的示例會將t_student表中全部記錄都刪掉
十4、條件語句
若是隻想更新或者刪除某些固定的記錄,那就必須在DML語句後加上一些條件
條件語句的常見格式
where 字段 = 某個值 ; // 不能用兩個 =
where 字段 is 某個值 ; // is 至關於 =
where 字段 != 某個值 ;
where 字段 is not 某個值 ; // is not 至關於 !=
where 字段 > 某個值 ;
where 字段1 = 某個值 and 字段2 > 某個值 ; // and至關於C語言中的 &&
where 字段1 = 某個值 or 字段2 = 某個值 ; // or 至關於C語言中的 ||
十5、DQL語句
格式
select 字段1, 字段2, … from 表名 ;
select * from 表名; // 查詢全部的字段
示例
select name, age from t_student ;
select * from t_student ;
select * from t_student where age > 10 ; // 條件查詢
十6、起別名
格式(字段和表均可以起別名)
select 字段1 別名 , 字段2 別名 , … from 表名 別名 ;
select 字段1 別名, 字段2 as 別名, … from 表名 as 別名 ;
select 別名.字段1, 別名.字段2, … from 表名 別名 ;
示例
select name myname, age myage from t_student ;
給name起個叫作myname的別名,給age起個叫作myage的別名
十7、計算記錄的數量
格式
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
示例
select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;
十8、排序
查詢出來的結果能夠用order by進行排序
select * from t_student order by 字段 ;
select * from t_student order by age ;
默認是按照升序排序(由小到大),也能夠變爲降序(由大到小)
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 ;
先按照年齡排序(升序),年齡相等就按照身高排序(降序)
十9、limit
使用limit能夠精確地控制查詢結果的數量,好比每次只查詢10條數據
格式
select * from 表名 limit 數值1, 數值2 ;
示例
select * from t_student limit 4, 8 ;
能夠理解爲:跳過最前面4條語句,而後取8條記錄
二10、簡單約束
建表時能夠給特定的字段設置一些約束條件,常見的約束有:
not null:規定字段的值不能爲null
unique: 規定字段的值必須惟一
default: 指定字段的默認值
建議:儘可能給字段設定嚴格的約束,以保證數據的規範性)
示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
name字段不能爲null,而且惟一
age字段不能爲null,而且默認爲1
二11、主鍵
主鍵(Primary Key,簡稱PK)用來惟一地標識某一條記錄
主鍵的設計原則:
主鍵應當是對用戶沒有意義的
永遠也不要更新主鍵
主鍵不該包含動態變化的數據
主鍵應當由計算機自動生成
二12、外鍵約束
利用外鍵約束能夠用來創建表與表之間的聯繫
外鍵的通常狀況是:一張表的某個字段,引用着另外一張表的主鍵字段