PDA

View Full Version : Alternative to SQLite for storing basic data



KeineAhnung
23rd May 2014, 07:09
Hi,

I have created a SQLite database with a lot of default values for my program. My initial plan was to add the db file to my resources but I just learned that this is not really working. Currently I do not plan for the possibility to change or add to the default values. So what would be the right way or format to to store the defaults? And how can I convert the SQLite database?

Lesiok
23rd May 2014, 08:13
Some INI file and QSettings ?

KeineAhnung
23rd May 2014, 10:06
I guess with a lot of work this would be possible but I think it is not very handy. In case I want to add a property I would have to go through 50 groups and manually add the new property. I thought about exporting the SQLite db to something like a xml file and to include that...

anda_skoa
23rd May 2014, 15:33
You could install the SQLite file with your application or, using your current approach, extract it (from resources) to a local file if not present as a file yet.

Cheers,
_

folibis
25th May 2014, 23:47
I like to use XML files for this purpose. It can be changed with any editor. You just need some simple wrapper class to load/save your data.

KeineAhnung
26th May 2014, 13:07
Hi folibis,

I have been looking more in depth into xml and found this nice tutorial (http://developer.nokia.com/community/wiki/QXmlStreamReader_to_parse_XML_in_Qt) that explains how to read from a file. So what do you do with your xml files? Do you load them into a 2D vector and use this or do you extract the data you want directly from the xml file?

A little about my situation. I have this xml file now:


<?xml version="1.0" encoding="UTF-8" ?>
<cards>
<card>
<type>green</type>
<class>witcher</class>
[...]
</card>
<card>
<type>jellow</type>
<class>witcher</class>
[...]
</card>
[...]
</cards>

So each card is defined by it type and class. I will keep adding cards to this file as my projects grows so I will never know how many types and classes there will be.
Currently, to create a new card I have an UI with two QComboBoxes. I fill the first one with this SQLite statement: qry->prepare("SELECT DISTINCT type FROM cards ORDER BY type"); and the second one depending from the first like this: qry->prepare("SELECT class FROM cards WHERE type='"+arg1+"' ORDER BY class"); where arg1 is the current text from the first QComboBox.
Could I do something similar with the xml file or do I have to go through the entire file picking out the types and classes every time something changes?
If Qt does not support anything like this, would it be possible to load the data into a vector and search the vector for the information I want?

I would appreciate your thoughts on this. Thanks

KeineAhnung
26th May 2014, 20:43
Hi,

I got a little further. Using the tutorial I got the code so far that it reads the xml file. My problem is now to get the data into my custom class and then into a vector. The tutorial uses if statements to determent where the information should go. With two elements thats okay but I got more than 40 and that is just not very handy. Here is the code snippet where the data needs to be transferred:


/* If it's named card, we'll dig the information from there.*/
if(xml.name() == "card") {
xml.readNext();
Card card;
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "card")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
QString elementName = xml.name().toString();
xml.readNext();
qDebug()<< elementName << "->" << xml.text().toString();
}
/* ...and next... */
xml.readNext();
}
cardData.append(card);
}

In php card would be an associative array and I would fill it $card[elementName] = xml.text().toString(); can I do something similar in Qt? By the way, card is not defined yet. I was not sure how this class should look like so that I can store the information.

folibis
27th May 2014, 05:00
Yeah, associative array is good idea in your case.
You can use QMap or QHash, documentation and examples you can found in QtAssistant.

KeineAhnung
27th May 2014, 13:16
Hi folibis,

thanks for the tip with the QHash. This works perfectly.

I crated a QHash<QString, QString> card; and filled that using card[elementName] = xml.text().toString();. The card is then stored in a QVector< QHash<QString, QString> > cardData;