C ++检查db的结果值 [英] C++ check result from db for value

查看:93
本文介绍了C ++检查db的结果值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我有一个很大的问题,如果它包含某个值并打印出来,我必须检查数据库中的结果集。但它不能像我尝试的那样工作。我可以打印结果,但我不知道如何将它保存在数组中,如果可能的话。



有没有人知道它是如何工作的?





这是我的尝试:



size_t myArraySize = sizeof( res-> getInt(1))/ sizeof(int);

int * end = res-> getInt(1)+ myArraySize;

//找到值3:

int * result = std :: find(res-> getInt(1),end,3);

if(result!= end){

cout<< 找到;

}





但它不起作用。





#include< iostream>

#include< occi.h>



使用命名空间std;



int main(){



oracle :: occi ::环境*环境;

oracle :: occi :: Connection * con;

oracle :: occi :: Statement * stmt;

oracle :: occi :: ResultSet * res;



尝试{



environment = oracle :: occi :: Environment :: createEnvironment(oracle :: occi :: Environment :: DEFAULT);

con = environment-> createConnection(user,pw,DB);



string asd =从表中选择id;

stmt = con> createStatement(asd);

res = stmt-> executeQuery();



while(res-> next())

// std :: cout<< res-> getInt(1)<<<& lt; res-> getString(2)<< std :: endl;

std :: cout<< res-> getInt(1)<<,;



size_t myArraySize = sizeof(res-> getInt(1))/ sizeof(int);

int * end = res- > getInt(1)+ myArraySize;

//找到值3:

int * result = std :: find(res-> getInt(1),结束,3);

if(结果!=结束){

cout<< 找到;

}



stmt-> closeResultSet(res);

con> terminateStatement(stmt);

environment-> terminateConnection(con);



} catch(oracle :: occi :: SQLException& e){

std :: cout<< e.what();

}



返回0;

}

编辑&运行





谢谢,



Merl

Hey guys,

I have a big problem, I have to check a result set i get from a database if it contains a certain value and print something. But it does not work the way I tried it. I can print the result, but I dont know how to save it in an array, if that is even possible.

Does anyone have an idea how it could work?


This was my try:

size_t myArraySize = sizeof(res->getInt(1)) / sizeof(int);
int *end = res->getInt(1) + myArraySize;
// find the value 3:
int *result = std::find(res->getInt(1), end, 3);
if (result != end) {
cout << "found";
}


but it does not work.


#include <iostream>
#include <occi.h>

using namespace std;

int main(){

oracle::occi::Environment* environment;
oracle::occi::Connection *con;
oracle::occi::Statement* stmt;
oracle::occi::ResultSet* res;

try{

environment = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
con = environment->createConnection("user", "pw", "DB");

string asd = "select id from table";
stmt = con->createStatement(asd);
res = stmt->executeQuery();

while (res->next())
//std::cout<<res->getInt(1)<<" "<<res->getString(2)<<std::endl;
std::cout<<res->getInt(1)<<",";

size_t myArraySize = sizeof(res->getInt(1)) / sizeof(int);
int *end = res->getInt(1) + myArraySize;
// find the value 3:
int *result = std::find(res->getInt(1), end, 3);
if (result != end) {
cout << "found";
}

stmt->closeResultSet(res);
con->terminateStatement(stmt);
environment->terminateConnection(con);

}catch(oracle::occi::SQLException &e){
std::cout<<e.what();
}

return 0;
}
Edit & Run


Thank you,

Merl

推荐答案

问:我可以打印结果,但我不知道如何将其保存在数组中



如果你可以打印你想要的东西,你已经拥有它。



把它放到一个数组中(不是我看到使用数组的意义你可能只需要创建一个定义数据的结构并存储它。



Pseudo-ish:

struct MyData {members_I_want_to_save; };

std :: vector< mydata> theStuff;



运行数据库查询。



IFF您现在拥有该套装的大小,请致电

theStuff.reserve(set_size); //条目数



对于DB查询中的每个结果,请执行以下操作:

MyData toSave;

mangleDbQueryResultInto_toSave( ...);

theStuff.push_back(toSave);



就是这样。
Q: I can print the result, but I dont know how to save it in an array

If you can print what you want, you already have it.

To put it into an array (not that I see the point of using an array over possibly more suitable collections), you simply/obviously have to create a struct defining the data, and store it.

Pseudo-ish:
struct MyData { members_I_want_to_save; };
std::vector<mydata> theStuff;

Run DB query.

IFF you have the size of the set now, DO call
theStuff.reserve(set_size); // # of entries

For each result in DB query do:
MyData toSave;
mangleDbQueryResultInto_toSave(...);
theStuff.push_back(toSave);

That's it.


这篇关于C ++检查db的结果值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆