 嵌入式(shi)數據(ju)庫是什么
							時間:2018-02-23      來源:未知
							嵌入式(shi)數據(ju)庫是什么
							時間:2018-02-23      來源:未知 
							SQLite,是(shi)一款輕型的(de)數(shu)(shu)據(ju)(ju)(ju)庫(ku)(ku),其設計目(mu)標是(shi)嵌入(ru)式(shi)的(de)數(shu)(shu)據(ju)(ju)(ju)庫(ku)(ku),而(er)且(qie)當前在很多嵌入(ru)式(shi)產品中(zhong)使用(yong)到了sqlite數(shu)(shu)據(ju)(ju)(ju)庫(ku)(ku),sqlite數(shu)(shu)據(ju)(ju)(ju)庫(ku)(ku)占用(yong)資源非常的(de)低(di),對嵌入(ru)式(shi)設備而(er)言,內(nei)存是(shi)非常寶(bao)貴的(de),而(er)sqlite數(shu)(shu)據(ju)(ju)(ju)庫(ku)(ku)可能只(zhi)需要幾(ji)百(bai)K的(de)內(nei)存就夠了。
Sqlite數據(ju)庫(ku)還能夠(gou)支(zhi)持Windows/Linux/Unix等等主流的(de)操作系統,其跨(kua)平(ping)臺的(de)可(ke)移植性(xing)特(te)別強,這極大的(de)拓展(zhan)了其生存的(de)空間。
同時(shi)能夠跟很多程(cheng)序語言相結合,比如C#、PHP、Java,C等,還有ODBC接(jie)口,同樣比起Mysql、PostgreSQL這兩款開(kai)源的世界著名數據(ju)(ju)庫(ku)管理系統(tong)來講,它的處理速度比他們都(dou)快,比起Oracle數據(ju)(ju)庫(ku)來說(shuo),免費也是極大的優勢。
SQLite第一個(ge)Alpha版本(ben)誕生于2000年(nian)(nian)5月,至2018年(nian)(nian)已經(jing)有18個(ge)年(nian)(nian)頭, SQLite 3也已經(jing)發布,并成功的進入了(le)我們(men)的事業。
除(chu)了上(shang)面提到的低內存占用率、跨(kua)平(ping)臺可移植性、多語言支持、免(mian)費開(kai)源,sqlite3數(shu)(shu)據(ju)庫還有(you)強大的功能,比如支持數(shu)(shu)據(ju)庫大小(xiao)可達2TB,十(shi)多萬行的代碼非常適合學習,良好的代碼注釋等(deng)等(deng)。
那么我(wo)們學(xue)習數據(ju)庫好可以快(kuai)速的(de)(de)入(ru)門(men),在門(men)外徘(pai)徊太久的(de)(de)話會挫傷我(wo)們學(xue)習的(de)(de)積(ji)極性。如果(guo)你是一個嵌入(ru)式的(de)(de)學(xue)習和開發者(zhe),那么從以下兩(liang)個方(fang)面出發可以幫你快(kuai)速的(de)(de)上(shang)手sqlite3數據(ju)庫:
1、Sqlite3的命令 :數據庫的創(chuang)建及(ji)增刪(shan)改查(cha)
2、2、sqlite3 API函數(shu)接口(kou):用API接口(kou)實現(xian)數(shu)據庫的創建及(ji)增(zeng)刪改(gai)查
當然了,以下(xia)是(shi)基(ji)于Ubuntu平臺做(zuo)的測(ce)試:
1-- 安裝數(shu)據庫:
在線安(an)裝:sudo apt-get install sqlite sqlite3
sudo apt-get install libsqlite3-dev
sudo apt-get install sqlitebrowser
離線安裝:
下(xia)載安裝下(xia)面三個包:
libsqlite3-0_3.7.2-1ubuntu0.1_i386.deb
libsqlite3-dev_3.7.2-1ubuntu0.1_i386.deb
sqlite3_3.7.2-1ubuntu0.1_i386.deb
sudo dpkg -i *.deb (數(shu)據庫安(an)裝文(wen)件)
2、Sqlite3的(de)命(ming)令
2.1、創建數據(ju)庫
sqlite3 stu.db
必須(xu)指定數(shu)據(ju)庫名字(zi)
2.2、sqlite命令
系統(tong)命令 以 "."開(kai)頭(tou)
普(pu)通命令 ,以";"結束(shu)
.schema 查(cha)看表(biao)的(de)結構
.quit 退(tui)出數據庫
.exit 退出數(shu)據庫(ku)
.help 查(cha)看幫助信息
.databases 查看數(shu)據(ju)庫
.tables 顯示數據庫中所有的表(biao)的表(biao)名
2.3、sqlite3 的(de)使用 : 增刪改查
1-- 創建一張表(biao)
create table 表名(字段(duan)(duan)名稱1 字段(duan)(duan)類型(xing),字段(duan)(duan)名稱2 字段(duan)(duan)類型(xing), ....);
create table stu(id int, name char, sex char , score int);
2-- 向(xiang)表中插入一條記(ji)錄(lu)
insert into 表名 values (字(zi)(zi)段值1,字(zi)(zi)段值2,...);
insert into stu values(1001, 'zhangsan', 'm', 89);
insert into stu (id, name, sex,score) values(1002, 'lisi', 'm', 99);
3-- 查詢記錄
select * from stu; // 查找所有(you)的記錄
select * from stu where id=1001; // 查找符號(hao)條件的記錄
select * from stu where id=1001 and name='zhangsan'; // 字符(fu)串需要加(jia)引號
select * from stu where name = 'zhangsan' or score=92;
4-- 刪除記錄
delete from stu where id=1004;
5-- 更(geng)新記錄
update stu set score=98 where id=1003;
6-- 刪(shan)除一張表
drop table stu;
7-- 添加一列
alter table stu add column score int;
8-- 刪除一列
sqlite3 不允許(xu)直接刪除一列
1)先創建一張新表(biao)
create table stu1 as select id , name from stu;
2)刪除原(yuan)來的舊表
drop table stu;
3)對新表重(zhong)命名
alter table stu1 rename to stu;
9-- 數據(ju)庫主(zhu)鍵(既(ji)設置的數據(ju)將會(hui)是(shi)唯一存在(zai)的)
create table usr(name text primary key , passwd text);
3、sqlite3 API 函數接口:
學習API函數(shu)(shu)接口,那么首先(xian)要掌握函數(shu)(shu)接口的三要素: 功能--參數(shu)(shu)--返回值,
然后(hou)就是寫代碼去驗(yan)證我們(men)隊函數的理(li)解,然后(hou)將函數用(yong)到實際的項目開發當中
(1)int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
功(gong)能:打開一個數據庫
參數(shu):filename 數(shu)據庫名字
ppdb 操作數據庫的指針,句柄(bing)。
返回(hui)值(zhi):成(cheng)功 SQLITE_OK , 失敗 error_code
(2)const char *sqlite3_errmsg(sqlite3* db);
功能:獲取錯誤信息描述
(3)int sqlite3_close(sqlite3* db);
功能(neng):關閉一個數據庫
(4)int sqlite3_exec(
sqlite3* db, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void * arg, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
功(gong)能:執行一條sql語句
參數(shu):db 數(shu)據(ju)庫的句柄指針
sql 將(jiang)要被(bei)執(zhi)行sql語句
callback 回調函(han)數, 只(zhi)有在(zai)查詢語句時,才給回調函(han)數傳參
arg 為(wei)callback 傳參(can)的
errmsg 錯誤(wu)信息的(de)地址
返回值:成(cheng)功 SQLITE_OK
出錯 errcode 錯誤(wu)碼
int (*callback)(void* arg ,int ncolumn ,char** f_value,char** f_name)
功(gong)能:得到(dao)查詢結果
參(can)數:arg 為回調函(han)數傳遞參(can)數使用(yong)的
ncolumn 記(ji)錄中包含的字(zi)段的數目(mu)
f_value 包含每個(ge)字段值的指針數組
f_name 包(bao)含每(mei)個(ge)字段名稱的指針數組(zu)
返回(hui)值:成(cheng)功(gong) 0,出錯(cuo) 非0
(5)int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
功能(neng):查(cha)詢數據庫(ku),它會創建一個新的(de)內存區(qu)域來存放查(cha)詢的(de)結(jie)果信息(xi)
參數:db 數據庫操作句(ju)柄
sql 數據庫的sql語(yu)句
azResult 查詢(xun)的結(jie)果
nRow 行數
nColumn 列數
errmsg 錯誤(wu)消息
返回值:
成功 0
出(chu)錯 errcode
nrow的(de)值為查詢到的(de)符合條件的(de)記錄數(不包括字段名)。
ncolumn的(de)值為查詢到的(de)符合條件的(de)字段(duan)數。
注意(yi):nrow的值不包(bao)括字段(duan)名(ming)(ming),如果打印(yin)時用for (i = 0; i < nrow; i++)會打印(yin)出(chu)字段(duan)名(ming)(ming),但(dan)是會少(shao)打印(yin)出(chu)一條(tiao)符合(he)條(tiao)件(jian)的記(ji)錄。
因(yin)此打印時要用 for (i = 0; i
(6)void sqlite3_free_table(char **result);
功能:釋放(fang)內存
4、下面我們先來測(ce)一下sqlite命令的使用:
fengjunhui@ubuntu:~/Sqlite$ sqlite3 fengjunhui.db
SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table stu(id int,name char,sex char,score int);
sqlite> insert into stu values(1001,'fengjunhui','m',88);
1001|fengjunhui|m|88
1002|liuxiaofan|m|92
1003|luzhengyu|m|96
1004|xiaohui|m|86
sqlite> select * from stu where name=fengjunhui ;
Error: no such column: fengjunhui ----注(zhu)意類型匹配
sqlite> select * from stu where name = 'fengjunhui';
1001|fengjunhui|m|88
sqlite> select * from stu where id=1003;
1003|luzhengyu|m|96
sqlite> update stu set sex = f where id=1002;
Error: no such column: f
sqlite> update stu set sex ='f' where id=1002;
sqlite> select * from stu
...> ;
1001|fengjunhui|m|88
1002|liuxiaofan|f|92
1003|luzhengyu|m|96
1004|xiaohui|m|86
sqlite> delete from stu where name='xiaohui';
sqlite> select * from stu;
1001|fengjunhui|m|88
1002|liuxiaofan|f|92
1003|luzhengyu|m|96
sqlite> alter table stu add column age int;
sqlite> select * from stu ;
1001|fengjunhui|m|88|
1002|liuxiaofan|f|92|
1003|luzhengyu|m|96|
sqlite> update stu set age = 25 where id=1001;
sqlite> select * from stu;
1001|fengjunhui|m|88|25
1002|liuxiaofan|f|92|
1003|luzhengyu|m|96|
sqlite> create table stu_bak as select id,name,sex,score from stu;
sqlite> .table
stu stu_bak
sqlite> .schema
CREATE TABLE stu(id int,name char,sex char,score int, age int);
CREATE TABLE stu_bak(
id INT,
name TEXT,
sex TEXT,
score INT
);
sqlite> drop table stu;
sqlite> alter table stu_bak rename to stu;
sqlite> .table
stu
sqlite> create table usr(name text primary key,passwd text);
sqlite> .talbe
Error: unknown command or invalid arguments: "talbe". Enter ".help" for help
sqlite> .table
stu usr
5、sqlite3 API函(han)數接(jie)口的使用(yong)
測試源碼:
  #include
  #include
//注(zhu)意:編(bian)譯時需(xu)要手動鏈接庫文件 -lsqlite3
#define DATABASE "student.db"
#define N 128
#define IS_NUM(index) (0 <= index && index <= 9) ? 1 : 0
int flags = 0;
int do_insert(sqlite3 *db)
{
int id;
char name[N];
int score;
char sql[N];
char *errmsg;
printf("please input id >>> ");
scanf("%d", &id);
getchar();
printf("please input name >>> ");
scanf("%s", name);
getchar();
printf("please input score >>> ");
scanf("%d", &score);
getchar();
sprintf(sql, "insert into stu values(%d, '%s', %d)", id, name, score);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
{
printf("the datas is inserted successfully\n");
}
return 0;
}
//回調函數負責得到(dao)查詢(xun)的(de)結果(guo)
int callback(void *arg, int ncolumn, char **f_value, char **f_name)
{
int i = 0;
if(flags == 0)
{
for(i = 0; i < ncolumn; i++)
{
printf("%-11s", f_name[i]);
}
putchar(10);
flags = 1;
}
for(i = 0; i < ncolumn; i++)
{
printf("%-11s", f_value[i]);
}
putchar(10);
return 0;
}
//查詢數據
int do_select(sqlite3 *db)
{
char *errmsg;
if(sqlite3_exec(db, "select * from stu", callback, NULL, &errmsg) != SQLITE_OK){
printf("%s\n", errmsg);
}else{
printf("the datas is selected successfully\n");
}
return 0;
}
int do_update(sqlite3* db)
{
int id,score;
char name[N]={0},columnname[N]={0},columntype[N]={0};
int num,index;
char* errmsg;
char sql[N]={0};
printf("input your index for search: 1 id 2 name 3 score: \n");
scanf("%d",&index);
getchar();
if(index == 1){
printf("input id : \n");
scanf("%d",&id);
}else if(index == 2){
printf("input name : \n");
scanf("%s",name);
}else {
printf("input score: \n");
scanf("%d",&score);
}
printf("your choice whose info to update: 1 id 2 name 3 score 4 colum: \n");
scanf("%d",&num);
getchar();
switch(num)
{
case 1:
printf("please input id: ");
scanf("%d",&id);
getchar();
if(index == 1){
printf("input sorry,the same info,no need change.\n ");
}else if(index == 2){
sprintf(sql,"update stu set id=%d where name='%s'",id,name);
}else if(index == 3){
sprintf(sql,"update stu set id=%d where score=%d",id,score);
}if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
printf("%s\n",errmsg);
}
break;
case 2:
printf("please input name: ");
scanf("%s",name);
getchar();
if(index == 1){
sprintf(sql,"update stu set name=%s where id='%d'",name,id);
}if(index == 2){
printf("input sorry,the same info,no need change.\n ");
}else if(index == 3){
sprintf(sql,"update stu set name=%s where score=%d",name,score);
}if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
printf("%s\n",errmsg);
}
break;
case 3:
printf("please input socre: ");
scanf("%d",&score);
getchar();
if(index == 1) {
sprintf(sql,"update stu set score=%d where id='%d'",score,id);
}else if(index == 2){
sprintf(sql,"update stu set score=%d where name=%s",score,name);
} else if(index == 3){
printf("input sorry,the same info,no need change.\n ");
}
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
printf("%s\n",errmsg);
}
break;
case 4:
printf("please input column name: ");
scanf("%s",columnname);
getchar();
printf("please input column type: INT or CHAR");
scanf("%s",columntype);
getchar();
sprintf(sql,"alter table stu add column '%s' '%s' '",columnname,columntype);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
printf("%s\n",errmsg);
}
break;
default:
printf("input illegal.\n");
}
printf("the datas is update successfully\n");
return 0;
}
int do_delete(sqlite3* db)
{
int i=0;
char table[N]={0};
int id,score,index;
char name[N]={0};
char* errmsg;
char sql[N]={0};
char *delsql[5]={0};
printf("\n");
do_select(db); //printf database info list
printf("\n");
printf("input index for delete: 1 id 2 name 3 score 4 table 5 colum: \n");
scanf("%d",&index);
getchar();
if(index == 1){
printf("input id : \n");
scanf("%d",&id);
sprintf(sql,"delete from stu where id=%d",id);
}else if(index == 2){
printf("input name : \n");
scanf("%s",name);
sprintf(sql,"delete from stu where name=%s",name);
}else if(index == 3){
printf("input score: \n");
scanf("%d",&score);
sprintf(sql,"delete from stu where score=%d",score);
}else if(index == 4){
printf("input which table: \n");
scanf("%s",table);
sprintf(sql,"drop table %s",table);
}else if(index == 5){
#if 0
sprintf(delsql[0],"create table stu1 as select id,name from stu\n");
sprintf(delsql[1],"drop table stu\n");
sprintf(delsql[2],"alter table stu1 rename to stu\n");
#endif
delsql[0] = "create table stu1 as select id,name from stu";
delsql[1] = "drop table stu";
delsql[2] = "alter table stu1 rename to stu";
for(i = 0;i < 3;i ++){
printf("delsql[%d]: %s\n",i,delsql[i]);
}
return 0;
}
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
printf("%s\n",errmsg);
}
return 0;
}
int main(int argc, const char *argv[])
{
sqlite3 *db;
char *errmsg;
int num;
//創建(打開)數(shu)據庫
if(sqlite3_open(DATABASE, &db) != SQLITE_OK) {
printf("%s\n", sqlite3_errmsg(db));
}else{
printf("the database is opened successfully\n");
}
//創建一張表(biao)
if(sqlite3_exec(db, "create table stu(id int, name char, score int)", NULL, NULL, &errmsg) != SQLITE_OK){
printf("%s\n", errmsg);
}else {
printf("the table is created successfully\n");
}
//對(dui)當前表進行增(zeng)刪改(gai)查
while(1)
{
printf("\n");
printf("***1:插入(ru)數(shu)據 2:查詢數(shu)據 3:修改數(shu)據 4:刪除數(shu)據 5:退出***\n");
printf("\n");
printf(">>> ");
scanf("%d", &num);
getchar();
switch(num)
{
case 1:
do_insert(db);
break;
case 2:
flags = 0;
do_select(db);
//do_select_get_table(db);
break;
case 3:
do_update(db);
break;
case 4:
do_delete(db);
break;
case 5:
sqlite3_close(db);
return -1;
default:
printf("please input correct option\n");
}
}
return 0;
}
測試結果:
fengjunhui@ubuntu:~/Sqlite$ ./a.out
the database is opened successfully
table stu already exists
1:插入數據(ju)(ju) 2:查詢數據(ju)(ju) 3:修改數據(ju)(ju) 4:刪(shan)除(chu)數據(ju)(ju) 5:退出
>>> 2
id name score
1001 liuxiaofan 92
1004 fengjunhui 98
1003 luzhengyu 96
the datas is selected successfully
1:插入數(shu)據 2:查詢數(shu)據 3:修改數(shu)據 4:刪除數(shu)據 5:退出
>>> 1
please input id >>> 1005
please input name >>> xiaohuihui
please input score >>> 76
the datas is inserted successfully
1:插入數(shu)據(ju) 2:查(cha)詢數(shu)據(ju) 3:修改數(shu)據(ju) 4:刪除數(shu)據(ju) 5:退(tui)出
>>> 3
input your index for search: 1 id 2 name 3 score:
1
input id :
1004
your choice whose info to update: 1 id 2 name 3 score 4 colum:
3
please input socre: 88
the datas is update successfully
1:插入數(shu)據(ju)(ju)(ju) 2:查詢數(shu)據(ju)(ju)(ju) 3:修改(gai)數(shu)據(ju)(ju)(ju) 4:刪除數(shu)據(ju)(ju)(ju) 5:退出
>>> 2
id name score
1001 liuxiaofan 92
1004 fengjunhui 88
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出
>>> 4
1001 liuxiaofan 92
1004 fengjunhui 88
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
input index for delete: 1 id 2 name 3 score 4 table 5 colum:
2
input name :
fengjunhui
no such column: fengjunhui
1:插(cha)入數據(ju) 2:查詢數據(ju) 3:修改數據(ju) 4:刪除數據(ju) 5:退(tui)出
>>> 2
id name score
1001 liuxiaofan 92
1004 fengjunhui 88
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
1:插(cha)入數(shu)據(ju)(ju)(ju) 2:查詢數(shu)據(ju)(ju)(ju) 3:修改數(shu)據(ju)(ju)(ju) 4:刪除數(shu)據(ju)(ju)(ju) 5:退出
>>> 5
fengjunhui@ubuntu:~/Sqlite$ gcc 5_student.c -lsqlite3
fengjunhui@ubuntu:~/Sqlite$ ./a.out
the database is opened successfully
table stu already exists
1:插入數(shu)據 2:查詢數(shu)據 3:修改數(shu)據 4:刪(shan)除數(shu)據 5:退(tui)出
>>> 4
id name score
1001 liuxiaofan 92
1004 fengjunhui 88
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
input index for delete: 1 id 2 name 3 score 4 table 5 colum:
2
input name :
'fengjunhui'
1:插入數(shu)(shu)據 2:查(cha)詢(xun)數(shu)(shu)據 3:修改數(shu)(shu)據 4:刪(shan)除數(shu)(shu)據 5:退出(chu)
>>> 2
id name score
1001 liuxiaofan 92
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
1:插(cha)入數據 2:查詢數據 3:修改(gai)數據 4:刪除(chu)數據 5:退出
>>> 5
fengjunhui@ubuntu:~/Sqlite$ gcc 5_student.c -lsqlite3
fengjunhui@ubuntu:~/Sqlite$ ./a.out
the database is opened successfully
table stu already exists
1:插入數據(ju) 2:查詢數據(ju) 3:修改(gai)數據(ju) 4:刪除數據(ju) 5:退出
>>> 4
id name score
1001 liuxiaofan 92
1003 luzhengyu 96
1005 xiaohuihui 76
the datas is selected successfully
input index for delete: 1 id 2 name 3 score 4 table 5 colum:
5
delsql[0]: create table stu1 as select id,name from stu
delsql[1]: drop table stu
delsql[2]: alter table stu1 rename to stu
1:插入(ru)數(shu)據(ju) 2:查詢數(shu)據(ju) 3:修改數(shu)據(ju) 4:刪除數(shu)據(ju) 5:退出
>>> 5
擴展內容:
sqlite3支持的數據類型:
NULL、INTEGER、REAL、TEXT、BLOB
但(dan)是(shi),sqlite3也支持(chi)如下的數據類型
smallint 16位(wei)整(zheng)數
integer 32位整(zheng)數
decimal(p,s) p是精確值(zhi),s是小數(shu)位(wei)數(shu)
float 32位(wei)實數
double 64位實數
char(n) n長(chang)度字符串,不能超過254
varchar(n) 長(chang)度(du)不固定大(da)字(zi)符串長(chang)度(du)為n,n不超過4000
graphic(n) 和(he) char(n) 一樣,但(dan)是(shi)單位是(shi)兩(liang)個字(zi)符double-bytes,n不超過127(中文字(zi))
vargraphic(n) 可變(bian)長度且大(da)長度為n
date 包含了年份、月份、日期
time 包含了小時、分鐘、秒
timestamp 包含了年、月、日、時、分、秒、千分之(zhi)一秒
sqlite3支(zhi)持(chi)的函數
【1】日(ri)期函數
datetime() : 產生日期和時間(jian)
date(): 產(chan)生日期
time():產生(sheng)時間
strftime():對以上3個函數(shu)產生的(de)日期和時間進行格式化(hua)
用法實例:
1、select date('2011-9-9','+1 day','+1 year'); 結果是 2010-09-10
2、select datetime('now'); 當前(qian)日(ri)期和時間
3、select datetime('now', 'start of month'); 本(ben)月(yue)的第一(yi)天零點,也可(ke)以設(she)置年和日的第一(yi)天
4、select datetime('now','+1 hour','-12 minute'); 當前時間加48分鐘
strftime()函數可以將YYYY-MM-DD HH:MM:SS格(ge)式的(de)日期字(zi)符串轉換為其它形式的(de)字(zi)符串
%d:天數(shu),01-31
%f :小數形式的秒,SS.SSS
%H:小時
%j :某一(yi)天是該年的第(di)幾天,001-366
%m:月份,00-12
%M:分(fen)鐘,00-59
%s:從1970到現在的秒數
%S:秒,00-59
%w:星期(qi),0-6,0是(shi)星期(qi)天
%W:某天是該年的第幾周(zhou),01-53
%Y:年,YYYY
%% 百(bai)分(fen)號(hao)
應用舉例:
select strftime('%Y.%m.%d %H:%M:%S','now','localtime');
二、【算術(shu)函數】
abs(X):返回絕對值
max(X,Y[,...]):返回大值
min(X,Y,[,...]):返(fan)回小值
random(*):返回隨(sui)機(ji)數
round(X[,Y]): 四舍(she)五入
三、【字(zi)符串處理函數(shu)】
length(x) :返回字符串字符個數
lower(x) :大寫(xie)轉(zhuan)小寫(xie)
upper(x):小寫(xie)轉(zhuan)大寫(xie)
substr(x,y,Z):截取子串
like(A,B):確定(ding)給定(ding)的字符(fu)串與指定(ding)的模式(shi)是否匹配

