Dealing with solutions within parts of exercises

I am trying to create a database of maths questions and answers in TeX form. The idea is that a simple command such as \getfromDB{101} will pull the text from the database of the question numbered 101. The solutions should be included in the text source with the question. Hence a typical entry in the database is:

What is the answer? \solution{This is it!}


A user can simply put the \getfromDB{101} text in their document as they like: say, within a list/enumerate/theorem, numbered whichever way they want. Now if all questions were like this, then I think that there are all sorts of options and packages available that can redefine the \solution macro the way any user would like.

But some questions come in parts:

This question has two parts:
\begin{parts}
\item Part one \solution{Part 1 answer}
\item Part two \solution{Part 2 answer}
\end{parts}


Again the idea is that the user can define the parts environment the way they like, but so far I haven't found a suitable package that can help with a suitable redefinition of the \solution macro.

I have looked at the following packages and so far I haven't been smart enough to solve my problem: answers and exsheets. Can anyone point me in an appropriate direction?

-

This is not exactly using a database but exsheets can selectively include questions from an external file. Questions can be given IDs and can be included by ID. Suppose your exernal file looks like this:

% this is file questions-db.tex
% ID=1
\begin{question}[ID=1]
First question.
\begin{parts}
\item Part one
\item Part two
\end{parts}
\end{question}
\begin{solution}
The answer to the first question.
\begin{parts*}
\item Part one
\item Part two
\end{parts*}
\end{solution}

% ID=2
\begin{question}[ID=2]
Second question.
\end{question}
\begin{solution}
The answer to the second question.
\end{solution}

% ID=3
\begin{question}[ID=3]
Third question.
\end{question}
\begin{solution}
The answer to the third question.
\end{solution}


Then you could use these questions in a main document as follows:

\documentclass{scrartcl}
\usepackage{exsheets}

\usepackage[inline]{enumitem}
\newlist{parts}{enumerate}{1}
\newlist{parts*}{enumerate*}{1}
\setlist[parts,parts*]{label=(\alph*)}

\begin{document}

\includequestions[IDs={1,3}]{questions-db.tex}

\printsolutions

\end{document}


It is not quite clear where the problem with the different question parts is...

-
This doesn't solve my problem. The format of the question as given in the database is such that part questions have the solution attached per part. Your solution has a list of questions followed by a list of solutions, which breaks this link. –  Geoff Oct 24 '12 at 4:33
Ah, that wasn't clear from your question. You should edit it, I guess. Do you have these exercises in a real database like in MySQL or how does the format look like? Anyway, this answer of mine might be of intererst for you. –  clemens Oct 24 '12 at 7:20
The database exists, but is a work in progress, so I can modify it as necessary. My thinking is that the solution goes directly with the question; so if a question has parts, each part has its solution. Exsheets (AFAIK) needs the questions as one list and then the solutions as another. This isn't what is stored in the db. –  Geoff Oct 24 '12 at 7:42
You haven't understood what I meant: you need to describe (in your question) how exactly the database looks like. Is it a simple text file? If so, how is its format? A csv list? One needs to know this in order to write a command \getfromDB. –  clemens Oct 24 '12 at 7:49
exheets needs every solution after the question it belongs to. But my linked answer shows a way around that! –  clemens Oct 24 '12 at 7:50

I usually do

\usepackage{versions}
...
\newboolean{ShowSolutions}
\setboolean{ShowSolutions}{false}

\ifthenelse{\boolean{ShowSolutions}}
{\includeversion{solution}}
{\excludeversion{solution}}
\begin{document}

question ?
\begin{solution}
\begin{solution}

next (sub) question ?
\begin{solution}