Chinaunix首页 | 论坛 | 博客
  • 博客访问: 270921
  • 博文数量: 76
  • 博客积分: 1500
  • 博客等级: 上尉
  • 技术积分: 594
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-05 23:43
文章分类

全部博文(76)

文章存档

2014年(4)

2013年(3)

2012年(20)

2011年(49)

分类: SQLite/嵌入式数据库

2011-10-20 10:27:50

转载于:

http://blog.21ic.com/user1/765/archives/2008/49947.html

张驿风

2008年8月19日

1.   下载sqlite3源代码

        

2. 解压源代码:

    tar  xvzf   sqlite-amalgamation-3.6.1.tar.gz

3. 配置交叉编译到arm linux平台:

    ./configure --prefix=/home/rootfs/home/sqlite --target=arm-linux --host=arm-linux LD=arm-linux-ld

4. 编译:

       make

5. 安装:

      make install

     头文件和生成的库文件将安装到:/home/rootfs/home/sqlite目录下。

6. 写自己的数据库应用

   //error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );

#i nclude
#i nclude
 

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}
 
int main(int argc, char **argv){
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;
 
  if( argc!=2 ){
    fprintf(stderr, "Usage: %s DATABASE \n", argv[0]);
    return 0;
  }
  rc = sqlite3_open(argv[1], &db);
  if( rc ){
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 0;
  }
 
  char *sql = " CREATE TABLE SensorData(               \
                        ID       INTEGER PRIMARY KEY,  \
                        SensorID INTEGER,              \
                        SiteNum  INTEGER,              \
                        Time     VARCHAR(12),          \
                        SensorParameter REAL           \
                );";
   //使用sql字符串指定的sql语言 创建数据表SensorData
 

    sqlite3_exec( db , sql , callback , 0 ,  &zErrMsg );

 
   //插入数据到数据表

  sql = "INSERT INTO 'SensorData' VALUES( 0 , 1 , 1 ,  '200605011206', 18.9 );" ;

   //error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );

    sqlite3_exec( db , sql , callback , 0 ,  &zErrMsg );

   //插入数据到数据表
  sql = "INSERT INTO  'SensorData'  VALUES(1 , 1 , 1 , '200605011306', 16.4 );" ;

   //error sqlite3_exec( db , sql , 0 , callback , &zErrMsg );

    sqlite3_exec( db , sql , callback , 0 ,  &zErrMsg );

 

  int nrow = 0, ncolumn = 0;
  char **azResult;
  int i;

  //从数据表查询数据
  sql = "SELECT * FROM SensorData ";
  sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 
  printf( "row:%d column=%d" , nrow , ncolumn );
  printf( "The result of querying is :" );
  for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ ){
     printf( "azResult[%d] = %s \r\n", i , azResult[i] );
  }
 
  sqlite3_free_table( azResult );
 
  if( rc!=SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
  }
  sqlite3_close(db);
 
  return 0;
}

7. 编写Makefile

 prefix=/home/rootfs/home/sqlite
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Libs = -L${libdir} -lsqlite3 -lpthread
Cflags = -I${includedir}

 
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
 
radiodb: radiodb.o
        $(CC) $^ $(Libs)  -o $@
radiodb.o:radiodb.c
        $(CC) $(Cflags) -c $^ -o $@
clean:
        rm -rf radiodb  *.o
in:
        cp radiodb /home/rootfs/home/sqlite

   注意makefile里的红色字体内容可以在sqlite安装目录的lib下的pkconfig下的文件查得。


这个问题是这样的,其实sqlite3_get_table这个API从一开始就是不推荐使用的,你可以看一下所有的SQLite范例,极少有用到这个API的,原因除了你提到的id取值范围问题之外,更重要的是它无法处理BLOB字段类型,所以在实际当中它只能算是一个测试API.
而且sqlite3_get_table函数本身你可以看一下源代码,也是由
int sqlite3_prepare(sqlite3*, const char*, int, sqlite3_stmt**, const char**);
int sqlite3_bind_double(sqlite3_stmt*, int, double);
int sqlite3_bind_int(sqlite3_stmt*, int, int);
int sqlite3_bind_int64(sqlite3_stmt*, int, long long int);
int sqlite3_bind_null(sqlite3_stmt*, int);
int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
int sqlite3_step(sqlite3_stmt*);
这些API组合而成的.
所以正确的做法是自己使用上面API函数,这样当表中的数据很多的时候可以用__int64来接收ID
阅读(797) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~