Support #1335
closedWt database search - only first hit displayed
0%
Description
Dear developers, experts,
I'm trying to create a function to perform a search in a given database.
The code attached works, compiling without problems.
The strange thing of this situation is the number of results(N) is correct, but when I'd like to print out the results, only the first hit is displayed N times.
Could you give me some advice, where could be the mistake?
Thank you in advance,
Daniel
My code is:
#ifndef Search_H
#define Search_H
#include
#include
#include
#include
#include
#include
#include
using namespace Wt;
using namespace std;
namespace dbo = Wt::Dbo;
template
class Search {
public:
Search( const char *table_name ) {
session = new dbo::Session();
connection = new dbo::backend::Sqlite3( table_name );
connection->setProperty("show-queries","true");
session->setConnection( *connection );
session->mapClass(table_name);
}
~Search() {
delete connection;
delete session;
}
vector<Wt::Dbo::ptr > search( ) {
unsigned int i=0;
string query;
cerr << "Search in database..." << endl;
dbo::Transaction transaction(*session);
Wt::Dbo::collection< Wt::Dbo::ptr > tCollection = session->find();
vector<Wt::Dbo::ptr > tVector(tCollection.begin(), tCollection.end());
for (unsigned int i=0; i<tVector.size(); i) {
cerr << tVector[i]code << \" \" << tVector[i]>chainid << endl;
return tVector;
}
protected:
dbo::Session *session;
dbo::backend::Sqlite3 *connection;
};
#endif //Search_H
Updated by Koen Deforche over 12 years ago
- Assignee set to Koen Deforche
Hey,
Could it be you're simply missing the persist() method and thus you get only default constructed objects ?
Regards,
koen
Updated by Daniel Kozma over 12 years ago
Dear Koen,
Thank you for your answer.
I've got persist() function, but not in Search.h. My persist() function is in Hit.h.
I use Hit class in template.
I'd like to call the search function from a context like:
Search<Hit> *search = new Search<Hit>("pdbtm");
Hits hits = search->search( params );
cerr << "search results count: " << hits.size();
for (unsigned int i=0; i<hits.size(); i++)
cerr << hits[i]->code << " " << hits[i]->chainid << endl;
And my Hit.h, which contains the persist function and define the data structure is the following:
Type 'Hits' is defined end of this file also.
#ifndef __Hit_H__
#define __Hit_H__
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/Session>
#include <Wt/Dbo/Transaction>
#include <Wt/Dbo/backend/Sqlite3>
#include <Wt/Dbo/Types>
#include <Wt/Dbo/WtSqlTraits>
#include <time.h>
using namespace Wt;
using namespace std;
namespace dbo = Wt::Dbo;
class Hit : public dbo::Dbo<Hit> {
public:
Hit() {
};
~Hit() {
}
template<class Action>
void persist(Action& a) {
Wt::Dbo::field(a, code, "code");
Wt::Dbo::field(a, chainid, "chainid");
}
string code;
string chainid;
};
typedef Wt::Dbo::collection< Wt::Dbo::ptr<Hit> > HitCollection;
typedef vector<Wt::Dbo::ptr<Hit> > Hits;
#endif
Thank you for your help in advance!
Best regards,
Daniel
Updated by Koen Deforche over 12 years ago
Hey,
Your code seems fine. I can only imagine that there is something wrong with the data, e.g. the 'id' column does not have unique values ? [that shouldn't be possible for a column that is primary key but Sqlite3 has some quirks in that area] Does the data look correctly when doing the query in sqlite3 directly ?
Regards,
koen
Updated by Daniel Kozma over 12 years ago
Dear Koen,
I've checked with SQLite Database Browser, search works well for the same query:
select * from pdbtm where code = '2bl2'
The data and the database seem correct.
Regards,
Daniel
Updated by Daniel Kozma over 12 years ago
Dear Koen,
I've found the solution.
I did not have the two auxilary column in my databese: ID, VERSION.
So, you had right, the issue is caused by the wrong structure of my database.
Thank you very much!
Best regards,
Daniel
Updated by Koen Deforche over 12 years ago
- Status changed from Feedback to Resolved
Updated by Koen Deforche over 12 years ago
- Status changed from Resolved to Closed
- Target version set to 3.2.2
Released in Wt 3.2.2