How do i identify multiple sqlite databases in my app at run time. I am making a quiz app for iphone in which i need 5-6 sqlite databases, one for each category(subject) of quiz. Help anyone
-
I don't really think you need different databases for each category. This sounds like a really bad database design to me. At this stage in your project, is remodeling the database structure an option? You could do it in a better wayBBog– BBog2012-09-26 12:43:07 +00:00Commented Sep 26, 2012 at 12:43
-
Yes, i can remodel it in any way u advice..please tell me the wayuser1700171– user17001712012-09-26 13:03:23 +00:00Commented Sep 26, 2012 at 13:03
-
I gave you an alternate answerBBog– BBog2012-09-26 13:58:17 +00:00Commented Sep 26, 2012 at 13:58
2 Answers
Use FMDB that is easy.
Identifing a database is simple, i dont't understand the problem...
FMDatabase *dbQuestions = [FMDatabase databaseWithPath:@"/tmp/tmpA.db"];
FMDatabase *dbAnswers = [FMDatabase databaseWithPath:@"/tmp/tmpB.db"];
FMDatabase *dbPeople = [FMDatabase databaseWithPath:@"/tmp/tmpC.db"];
[...]
[dbQuestions open]
[dbAnswers open]
[dbPeople open]
[...]
cheers.
Comments
Since this would be too long to post as a comment and it does somehow address your question, I would suggest again to use a different structure for your application because having multiple databases is not practical for such a simple thing as a quiz.
You can find some database structure examples here Storing a multiple choice quiz in a database - deciding the schema or here Database Design For Developing 'Quiz' Web Application using PHP and MySQL
Assuming you have simple quizzes with different categories, a possible alternative would be to store your categories in a Categories table.
* Category
- category_id (integer)
- category_name (string/text)
Where the id would be unique for each one. You could have, for example, the category sports with the id 1 and the category history with the id 2.
The questions could be stored in another table, named Questions
* Questions
- question_id (integer)
- question_category (integer)
- question_text (string/text)
- question_correct_answer_id (integer)
You can have question 1: For what team played John Doe?. In the question_category field you put 1, since 1 is the id of the Sports category. In the question_correct_answer_id you will store the id of the correct answer (not the answer itself, just the id)
Last but not least, an Answers table.
* Answers
- answer_id (integer)
- question_id (integer)
- answer_text (string/text)
Here answer_id is the id of the answer, you can store it in the Questions table if it's the correct one, and a question_id to identify the question.
Then you can do something like:
`SELECT * FROM Questions WHERE 'question_category' = 1`
and get all the Sports questions. And for each question, you can do
SELECT * FROM Answers WHERE 'question_id' = 'your question's id'
Be careful, my examples aren't necessary the best solution, it's just a basic example that uses just a single database, but with multiple tables instead of multiple databases.
My advice to you would be to consider this alternative and if you think it's worth it, read a little about databases and design a better one.
Good luck!