Results 1 to 9 of 9

Thread: Alternative to SQLite for storing basic data

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Alternative to SQLite for storing basic data

    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?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to SQLite for storing basic data

    Some INI file and QSettings ?

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Alternative to SQLite for storing basic data

    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...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to SQLite for storing basic data

    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,
    _

  5. #5
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to SQLite for storing basic data

    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.

  6. #6
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Alternative to SQLite for storing basic data

    Hi folibis,

    I have been looking more in depth into xml and found this nice tutorial 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:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <cards>
    3. <card>
    4. <type>green</type>
    5. <class>witcher</class>
    6. [...]
    7. </card>
    8. <card>
    9. <type>jellow</type>
    10. <class>witcher</class>
    11. [...]
    12. </card>
    13. [...]
    14. </cards>
    To copy to clipboard, switch view to plain text mode 
    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

  7. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Alternative to SQLite for storing basic data

    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:
    Qt Code:
    1. /* If it's named card, we'll dig the information from there.*/
    2. if(xml.name() == "card") {
    3. xml.readNext();
    4. Card card;
    5. while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "card")) {
    6. if(xml.tokenType() == QXmlStreamReader::StartElement) {
    7. QString elementName = xml.name().toString();
    8. xml.readNext();
    9. qDebug()<< elementName << "->" << xml.text().toString();
    10. }
    11. /* ...and next... */
    12. xml.readNext();
    13. }
    14. cardData.append(card);
    15. }
    To copy to clipboard, switch view to plain text mode 
    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.

  8. #8
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to SQLite for storing basic data

    Yeah, associative array is good idea in your case.
    You can use QMap or QHash, documentation and examples you can found in QtAssistant.

  9. The following user says thank you to folibis for this useful post:

    KeineAhnung (27th May 2014)

  10. #9
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Alternative to SQLite for storing basic data

    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;

Similar Threads

  1. Problem in storing hex data to char variable
    By prasad1001 in forum Newbie
    Replies: 1
    Last Post: 7th April 2014, 07:16
  2. Data Reading and Storing in arrays
    By kango in forum Newbie
    Replies: 8
    Last Post: 6th January 2013, 15:13
  3. Saving and storing data,QtSql
    By salmanmanekia in forum Newbie
    Replies: 7
    Last Post: 20th April 2010, 19:08
  4. storing data in hexadecimal format
    By aj2903 in forum Qt Programming
    Replies: 6
    Last Post: 13th January 2010, 05:29
  5. Replies: 1
    Last Post: 3rd December 2009, 09:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.