VS2013 生成sqlite3动态连接库及sqlite3.dll的调用

一,生成sqlite3动态连接库
1,去sqlite官网上下载最近的sqlite源码包,解压后得到四个文件:shell.c,sqlite3.c,sqlite3.h,sqlite3ext.h
此处还需要sqlite3.def文件,它在sqlite官方生成的dll包中,下载下来,解压即可。
其中,shell.c文件是做来生成exe可执行文件用的。
2,打开vs2008 新建sqlite3的非mfc的DLL项目,这里只需要建立空的项目即可。
3,将上面非shell.c的四个文件复制动sqlite3项目工程的目录下

4,将sqlite3.h,sqlite3ext.h两文件添加到项目的头文件下面,把sqlite3.c,sqlite3.def添加到项目的源文件下面。

5,编译即可以得来sqlite3.dll文件
   注意,此时,并未有sqlite3.lib生成,解决方法如下:

6,再次编译:
 出现如下错误:
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_database_name
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_database_name16
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_origin_name
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_origin_name16
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_table_name
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_table_name16
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_rtree_geometry_callback
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_table_column_metadata
1>F:C++_Programsqlite3Debugsqlite3.lib : fatal error LNK1120: 8 个无法解析的外部命令


解决办法是:
 修改项目属性的配置,在预处理器定义中添加SQLITE_ENABLE_COLUMN_METADATA(配置属性-> c/c++ -> 预处理器 -> 预处理器定义)
再次编译,此时仍然会报一个:
1>sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_rtree_geometry_callback
的错误,解决办法是:
在预处理器定义中添加:SQLITE_ENABLE_RTREE
再次编译即会成功生成sqlite3.dll和sqlite3.lib文件

二,生成sqlite3可执行文件
若要生成sqltie3.exe可执行文件,只需新建win32控制台空项目,要将 sqlite3.c,sqlite3.h,shell.c引入源文件中,如下:
[转载]VS2008 <wbr>生成sqlite3动态连接库及sqlite3.dll的调用
生成解决方案,就能得到我们需要的sqlite3.exe文件


三,在C++项目中引用sqlite3的动态连接库
1,新建测试工程sqlite3_Test:
2,将上面生成的sqlite3.dll文件放到debug目录中,在项目目录中引入sqlite3.lib文件,
  并把sqlite3.h加入到项目头文件中
3,在stdafx.h文件中将sqlite3.h头文件引入
#ifndef SQLITE3
#define SQLITE3
#include "sqlite3.h"
#endif
4,在sqlite3_test.cpp源文件中,写入sqltie3数据库操作:
首先在stdafx.h头文件中加入显示数据库查询结果的回调函数:
int showTableInfo(void *para,int n_column,char **column_value,char **column_name);


修改Sqlite3_Test.cpp的源码:
 

[cpp] view plain copy
 
  1. // sqlite3Test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <windows.h>  
  6. //在stdafx.h文件中将sqlite3.h头文件引入  
  7. #ifndef SQLITE3  
  8. #define SQLITE3  
  9. #include "sqlite3.h"  
  10. //首先在stdafx.h头文件中加入显示数据库查询结果的回调函数:  
  11. int showTableInfo(void *para, int n_column, char **column_value, char **column_name);  
  12. #endif  
  13.   
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.     char *errMsg;  
  18.     int rc;  
  19.     sqlite3 *db;  
  20.     rc = sqlite3_open("manage1.db", &db);  
  21.     if (rc == SQLITE_OK)  
  22.     {  
  23.         MessageBox(NULL, _T("打开数据库成功!"), _T("消息"), MB_OK | MB_ICONWARNING);  
  24.         rc = sqlite3_exec(db, "create table if not exists user(ID integer,name varchar(32))", NULL, NULL, &errMsg);  
  25.         if (rc != SQLITE_OK)  
  26.         {  
  27.             printf("创建表失败,错误码:%d,错误原因:%sn", rc, errMsg);  
  28.             MessageBox(NULL, _T("创建表user失败!"), _T("错误"), MB_ICONWARNING);  
  29.         }  
  30.         rc = sqlite3_exec(db, "insert into user values('123','测试')", NULL, NULL, &errMsg);  
  31.         if (rc != SQLITE_OK)  
  32.         {  
  33.             MessageBox(NULL, _T("插入数据失败!"), _T("错误"), MB_ICONWARNING);  
  34.         }  
  35.         rc = sqlite3_exec(db, "select * from user", showTableInfo, NULL, &errMsg);  
  36.         if (rc != SQLITE_OK)  
  37.         {  
  38.             MessageBox(NULL, _T("查询失败!"), _T("错误"), MB_ICONWARNING);  
  39.         }  
  40.   
  41.     }  
  42.     return 0;  
  43. }  
  44.   
  45. int showTableInfo(void *para, int n_column, char **column_value, char **column_name)  
  46. {  
  47.     int i;  
  48.     printf("记录包含%d个字段 ", n_column);  
  49.     for (i = 0; i<n_column; i++)  
  50.     {  
  51.         printf("字段名:%s  >> 字段值:%s ", column_name[i], column_value[i]);  
  52.     }  
  53.     printf("--------------------------------------n");  
  54.     return 0;  
  55. }  

下载地址:http://www.sqlite.org/download.html

原文地址:https://www.cnblogs.com/ransn/p/8059327.html