原创 嵌入式数据库sqlite

2010-1-7 12:07 2403 1 1 分类: MCU/ 嵌入式

自己总结的一些关于嵌入式数据库的一些操作步骤


第一章


1、创建sqlite数据库表


#sqlite3 demo.db


sqlite> create table t(id int,name char(10));


sqlite> insert into t values(0,’boobooke’);


sqlite> select * from t;


0|boobooke


下面是插入一个数据


sqlite> insert into t values(1,’ ‘);


sqlite> select *form t;


0|boobooke


1|


下面是更新数据


sqlite> update t set name=’<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />wilson’ where id="1";


sqlite> select * from t;


0|boobooke


1|wilson


下面是可以把竖线换成另外一种符号


sqlite> .separator -


sqlite> select * from t;


0-boobooke


1-wilson


2-booke


3-hahaha


下面是几种不同模式的输出


1


sqlite> .mode line


sqlite> select * from t;


   id = 0


 name = boobooke


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 


   id = 1


 name = wilson


 


   id = 2


 name = booke


 


   id = 3


 name = hahaha


2


sqlite> .mode column


sqlite> select * from t;


0           boobooke 


1           wilson   


2           booke    


3           hahaha   


sqlite> .header on


sqlite> select * from t;


id          name     


----------  ----------


0           boobooke 


1           wilson   


2           booke    


3           hahaha 


 


3


sqlite> .width 3 30


sqlite> select * from t;


id   name                         


---  ------------------------------


0    boobooke                     


1    wilson                       


2    booke                        


3    hahaha


 


4


sqlite> .header off


sqlite> select * from t;


0    boobooke                     


1    wilson                        


2    booke                        


3    hahaha                       


sqlite> .header on


sqlite> select * from t;


id   name                         


---  ------------------------------


0    boobooke                     


1    wilson                        


2    booke                        


3    hahaha


 


5


sqlite> .mode insert wilsontbl


sqlite> select * from t;


INSERT INTO wilsontbl VALUES(0,'boobooke');


INSERT INTO wilsontbl VALUES(1,'wilson');


INSERT INTO wilsontbl VALUES(2,'booke');


INSERT INTO wilsontbl VALUES(3,'hahaha');


6


sqlite> .mode html


sqlite> select * from t;


<TR><TH>id</TH><TH>name</TH></TR>


<TR><TD>0</TD>


<TD>boobooke</TD>


</TR>


<TR><TD>1</TD>


<TD>wilson</TD>


</TR>


<TR><TD>2</TD>


<TD>booke</TD>


</TR>


<TR><TD>3</TD>


<TD>hahaha</TD>


</TR>


7)逗号隔开


sqlite> .mode csv


sqlite> select * from t;


"id","name"


0,"boobooke"


1,"wilson"


2,"booke"


3,"hahaha"


 


8output test.txt是把查询结果输出到test.txt里,stdout是输出到屏幕上


sqlite> .output test.txt


sqlite> select * from t;


sqlite> .output stdout


sqlite> select * from t;


"id","name"


0,"boobooke"


1,"wilson"


2,"booke"


3,"hahaha"


sqlite> .e


[root@localhost /]# cat test.txt


"id","name"


0,"boobooke"


1,"wilson"


2,"booke"


3,"hahaha"


 


9)查看你是怎么创建表的


sqlite> .schema


CREATE TABLE t(id int,name char(10));


10)压缩数据库


#echo ".dump" | sqlite3 demo.db | gzip -c > backup.gz


[root@localhost /]# ls


backup.gz  home            net                 sbin           tmp


bin        Kernel          nfs                 selinux        u-boot-1.3.3


boot       Kernel.tar.bz2  nfs_1               sqlitedemo.c   uboot.tar.bz2


11)解压缩数据库


#zcat backup.gz | sqlite3 demo1.db


#ll


-rw-r--r--   1 root root     2048 12-17 21:04 demo1.db


-rw-r--r--   1 root root     2048 12-17 17:46 demo.db


两个文件是相同的大小


 


第二章


1、用C文件实现sqlite的方法:


如下程序就是实现用C文件查询数据库的方法:


#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>


static int callback(void *NotUsed,int argc,char **argv,char **azColName)
{
   int i;
   for(i=0;i<argc;i++)
        {
      printf("%s = %s\n",azColName,argv?argv:"NULL");
        }
   printf("\n");
   return 0;
}


int main(int argc,char **argv)
{
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;
  if(argc != 3)
      {
    fprintf(stderr,"Usage: %s DATABASE SQL-STATEMENT\n",argv[0]);
    exit(0);
      }
  rc="sqlite3"_open(argv[1],&db);
  if(rc)
     {
    fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(1);
     }
  rc="sqlite3"_exec(db,argv[2],callback,0,&zErrMsg);
  if(rc!=SQLITE_OK)
     {
    fprintf(stderr,"SQL error: %s\n",zErrMsg);
    sqlite3_free(zErrMsg);
     }
  printf("The SQL statement is executed successful.Please check the database file!\n");
  sqlite3_close(db);
  return 0;
}


编译如下:


gcc -I/usr/include -L/usr/lib -lsqlite3 sqlitedemo.c -o dbtest


/usr/include是你sqlite3的头文件,/usr/lib是你sqlite3所在的库文件,sqlitedemo.c是你所写的C文件,dbtest是你生成的可执行文件


2、执行程序的步骤


1


./dbtest demo.db "SELECT * FROM t"


dbtest是你所要执行的可执行文件,demo.db是你数据库的名字


2


./dbtest demo.db "INSERT INTO t VALUES(3,'hahaha')"


t是你的数据库里面表的名字


这个是插入一个数据


 


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
1
关闭 站长推荐上一条 /3 下一条