Forums

OverviewV-Play 2 Support (Qt 5) › How do I create a database?

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #8837

    Thanh

    Hello everyone!

    Sorry if this is a dumb question but I’m new around here.  I’ve been reading tutorials and been wanting to make a trivia game and need to store the questions somewhere.  I searched one topic on databases but I still didn’t quite understand how to create and pull from the database.  I’m coming from a web developing background so I know how to pull using queries with mySQL and PHP but I don’t know how this is translated to game development.  Any help is appreciated!

    #8843

    spike

    Regarding MySQL you probably have to create some C++ classes and expose it to the QML context. See here.

    If you only need local storage you can use the Storage class provided by V-Play or refer to the Local Storage Module from Qt.

    I don’t know the actual difference between those two and if it’s just a wrapper, but to me it seems that the latter leaves more control in your hands, while the first one is rather simple and basic.

    #8845

    Thanh

    Thanks spike!  I’ll try to implement it.  So for example could do:

     

    Storage {
        id: myLocalStorage
        databaseName: "myStorage"
        setValue(1, "Apple", "Red, round, delicious")
      }

    If I theoretically wanted to store fruit with an id as a key, the name of the fruit as a value, and a description of the fruit as another value?  The parameters for setValue are (key, value) how would I add more values or “columns”?

     

     

     

    #8846

    spike

    Let’s say you have a Fruit object:

    var fruit = {id: 1, value: 100, description: "Some text."};

     

    Now, when you call myLocalStorage#setValue you can take the id as the key and the whole object as the value.

    With JSON.stringify(object) you get a JSON string of the object.

    JSON.stringify(fruit);
    => '{"id":1,"value":100,"description":"Some text."}'

     

    So we have

    myLocalStorage.setValue(fruit.id, JSON.stringify(fruit));

     

    Now, in order to reconstruct your fruit from JSON, simply retrieve the value and call JSON.parse on the string:

    var JSONifiedFruit = myLocalStorage.getValue(1);
    var fruit = JSON.parse(JSONifiedFruit);

     

    I hope it helps!

    #8847

    Martin

    Nice one spike!    They could almost do with an example of this – copying yours – in the doco, probably on the Storage page.

     

    • This reply was modified 3 years, 5 months ago by  GreenAsJade.
    #8851

    Thanh

    Thank you so much for explaining in detail spike!  This helps me go a long way.

    #8852

    Christian
    V-Play Team

    Hi,

    thanks for the sample and great explanation spike!

    You actually do not need to call JSON.stringify(), because this is done automatically in setValue(), and it then gets parsed with JSON.parse() again in getValue().

    We’ll add this example to the Storage documentation as Martin suggested, to help make it more clear.

    One more thing regarding the difference between the Storage class provided by V-Play and the Local Storage Module from Qt:

    Storage is a simple key/value store wrapper and uses the Local Storage Module internally. For most use cases, having simple key/value data is sufficient. If you have large databases though and want to make complex search queries, using the SQL syntax for these queries with the Local Storage Module is the better option. We’ll also add that to the docs. 😉

    Cheers, Chris

    • This reply was modified 3 years, 5 months ago by  Christian.
Viewing 7 posts - 1 through 7 (of 7 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support