DLL文件和LIB文件生成(关于sqlite数据库)

SQLite是一个开源的跨平台的轻型数据库,WINCE本身也有一个自带的数据库SQLCE ,但占用的资源会比较大。最近项目中考虑用到 SQLite,因此特别研究了一下,下面介绍一下具体的移植方法。

一、下载SQLite源码

       去SQLite官网http://www.sqlite.org/download.htm下载最新的source code。我下载的是sqlite-amalgamation-3071401.zip。解压后会得得到四个文件(shell.c、sqlite3.c、sqlite3.h、sqlite3ext.h)。

二、编译生成WINCE下DLL

      1. 在VS2005下新建一个Win32智能设备项目,选择相应的SDK,并选择应用程序类型为DLL。

      2. 将sqlite-amalgamation-3071401.zip解压后的sqlite3.c、sqlite3.h文件拷贝到工程目录下并添加至工程目录。如下图所示。

            

       3.在预处理中增加SQLITE_ENABLE_RTREE和SQLITE_ENABLE_COLUMN_METADATA宏定义。如图所示:

           

        4.编译项目,会提示“error LNK2019: 无法解析的外部符号 localtime_s,该符号在函数 osLocaltime 中被引用”错误,此错误的解决方法是将localtime_s替换成_localtime64_s即可。

        5.编译成功后会发现目录下会生成SQLite.dll文件,但会发现没有lib文件,这样在使用这个DLL时是没法编译的,所以还需要导出.lib文件,解决方法是在SQLite官网上下载sqlite-dll-win32-x86-XXXXXX.zip文件,解压后将目录下的sqlite3.def文件拷贝到DLL工程目录,在项目属性--链接器--输入--模块定义文件中添加sqlite3.def,如下图所示,然后编译即可。

           

这样SQLite的编译工作就大功告成了。

三、WINCE 下SQLite测试

        新建一个SQLite测试程序,测试代码如下:       

 

[cpp] view plaincopy

  1. #include <windows.h>  
  2.   
  3. // sqlite3的回调函数  
  4. int SQLiteQueryResultCallBack( void * para, int n_column, char ** column_value, char ** column_name )  
  5.   
  6. int main(int argc,char* argv[])  
  7. {  
  8.     sqlite3 * db = NULL; //声明sqlite关键结构指针  
  9.     int result;  
  10.     // 打开或创建数据库  
  11.     result = sqlite3_open("NAND2\\sqlite.db", &db );  
  12.     if( result != SQLITE_OK )  
  13.     {  
  14.         //数据库打开失败  
  15.         return -1;  
  16.     }  
  17.     char * errmsg = NULL;  
  18.     // 数据库操作代码  
  19. #if 1  
  20.     // 创建一个测试表,表名叫 MyTable,有2个字段: ID 和 name。其中ID是一个自动增加的类型,以后insert时可以不去指定这个字段,它会自己从0开始增加  
  21.     result = sqlite3_exec( db, "create table MyTable( ID integer primary key autoincrement, name nvarchar(32) )", NULL, NULL, &errmsg );  
  22.     if(result != SQLITE_OK )  
  23.     {  
  24.         printf("创建表失败,错误码:%d,错误原因:%s\n", result, errmsg );  
  25.     }  
  26.     // 插入记录  
  27.     result = sqlite3_exec( db, "insert into MyTable( name ) values ( '张三' )", 0, 0, &errmsg );  
  28.     if(result != SQLITE_OK )  
  29.     {  
  30.         printf("插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg );  
  31.     }  
  32.     // 插入记录  
  33.     result = sqlite3_exec( db, "insert into MyTable( name ) values ( '李四' )", 0, 0, &errmsg );  
  34.     if(result != SQLITE_OK )  
  35.     {  
  36.         printf("插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg );  
  37.     }  
  38. #endif  
  39.     // 开始查询数据库  
  40.     result = sqlite3_exec( db, "select * from MyTable", SQLiteQueryResultCallBack, NULL, &errmsg );  
  41.     // 关闭数据库  
  42.     sqlite3_close( db );  
  43.   
  44.     return 0;  
  45. }  
  46.   
  47. // sqlite3的回调函数  
  48. int SQLiteQueryResultCallBack( void * para, int n_column, char ** column_value, char ** column_name )  
  49. {  
  50.     printf( "******************************\n" );  
  51.     printf("记录包含 %d 个字段\n", n_column );  
  52.     for(int i = 0 ; i < n_column; i ++ )  
  53.     {  
  54.         printf( "字段名:%s 字段值:%s\n", column_name[i], column_value[i] );  
  55.     }  
  56.     printf( "******************************\n" );  
  57.     return 0;  
  58. }  
 
  1. #include <windows.h>

  2.  
  3. // sqlite3的回调函数

  4. int SQLiteQueryResultCallBack( void * para, int n_column, char ** column_value, char ** column_name )

  5.  
  6. int main(int argc,char* argv[])

  7. {

  8. sqlite3 * db = NULL; //声明sqlite关键结构指针

  9. int result;

  10. // 打开或创建数据库

  11. result = sqlite3_open("NAND2\\sqlite.db", &db );

  12. if( result != SQLITE_OK )

  13. {

  14. //数据库打开失败

  15. return -1;

  16. }

  17. char * errmsg = NULL;

  18. // 数据库操作代码

  19. #if 1

  20. // 创建一个测试表,表名叫 MyTable,有2个字段: ID 和 name。其中ID是一个自动增加的类型,以后insert时可以不去指定这个字段,它会自己从0开始增加

  21. result = sqlite3_exec( db, "create table MyTable( ID integer primary key autoincrement, name nvarchar(32) )", NULL, NULL, &errmsg );

  22. if(result != SQLITE_OK )

  23. {

  24. printf("创建表失败,错误码:%d,错误原因:%s\n", result, errmsg );

  25. }

  26. // 插入记录

  27. result = sqlite3_exec( db, "insert into MyTable( name ) values ( '张三' )", 0, 0, &errmsg );

  28. if(result != SQLITE_OK )

  29. {

  30. printf("插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg );

  31. }

  32. // 插入记录

  33. result = sqlite3_exec( db, "insert into MyTable( name ) values ( '李四' )", 0, 0, &errmsg );

  34. if(result != SQLITE_OK )

  35. {

  36. printf("插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg );

  37. }

  38. #endif

  39. // 开始查询数据库

  40. result = sqlite3_exec( db, "select * from MyTable", SQLiteQueryResultCallBack, NULL, &errmsg );

  41. // 关闭数据库

  42. sqlite3_close( db );

  43.  
  44. return 0;

  45. }

  46.  
  47. // sqlite3的回调函数

  48. int SQLiteQueryResultCallBack( void * para, int n_column, char ** column_value, char ** column_name )

  49. {

  50. printf( "******************************\n" );

  51. printf("记录包含 %d 个字段\n", n_column );

  52. for(int i = 0 ; i < n_column; i ++ )

  53. {

  54. printf( "字段名:%s 字段值:%s\n", column_name[i], column_value[i] );

  55. }

  56. printf( "******************************\n" );

  57. return 0;

  58. }

四、SQLite可视化管理工具

 

 

 

        SQLite本身没有可视化管理工具,只提供了一个命令行的管理工具SQLite.exe。有一个第三方的可视化管理工具Sqlite Expert,用着还可以,下载地址: http://www.sqliteexpert.com/download.html

转载于:https://my.oschina.net/u/3874841/blog/3049798

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值