关于vs2010中mfc使用sqlite3
来源:互联网 发布:nba十大得分后卫 知乎 编辑:程序博客网 时间:2024/04/29 02:58
今天写下关于vs2010中mfc使用sqlite3的文章,本人水平有限,有错或改进的,尊请各位看官指出啊^_^。
不费话,不多说,有关sqlite的详细可以百度、google哈。本文是讲如何连接使用sqlite的,切入正题,请准备好工具!
第一步:
到Sqlite的官方网站(http://www.sqlite.org/download.html)下载http://www.sqlite.org/snapshot/sqlite-amalgamation-201308152240.zip源文件。
下载完没?OK,下面我们要制作dll文件,要有sqlite.lib才能使用!
①用vs2010 文件—新建项目—Win32—Win32项目,项目取名叫”sqlite”吧
下一步:选DLL(D),再勾空项目
OK,把sqlite3ext.h、sqlite3.h、sqlite.c这三个东东复制到工程里(不要shell.c的哦)
OK,在 配置管理器 里改为release
OK,按F5编译!
②到Release目录下把sqlite.lib连同sqlite3ext.h、sqlite3.h、sqlite.c一起复制到你要使用数据库里的工程里,在工程资源文件里添加sqlite.lib
③ 这你懂的
extern "C"{#include "sqlite3.h"};#pragma comment(lib,"sqlite.lib")/*为什么要 extern “C” ?这是C++的基础。要在 C++里使用一段 C 的代码,必须要用 extern “C” 括起来。C++跟 C虽然语法上有重叠,但是它们是两个不同的东西,内存里的布局是完全不同的,在C++编译器里不用extern “C”括起C代码,会导致编译器不知道该如何为 C 代码描述内存布局。可能在 sqlite3.c 里人家已经把整段代码都 extern “C” 括起来了,但是你遇到一个.c 文件就自觉的再括一次,也没什么不好。*///-----------------------------------------------------OK,尽情的爽吧---------------------------------------------------//下载是我简单作用sqlite3的代码,不好看,请包涵! 详细教程在这:http://download.csdn.net/detail/q775968375/5970059void CMySqliteDlg::OnBnClickedOk(){// TODO: 在此添加控件通知处理程序代码//CDialogEx::OnOK();CTime m_time; m_time=CTime::GetCurrentTime();//获取当前时间日期 FILE *fp=fopen("debug.txt","a+");fprintf(fp,"\n------------------------%s------------------------\n",m_time.Format(_T("%Y-%m-%d %H:%M:%S %A")));::sqlite3 *db=NULL;::sqlite3_stmt *stmt;char *sql=NULL,*error=NULL;const char *tail=NULL;int res;res=::sqlite3_open("MySqlite.db",&db);//打开数据库(不存在创建)if(res!=SQLITE_OK){ ::sqlite3_close(db);fprintf(fp,"open database is failure:%s\n",::sqlite3_errmsg(db));return;}else fprintf(fp,"open database is success!\n"); //create table sql="create table if not exists MyTable(Row integer primary key autoincrement,Content text)";res=::sqlite3_exec(db,sql,NULL,NULL,&error);if(res!=SQLITE_OK){ ::sqlite3_close(db); fprintf(fp,"error create table:%s %s\n",error,::sqlite3_errmsg(db)); return;}else fprintf(fp,"table is created\n");//insertsql="insert or replace into MyTable(Row,Content) values(?,?)";//sqlite3_prepare_v2的作用是将UTF-8格式的SQL语句转换为编译后的语句,并返回指向该语句的指针 res=::sqlite3_prepare_v2(db,sql,-1,&stmt,&tail);if(res!=SQLITE_OK){ ::sqlite3_close(db); fprintf(fp,"insert is failure:%s %s\n",tail,::sqlite3_errmsg(db)); return;}for(int i=0;i<3;i++){//::sqlite3_bind_int(stmt,1,0);//1:id 2:data::sqlite3_bind_text(stmt,2,"数据库数据",-1,NULL);if(::sqlite3_step(stmt)!=SQLITE_DONE){fprintf(fp,"error update table:%s\n",::sqlite3_errmsg(db)); }::sqlite3_reset(stmt); //重新初始化该sqlite3_stmt对象绑定的变量。}::sqlite3_finalize(stmt);//deletesql="delete from MyTable where Row=2";//res=::sqlite3_prepare_v2(db,sql,-1,&stmt,&tail);res=::sqlite3_exec(db,sql,NULL,NULL,&error);if(res!=SQLITE_OK){ ::sqlite3_close(db); fprintf(fp,"delete error:%s %s\n",error,::sqlite3_errmsg(db)); return;}else fprintf(fp,"delete is success\n");::sqlite3_finalize(stmt);//updatesql="update MyTable set Content='修改后的数据' where Row=3";res=::sqlite3_exec(db,sql,NULL,NULL,&error);if(res!=SQLITE_OK){ ::sqlite3_close(db); fprintf(fp,"update error:%s %s\n",error,::sqlite3_errmsg(db)); return;}else fprintf(fp,"update is success\n");::sqlite3_finalize(stmt);//querysql="select Row,Content from MyTable order by Row";res=::sqlite3_prepare_v2(db,sql,-1,&stmt,&tail);if(res!=SQLITE_OK){ ::sqlite3_close(db); fprintf(fp,"query is failure:%s %s\n",tail,::sqlite3_errmsg(db)); return;}while(::sqlite3_step(stmt)==SQLITE_ROW){static int cnt=::sqlite3_column_count(stmt);for(int i=0;i<cnt;i++){fprintf(fp,"Data:%s\n",::sqlite3_column_text(stmt,i));}/*int row=::sqlite3_column_int(stmt,0);char *rowData=(char*)::sqlite3_column_text(stmt,1);if(rowData==NULL)rowData="NULL";*/}//释放statement::sqlite3_finalize(stmt);//关闭数据库::sqlite3_close(db);::fclose(fp);}
0 0
- 关于vs2010中mfc使用sqlite3
- 关于vs2010中mfc使用sqlite3
- VS2010/MFC sqlite3
- vs2010 C# 使用SQLite3
- VS2010 在共享 DLL 中使用 MFC
- vs2010 MFC中listcontrol控件的使用
- VS2010 MFC 中线程的使用
- SQLITE3 在VC/MFC 中使用的一点体会
- SQLITE3 在VC/MFC 中使用的一点体会
- 在MFC应用程序中通过CppSQLite3来使用SQLite3
- SQLITE3 在VC/MFC 中使用的一点体会
- 【MFC-9】VS2010/MFC基于对话框的SQlite3数据库编程
- 关于vs2010 mfc中CFile的一些用法
- VS2010/MFC flexcell使用
- 使用VS2010编译sqlite3,生成静态的sqlite3.lib
- 关于MFC中Static Text的使用。VS2008、VS2005、VS2010、error C2065: “IDC_STATIC5”: 未声明的标识符
- 关于vs2010中使用本地帮助
- 关于VC++中sqlite3的配置和简单使用小结
- 自学Linux系统的小总结(一)
- 寒假10days总结
- 作业3 DDS模块设计
- MxNet系列——how_to——cloud
- centos 7 上mysql部署
- 关于vs2010中mfc使用sqlite3
- 算法训练 最小乘积(基本型)
- python 环境变量配置
- Redis基础教程三
- MxNet系列——how_to——caffe
- java中javamail收发邮件实现方法
- 云计算总结1:IAAS架构方面
- linux查找目录下的所有文件中是否含有某个字符串
- MxNet系列——how_to——bucketing