[Tex/LaTex] Use datatool to read a row from a CSV file, then use the variables in the document

datatool

I'm confused about how to use "datatool" to read variables from a row in my database, and pass them on to be formatted in my text. I don't want to make a table, but that's about all there seems to be examples of on the Internet.

I have a CSV file made up something like this:

name|points
John|172
Sue|120
Mike|64

After two days of staring at the datatool manual, this is the extent of my success:

\documentclass{memoir}
\usepackage{datatool}
\DTLsetseparator{|}
\begin{document}
\DTLloaddb{players}{database.csv}
\DTLforeach*{players}{\name=name,\points=points}
\subsection{\name}
\textit{\points}
\end{document}

That prints the chosen variables with the formatting I desire, but it only does so with the last entry in the database. My guess is that "\dtlgetrowforvalue{players}{1}{John}" could be used to find John's row in the database, but I don't understand how to integrate that with the commands that read and display the variables.

I am unfamiliar with much of the terminology used in the manual, and finding no examples that look like what I wish to achieve, my attempts at bullying my way through by trial and error have failed. I don't necessarily need a working example, but I would very much like for someone here to put me onto the appropriate commands, at least.

Best Answer

\DTLforeach takes three compulsory arguments, not two as in your example. This fixes your example:

\documentclass{memoir}

\usepackage{datatool}
\DTLsetseparator{|}

\DTLloaddb{players}{database.csv}

\begin{document}

\DTLforeach*
{players}% database label
{\name=name,\points=points}% assignment
{% Stuff to do at each iteration:
  \subsection{\name}
  \textit{\points}
}

\end{document}

This produces:

Image of result

Edit:

\DTLforeach is a repetitive (loop) command designed to perform a particular action for each row of data. If you just want to lookup information from a single row of the data, there are a number of methods to do this. Here are some examples:

\documentclass{memoir}

\usepackage{datatool}
\DTLsetseparator{|}

\DTLloaddb{players}{database.csv}

\begin{document}

Sue's points: \DTLfetch{players}{name}{Sue}{points}.

Pull information from row 3.
\DTLassign{players}{3}{\name=name,\points=points}

Here's the information: Name: \name. Points: \points.

Pull information from the first row where the name column has the
value ``John''.
\DTLassignfirstmatch{players}{name}{John}{\name=name,\points=points}

Here's the information: Name (we already know this anyway): \name.
Points: \points.

\newcommand{\PlayerName}{John}
As above, but the required name (\PlayerName) is given by a command:
\xDTLassignfirstmatch{players}{name}{\PlayerName}{\name=name,\points=points}

Here's the information: Name (we already know this anyway): \name.
Points: \points.

\end{document}

This produces:

Image of result

Sue’s points: 120.
Pull information from row 3.
Here’s the information: Name: Mike. Points: 64.
Pull information from the first row where the name column has the value “John”.
Here’s the information: Name (we already know this anyway): John. Points: 172.
As above, but the required name (John) is given by a command:
Here’s the information: Name (we already know this anyway): John. Points: 172.

Related Question