日韩在线免费播放-日韩在线免费av-日韩在线免费-日韩在线毛片-国产高清不卡视频-国产高清不卡

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > sqlite常用命令及編程接口介紹

sqlite常用命令及編程接口介紹 時間:2017-10-30      來源:未知

一、常用命令介紹

在終端下運行sqlite3   <*.db>,出現如下提示符

SQLite  version  3.7.2

Enter “.help” for instructions

Enter SQL statements terminated with a “;”

sqlite>

<*.db> 是要打開的數據庫文件。若該文件不存在,則自動創建。 

顯示所有命令 

sqlite> .help

退出sqlite3

sqlite>.quit

顯示當前打開的數據庫文件 

sqlite>.database

顯示數據庫中所有表名 

sqlite>.tables

查看表的結構 

sqlite>.schema  <table_name>

 

以下為SQL命令,每個命令以;結束 

創建新表 

>create  table  <table_name>  (f1  type1, f2  type2,…);

sqlite> create table student(no int primary key, name text, score real);

sqlite> create table stu (no int primary key, name text not null, score real);

 

備注:1)默認字段值可以為空;2)下面的命令,sqlite3不支持

paper_name+author_id   構成復合主鍵   

   create   table   paper   (   

   paper_name   varchar(50)   not   null,   

   author_id   char(10)   not   null, 

//…..  

   constraint   PK_paper   primary   key(paper_name,author_id)   --復合主鍵   

   )       

刪除表 

sqlite>drop  table  <table_name>

sqlite>drop  table  student

查詢表中所有記錄 

sqlite>select  *  from  <table_name>;  

按指定條件查詢表中記錄 

sqlite>select  *  from  <table_name>  where  <expression>; 

sqlite> select * from student 

sqlite>select * from student where score is not null;

sqlite> select * from student where name=’zhao’

sqlite> select * from student where name=’zhao’ and score >=95

sqlite> select id, name from student where name=’zhao’ and score >=95

sqlite> select count(*) from student where score>90

sqlite>select * from student order by score desc;

sqlite>select * from student order by score asc;

向表中添加新記錄 

sqlite>insert  into  <table_name>  values (value1, value2,…);

sqlite> insert into student values(1, ‘zhao’, 92);

sqlite> insert into student(no, name) values(2, ‘li’);

 

按指定條件刪除表中記錄 

sqlite>delete  from  <table_name>  where  <expression>

sqlite> delete from student where score<60;

更新表中記錄 

sqlite>update  <table_name>  set  <f1=value1>, <f2=value2>…   where  <expression>;  

sqlite> update student set score=0;

sqlite> update student set name=’sun’ where no=3;

sqlite>update student set score=100, name='chenyong' where no=2;

在表中添加字段 

sqlite>alter table <table> add column <field> <type>; 

sqlite> alter table student add column gender integer default 0;

在表中刪除字段 

Sqlite中不允許刪除字段,可以通過下面步驟達到同樣的效果

sqlite> create table stu as select no, name, score from student

sqlite> drop table student

sqlite> alter table stu rename to student

二、常用編程接口介紹

1) int   sqlite3_open(char  *path,   sqlite3 **db); 

     功能:打開sqlite數據庫 

            path: 數據庫文件路徑 

            db: 指向sqlite句柄的指針 

     返回值:成功返回0,失敗返回錯誤碼(非零值) 

        

2) int   sqlite3_close(sqlite3 *db); 

   功能:關閉sqlite數據庫          

      返回值:成功返回0,失敗返回錯誤碼 

3) const  char  *sqlite3_errmg(sqlite3 *db); 

        返回值:返回錯誤信息 

4) typedef  int (*sqlite3_callback)(void *, int, char **, char **);

int   sqlite3_exec(sqlite3 *db, const  char  *sql,  sqlite3_callback callback, void *,  char **errmsg); 

   功能:執行SQL操作 

              db:數據庫句柄 

              sql:SQL語句 

       callback:回調函數 

       errmsg:錯誤信息指針的地址 

       返回值:成功返回0,失敗返回錯誤碼 

 

不需要回調函數的情況:有關插入或更新的sql語句。

if  (sqlite3_exec(db, “delete from  table1  where id = 1”,  NULL,  NULL, &errmsg) !=  SQLITE_OK)

{

     printf(“error :  %s\n”,  errmsg);

     exit(-1);

 }

需要回調函數的情況:有關查詢的sql語句。

int  callback(void *para, int f_num,  char **f_value, char **f_name)

{

      int  i;

      printf(“*****************************\n”);

      for (i=0; i<f_num; i++)

      {

            printf(“%s :  %s\n”,  f_name[i], f_value[i]);

      }

      return  0; 

}

if  (sqlite3_exec(db, “select  *  from  table”,  callback,  NULL, &errmsg) !=  SQLITE_OK)

{

     printf(“error :  %s\n”,  errmsg);

     exit(-1);

 }

代碼輸出如下:

no:2

name:zhao

score:86

no:3

name:wang

score:86

不使用回調函數執行SQL語句:

if  (sqlite3_get_table(db, “select  *  from  table”,  &resultp,  &nrow, &ncolumn,  &errmsg) !=  SQLITE_OK)

{

     printf(“error :  %s\n”,  errmsg);

     exit(-1);

 }

index = ncolumn;    // 第一條記錄的第一個字段的下標 

for  (i=0;  i<nrow;  i++)

{

     for  (j=0;  j<ncolumn;  j++)

     {

           printf(“%s  :  %s\n”,  resultp[j],  resultp[index++]);    

     }

}

for  (i=0;  i<(nrow+1)* ncolumn;  i++)

{

    printf(“%s   ”,  resultp[i];   

}

Printf(“\n”); 

代碼輸出如下:

no:2

name:zhao

Score:86

no:3

name:wang

Score:86

no name score 2 zhao 86 3 wang 86

 

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN' 

上一篇:TCP/IP協議與OSI協議詳解

下一篇:FS4412 移植 libiconv庫

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 林正英电影大全免费看| 意外的春天 电影| alexandra hedison| 84年社保要交15年还是20年| 王宝强电影全部作品| 美妙旋律| 觉醒年代免费看| 心理健康《微笑的力量》ppt| 天堂在线中文视频| 狗叫声吸引狗| 欧美一级毛片免费看| cctv17农业农村频道在线直播| 山上的小屋免费阅读小说全文| 艺术影院| 黑龙江省地图高清全图| 罪孽天使| 黄婉伶| 男同操男同| 伊人春色在线观看视频| 生活片爱情电影大全| 妈妈的爱情房客 电影| 金鸳鸯| 网页抖音| 北京新闻频道回看| 西班牙女郎| 在线播放的免费电影网站| 亚纱美| 网络谜踪| 日本女人性生活视频| 日本电影姐姐| 保镖电影大全免费| 泰坦尼克号电影完整版在线观看| 关于想象的作文| 温州新闻| 浪荡子的旅程电影| smc压力表| 动漫秀场| 我爱发明鬼畜视频| 陈宝莲拍过的电影| 爱情最美丽 电视剧| 徐少强全部电影|