PDA

View Full Version : Can you add a custom object to a SQL database?



agerlach
28th June 2010, 21:52
I have the following class:

class siStore
{
public:
siStore();
~siStore();

QVector<float> *si;
float siSum;
float si2Sum;
int length;
int siWidth;
}
(http://doc.trolltech.com/latest/class%20sistore%3Cbr%20/%3E%0A%7B%3Cbr%20/%3E%0Apublic:%3Cbr%20/%3E%0A%20%20%20sistore%28%29;%3Cbr%20/%3E%0A%20%20%20%7Esistore%28%29;%3Cbr%20/%3E%0A%3Cbr%20/%3E%0A%20%20%20qvector%3Cfloat?%20*si;%3Cbr%20/%3E%0A%20%20%20float%20sisum;%3Cbr%20/%3E%0A%20%20%20float%20si2sum;%3Cbr%20/%3E%0A%20%20%20int%20length;%3Cbr%20/%3E%0A%20%20%20int%20siwidth;%3Cbr%20/%3E%0A%7D%3Cbr%20/%3E.html)
In my program I then create a QList<siStore*> called siStack.

In practice, the application generates a "training set" called siStack based on a few training parameters and an input 3D surface mesh. Each surface mesh may require multiple "training sets" using differing parameters.

I envision my database to look like this: Table1 =name[ modelID modelName], Table 2 = parameters[ paramID modelID Param1 Param2 ...], Table 3 = trainingSet[ paramID siStack].

Is this possible, or even make sense? From what I read I need to use a BLOB type, but how do I convert my object to a BLOB and how to I later read the BLOB and convert it to a QList<siStore*>?

Thanks

Lykurg
28th June 2010, 22:11
Create a << and >> operator for your class siStore, then you can easily store your list siStack to a database and read it back in (using the operators...).

agerlach
29th June 2010, 04:14
That makes sense from what I have read, but how do I go about doing that? Could you give me a brief example?

Lykurg
29th June 2010, 10:41
Try something like that as a starting point
QDataStream& operator<<(QDataStream& stream, const siStore& class)
{
stream << siStore.siSum << class.si2Sum() << ...;
return stream;
}

agerlach
30th June 2010, 15:51
hmm... Unfortunately I'm away from my development PC and am unable to try your suggestion. But just to make sure I understand everything correctly, you would append all the members of my class to a QDataStream which would then be a binary encoding and could directly be used as a BLOB? Something like this, where siStack is a BLOB:


QSqlQuery query;
query.prepare("INSERT INTO trainingSet (id, siStack) "
"VALUES (:id, :siStack)");
query.bindValue(":id", 1001);
query.bindValue(":siStack", myQDataStream);
query.exec();