gogo专业大尺度亚洲高清人体,美女张开双腿让男生桶,亚洲av无码一区二区三区鸳鸯影院,久久久久国产精品人妻

當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > sqlite常用命令及編程接口介紹

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

一、常用命令介紹

在終端下運行sqlite3   <*.db>,出現(xiàn)如下提示符

SQLite  version  3.7.2

Enter “.help” for instructions

Enter SQL statements terminated with a “;”

sqlite>

<*.db> 是要打開的數(shù)據(jù)庫文件。若該文件不存在,則自動創(chuàng)建。 

顯示所有命令 

sqlite> .help

退出sqlite3

sqlite>.quit

顯示當(dāng)前打開的數(shù)據(jù)庫文件 

sqlite>.database

顯示數(shù)據(jù)庫中所有表名 

sqlite>.tables

查看表的結(jié)構(gòu) 

sqlite>.schema  <table_name>

 

以下為SQL命令,每個命令以;結(jié)束 

創(chuàng)建新表 

>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   構(gòu)成復(fù)合主鍵   

   create   table   paper   (   

   paper_name   varchar(50)   not   null,   

   author_id   char(10)   not   null, 

//…..  

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

   )       

刪除表 

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數(shù)據(jù)庫 

            path: 數(shù)據(jù)庫文件路徑 

            db: 指向sqlite句柄的指針 

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

        

2) int   sqlite3_close(sqlite3 *db); 

   功能:關(guān)閉sqlite數(shù)據(jù)庫          

      返回值:成功返回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); 

   功能:執(zhí)行SQL操作 

              db:數(shù)據(jù)庫句柄 

              sql:SQL語句 

       callback:回調(diào)函數(shù) 

       errmsg:錯誤信息指針的地址 

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

 

不需要回調(diào)函數(shù)的情況:有關(guān)插入或更新的sql語句。

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

{

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

     exit(-1);

 }

需要回調(diào)函數(shù)的情況:有關(guān)查詢的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

不使用回調(diào)函數(shù)執(zhí)行SQL語句:

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

{

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

     exit(-1);

 }

index = ncolumn;    // 第一條記錄的第一個字段的下標(biāo) 

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協(xié)議與OSI協(xié)議詳解

下一篇:FS4412 移植 libiconv庫

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

回到頂部