0

For a Quiz game I would like to store the questions in a database containing 7 columns for the Primary key, Question, 4 answers, and the correct answer then run a random method that would pick on of the questions and populate several text fields with the data.

I would then store the value for the "correct answer" column in a variable for a if statement called later on when the user selects an answer.

I have attempted so many tutorials involving SQLite databases that I am very unclear how to do the following:

  1. Connect my xcode project to the database (a read-only database stored in the resources folder)

  2. Load the database into an object (or load each row at run time at each question to save memory?)

  3. Make a method that randomly selects a row, then populates text fields with the data and stores the correct answer column into a variable.

I have been banging my head on my desk for several weeks now, so ANY help would be GREATLY appreciated!

Thanks guys!

1
  • 1
    There are lots of good references for using SQLite and I will try to find you one but have you considered core data? Commented Mar 29, 2011 at 17:53

1 Answer 1

2

iPhone Programming Tutorial – Creating a ToDo List Using SQLite Part 1

iPhone SDK Tutorial: Reading data from a SQLite Database

Selecting a Random Row in SQLite

SELECT * FROM table ORDER BY RANDOM() LIMIT 1;

And here is Core Data and How to select a random row if you want to give that a try

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Joe, Thanks for your quick reply. I've actually gone through both of those tutorials and the first is built around having a database in a tableView that is writable, and the second is based around a TableView application. In both of those tutorials they load the database data into a TableView object with handles all the database rows. I need to be able to specify what row I want to load (which I will do with a random method) then take the information in that rows columns and populate my text fields with it.
You will retrieve your rows the same as done in the example. Then you will be able to get a string like so NSString *myQuizQuestion = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)] and display it like this myQuizQuestionLabel.text = myQuizQuestion.
Hi Joe, thanks again for your patience (I'm fairly new in Xcode). I've looked into CoreData as well, its just seems more out of my "relm" of understanding. I think what I'm really looking for is a step by step idea of how to do what I'm looking for, but I can't expect someone (like your self) to take the time to give a complete stranger a detailed step-by-step on how to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.