public void AddQuestions(Dictionary <string, string> quizzData) { var quizzId = quizzData["quizz_id"]; using (var dbWrapper = new DbWrapper("studybuddyquizzes")) { if (!dbWrapper.DoesDbExist()) { InstantiateQuizzDatabase(); } using (var command = new SQLiteCommand("SELECT QuizzID from Quizzes WHERE QuizzID = @QuizzID")) { command.Parameters.AddWithValue("@QuizzID", quizzId); var found = dbWrapper.ExecuteScalar(command); } } }
public void CreateNewQuizz(Quizz quizzData) { using (var dbWrapper = new DbWrapper("studybuddyquizzes")) { var quizzId = 0; if (!dbWrapper.DoesDbExist()) { InstantiateQuizzDatabase(); } using ( var command = new SQLiteCommand( "INSERT INTO Quizzes (QuizzName, Description) values (@QuizzName, @Description)") ) { command.Parameters.AddWithValue("@QuizzName", quizzData.name); command.Parameters.AddWithValue("@Description", quizzData.description); dbWrapper.ExecuteNonQuery(command); command.CommandText = "SELECT QuizzID FROM Quizzes WHERE QuizzName = @QuizzName"; quizzId = int.Parse(dbWrapper.ExecuteScalar(command).ToString()); } foreach (var question in quizzData.questions) { var questionId = -1; using ( var command = new SQLiteCommand( "INSERT INTO Questions (Question, QuizzID) values (@Question, @QuizzID)") ) { command.Parameters.AddWithValue("@QuizzID", quizzId); command.Parameters.AddWithValue("@Question", question.question); dbWrapper.ExecuteNonQuery(command); command.CommandText = "SELECT QuestionID FROM Questions WHERE Question = @Question AND QuizzID = @QuizzID"; questionId = int.Parse(dbWrapper.ExecuteScalar(command).ToString()); } foreach (var option in question.options) { using ( var command = new SQLiteCommand( "INSERT INTO Answers (QuestionID, Answer, Is_Correct) values (@QuestionID, @Answer, @Is_Correct)") ) { command.Parameters.AddWithValue("@QuestionID", questionId); command.Parameters.AddWithValue("@Answer", option.answer); if (question.rightAnswerIds.Contains(option.id)) { command.Parameters.AddWithValue("@Is_Correct", 1); } else { command.Parameters.AddWithValue("@Is_Correct", 0); } dbWrapper.ExecuteNonQuery(command); } } } } }