#include <iostream>
#include <string>
#include <sstream>
#include "sqlite3.h"
using namespace std;
sqlite3 * pDB;
int createTable()
{
char* errMsg;
string dropTab = "drop table test_tab;";
string strSQL= "create table test_tab (f1 int, f2 long);";
//删掉已经存在的table
int res= sqlite3_exec(pDB , dropTab.c_str() , 0 , 0 , &errMsg);
if (res != SQLITE_OK)
{
cout << "执行SQL出错." << errMsg << endl;
return -1;
}
//建立一个空table
res = sqlite3_exec(pDB , strSQL.c_str() ,0 ,0, &errMsg);
if (res != SQLITE_OK)
{
cout << "执行创建table的SQL出错." << errMsg << endl;
return -1;
}
else
{
cout << "创建table的SQL成功执行."<< endl;
}
return 0;
}
int insert1()
{
char* errMsg;
int res = sqlite3_exec(pDB,"begin transaction;",0,0, &errMsg);
for (int i= 1; i < 10; ++i)
{
stringstream strsql;
strsql << "insert into test_tab values(";
strsql << i << ","<< (i+10) << ");";
string str = strsql.str();
res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
if (res != SQLITE_OK)
{
cout << "执行SQL出错." << errMsg << endl;
return -1;
}
}
res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);
cout << "SQL成功执行."<< endl;
return 0;
}
//arg---数据表内的数据行数
//argv---行数据
//azColName---属性名
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
for(int i = 0 ; i < argc ; i++)
{
//读的是一行数据?
cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << ", " ;
}
cout<< "\n";
return 0;
}
int select1()
{
char* errMsg;
string strSQL= "select * from test_tab;";
int res = sqlite3_exec(pDB, strSQL.c_str(), callback , 0 , &errMsg);
if (res != SQLITE_OK)
{
cout << "执行SQL 出错." << errMsg << endl;
return -1;
}
else
{
cout << "SQL成功执行."<< endl;
}
return 0;
}
int main(int argc, char* argv[])
{
int res = sqlite3_open("D:\\sql.db", &pDB);
if( res ){
cout << "Can't open database: "<< sqlite3_errmsg(pDB);
sqlite3_close(pDB);
return -1;//返回-1和0之间有什么区别吗?
}
res = createTable();
if (res != 0)
{
return 0;
}
res = insert1();
if (res != 0)
{
return 0;
}
select1();
getchar();
return 0;
}
c++写的sqlit数据库的实例程序
2星 需积分: 10 160 浏览量
2008-11-28
09:02:13
上传
评论
收藏 5.51MB RAR 举报
zlltalent
- 粉丝: 0
- 资源: 8
最新资源
- 电子设计项目毕业设计及产品设计资料开关电源论文资料
- 模型预测控制(MPC)基准测试问题的开放集合Open collection of model predictive contro
- 基于用户的协同过滤算法实现的商品推荐系统
- TI杯大奖赛本科组+研究生组论文+源代码+PCB自学习式走迷宫智能小车
- TI杯大奖赛本科组+研究生组论文+源代码+PCB专业组-仪器仪表类-兰州大学-基于MSP430的心电监控系统
- graphab-2.6英文用户手册
- 修正正弦波逆变器原理图
- pyltp-0.2.1-cp36+pyltp-0.4.0-cp36.zip
- TI杯大奖赛本科组+研究生组论文+源代码+PCB专业组-先进控制类-电子科技大学-基于TMS320F28027的新能源可控整流与
- 基于python的五子棋游戏编程代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈