#include <IOSTREAM>
#include <STRING>
#include <SSTREAM>
#include "sqlite3.h"
using namespace std;
static int num=0;
sqlite3* pDB;
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc;i++) { std::cout<< ++num<< " = "<< (argv[i] ? argv[i] : "NULL");
}
std::cout<< "\n";
return 0;
}
void gongneng()
{
cout<<"*********************************************************\n";
cout<<" 1. 显示功能项\n";
cout<<" 2. 显示所有的表\n";
cout<<" 3. 查询要查的表名\n";
cout<<" 4. 插入你要插入的值\n";
cout<<" 5. 更新你要更新表的值\n";
cout<<" 6. 删除你要表值的值\n";
cout<<"*********************************************************\n";
}
int select1()
{
char* errMsg;
string strSQL= "select name from sqlite_master where type='table'";
int res = sqlite3_exec(pDB,strSQL.c_str(),callback,0, &errMsg);
num=0;
if (res != SQLITE_OK)
{
std::cout << "执行SQL 出错." << errMsg << std::endl;
return -1;
}
else
{
std::cout << "SQL成功执行."<< std::endl;
}
return 0;
}
int select2(string var)
{
char *errMsg;
char **resultp; /* Result written to a char *[] that this points to */
int nrow; /* Number of result rows written here */
int ncolumn; /* Number of result columns written here */
string strSQL= "select * from " + var + ";";
int res = sqlite3_get_table(pDB,strSQL.c_str(),&resultp, &nrow, &ncolumn, &errMsg);
for(int i=0; i <=nrow; i++) {
for(int j=0; j <ncolumn; j++) {
/* the i+1 term skips over the first record,
which is the column headers */
cout<<resultp[i*ncolumn + j]<<" ";
}cout<<endl;
}
if (res != SQLITE_OK)
{
std::cout << "执行SQL 出错." << errMsg << std::endl;
return -1;
}
else
{
std::cout << "SQL成功执行."<< std::endl;
}
return 0;
}
int insert(string var)
{
char *errMsg;
char **resultp; /* Result written to a char *[] that this points to */
int nrow; /* Number of result rows written here */
int ncolumn; /* Number of result columns written here */
string x;
string y;
string p;
cout<<"请输入你的值:\n";
cin>>x>>y>>p;
cout<<endl;
string strSQL= "insert into " + var + " values ('" + x + "','" + y + "','" + p + "');";
int res = sqlite3_get_table(pDB,strSQL.c_str(),&resultp, &nrow, &ncolumn, &errMsg);
/*for(int i=0; i <=nrow; i++) {
for(int j=0; j <ncolumn; j++) {
/* the i+1 term skips over the first record,
which is the column headers */
//cout<<resultp[i*ncolumn + j]<<" ";
//}cout<<endl;
//}*/
if (res != SQLITE_OK)
{
std::cout << "执行SQL 出错." << errMsg << std::endl;
return -1;
}
else
{
std::cout << "SQL成功执行."<< std::endl;
}
return 0;
}
int delete1(string var)
{
char *errMsg;
char **resultp; /* Result written to a char *[] that this points to */
int nrow; /* Number of result rows written here */
int ncolumn; /* Number of result columns written here */
string x;
string y;
string p;
cout<<"请输入你要删除的字段:\n";
cin>>x;
cout<<"请输入你要删除的值:\n";
cin>>y;
cout<<endl;
string strSQL= "delete from " + var + " where " + x + " = '" + y + "';";
int res = sqlite3_get_table(pDB,strSQL.c_str(),&resultp, &nrow, &ncolumn, &errMsg);
/*for(int i=0; i <=nrow; i++) {
for(int j=0; j <ncolumn; j++) {
/* the i+1 term skips over the first record,
which is the column headers */
//cout<<resultp[i*ncolumn + j]<<" ";
//}cout<<endl;
//}*/
if (res != SQLITE_OK)
{
std::cout << "执行SQL 出错." << errMsg << std::endl;
return -1;
}
else
{
std::cout << "SQL成功执行."<< std::endl;
}
return 0;
}
int update(string var)
{
char *errMsg;
char **resultp; /* Result written to a char *[] that this points to */
int nrow; /* Number of result rows written here */
int ncolumn; /* Number of result columns written here */
string m;
string n;
//string q;
string k;
cout<<"请输入要更新的字段:\n";
cin>>m;
cout<<"请输入要更新的新值:\n";
cin>>n;
cout<<"请输入要更新的旧值:\n";
cin>>k;
cout<<endl;
string strSQL= "update " + var + " set " + m + " = " + n + " where " + m + " = " + k;
int res = sqlite3_get_table(pDB,strSQL.c_str(),&resultp, &nrow, &ncolumn, &errMsg);
/*for(int i=0; i <=nrow; i++) {
for(int j=0; j <ncolumn; j++) {
/* the i+1 term skips over the first record,
which is the column headers */
//cout<<resultp[i*ncolumn + j]<<" ";
//}cout<<endl;
//}
if (res != SQLITE_OK)
{
std::cout << "执行SQL 出错." << errMsg << std::endl;
return -1;
}
else
{
std::cout << "SQL成功执行."<< std::endl;
}
return 0;
}
int main(int argc, char **)
{
/*if (argc < 2)
{
std::cout << "请输入命令行参数:sqlite数据库名." << std::endl;
return 0;
}*/
int res = sqlite3_open("me.db", &pDB);
if( res ){
std::cout << "Can't open database: "<< sqlite3_errmsg(pDB);
sqlite3_close(pDB);
return -1;
}
system("cls");
while(1){
cout<<"*********************************************************\n";
cout<<" 1. 显示功能项\n";
cout<<" 2. 显示所有的表\n";
cout<<" 3. 查询要查的表名\n";
cout<<" 4. 插入你要插入的值\n";
cout<<" 5. 更新你要更新表的值\n";
cout<<" 6. 删除你要表值的值\n";
cout<<" 7. 退出程序\n";
cout<<"*********************************************************\n";
//select1();
int data;
string var;
cout<<"请输入序号:"<<endl;
cin>>data;
if(data==1)
{
gongneng();
}
if(data==2)
{
system("cls");
select1();
}
if(data==3){
system("cls");
cout<<"请输入您要查询的表名:\n";
cin>>var;
cout<<endl;
select2(var);
}
if(data==4)
{
system("cls");
cout<<"请输入您要插入值的表:\n";
cin>>var;
insert(var);
}
if(data==5)
{
system("cls");
cout<<"请输入你要更新的值的表:\n";
cin>>var;
update(var);
}
if(data==6)
{
system("cls");
cout<<"删除你要表值的值:\n";
cin>>var;
delete1(var);
}
if(data==7)
{
return 0;
}
//system("cls");
}
return 0;
}
sql-example.rar_mfc开发sql_sql MFC
版权申诉
37 浏览量
2022-09-20
19:43:58
上传
评论
收藏 1.68MB RAR 举报
Kinonoyomeo
- 粉丝: 77
- 资源: 1万+
最新资源
- 基于C++和Yolo5检测和React前端开发的人流量检测系统源码+文档说明+详细注释(高分项目)
- C#毕业设计-跟踪机器人运动坐标并可视化路径轨迹源码+数据库
- 基于浏览器JS做路径跟踪渲染+源代码+界面截图
- 基于USV路径跟踪LOS控制算法matlab仿真源码+详细注释(下载直接使用)(高分项目)
- CentOS7的docker镜像
- 基于C++和Pure-pursuit算法实现的路径跟踪和给予LQR的轨迹跟踪+源代码+文档说明+脚本(高分项目)
- 基于python实现的路径跟踪控制实现的项目源代码+文档说明(高分课程设计)
- 基于神经网络的虚假评论识别系统(Python源码+文档资料+数据集+代码流程说明文档+详细注释)
- 科大讯飞开发者大赛锂离子电池生产参数调控及生产温度预测挑战赛记录python源码
- 基于Python通过神经网络训练锂离子电池使用相关数据,预测电池当前最大容量+源代码+文档说明(毕业设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈