About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings

Exercise 6: Fetching a Row of Data (Solution)

This is a solution to Exercise 6. You need the sample file people.csv. Alternatively, if you want to use data from the samples SQL database, remember to use datatooltk to import the data to a .dbtex file and load it using \DTLloaddbtex as described in Loading Data From a .dbtex File.

\documentclass[captions=tableheading]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{datatool}

\DTLloaddb{people}{people.csv}
\DTLloaddb{countries}{country-codes.csv}

\begin{document}

% We can't fetch by surname since there are two people
% with the surname "Canary", so we have to fetch by
% Fred's unique id. This happens to be the same as 
% the row number, so in this example, \DTLassign{6}{...} 
% could be used instead, but in the real word the rows 
% could get shuffled around or people could be deleted 
% from the data (for example, they're a customer who's 
% closed their account or someone who's resigned from a club.)

\DTLassignfirstmatch{people}{id}{6}{%
  \Surname=surname,%
  \Forenames=forenames,%
  \Title=title,%
  \AddressI=address1,%
  \AddressII=address2,%
  \Town=town,%
  \County=county,%
  \Postcode=postcode,%
  \CountryCode=country%
}
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}

\begin{tabular}{l}
  \Title\ \Forenames\ \Surname\\
  \AddressI\\
  \ifdefempty{\AddressII}{}{\AddressII\\}%
  \Town\\
  \County\\
  \Postcode\\
  \CountryName
\end{tabular}

\end{document}

Download fetchfred.tex or fetchfred.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs