PDA

View Full Version : Converting DB to code



Atomic_Sheep
18th August 2013, 11:26
Hi guys,

I'm trying to use values in my database as arguments for functions e.g. I have a column with colours e.g. red, blue etc and I would like to use these values as arguments into a function.

In my header file, I've got #define red 7 etc etc, however the only way that I can think of using the database values would be a conversion function such as:


int MyClass::Conversion(QString sDatabaseColumnText)
{
switch(sDatabaseColumnText)
case "red":
{
return 7;
}
}

and then my function would just have:



function(Conversion(query.value(0).toString();


I haven't compiled the code so I'm not sure if there are syntax errors, but it's the ideas that I'm more interested in.

I obviously don't want to create a table in my database whereby I have an 'int' value assigned to my values, because this will double up my #defines and it's prone to errors and becomes an updating nightmare.

EDIT P.S. I just remembered that cases can only use ints so my use of strings won't work, but anyway, imagine that I change my example to a bunch of 'if' statements.

Santosh Reddy
23rd August 2013, 12:47
This is tipical case to use map<int, string>