0

I'm working on a trivia like app and wondering how is the best way to store all of the questions and answers. Right now, I just have a random number and using a whole lot of if statements. For example, if randomNum = 25, then question is THIS and choices are THIS. This seems to work fine, but my file is starting to get very large and this seems like it should cause performance issues. Space is also starting to become an issue. I have started to look into just putting all of the data into database and use a random number to just retrieve a row. Anybody have any suggestions on which would be the best practice or have any other ways of doing this?

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274

3 Answers3

2

Sounds like its a good time to start using the database. You can learn how to include a pre-populated database here.

Community
  • 1
  • 1
Haphazard
  • 10,900
  • 6
  • 43
  • 55
0

...using a whole lot of if statements.

I have started to look into just putting all of the data into database and use a random number to just retrieve a row

I think you've kinda answered the question yourself.

What happens with your model if you have 10,000 questions? Are you going to use 10,000 'if' statements?

Even if you're never going to get to that many questions, using a SELECT on a DB where the question number equals a particular random number, is going to be far more extensible.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
0

You should use the database.

It's not just a maintainability and (ultimately) a code simplicity option, either, but offers significant advantages.

Imagine if you want to be able to supply different packs of questions, for example. You could offer people the ability to download a trivia pack from a website, or load it from a file off their SDcard. This simply would not work for masses of if statements.

Suppose you want to let people add their own trivia questions? Upload them to the website for voting and ultimate inclusion into crowd-sourced question packs.

So yeah: you should use a database.

karora
  • 1,223
  • 14
  • 31