% This is a solution to one of the exercises accompanying "LaTeX for Administrative Work"
% See www.dickimaw-books.com/latex/admin/ for the book details,
% including licence.
%
% Two LaTeX runs are required for longtable to correctly align the
% table columns.

% arara: pdflatex
% arara: pdflatex
\documentclass[captions=tableabove]{scrartcl}

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

\usepackage{datatool}
\usepackage{longtable}
\usepackage{booktabs}

% Load data from booklist.csv:
\DTLloaddb{books}{booklist.csv}
% Load data from country-codes.csv:
\DTLloaddb{countries}{country-codes.csv}

% Add rules from booktabs package:
\renewcommand{\dtldisplaystarttab}{\toprule}%
\renewcommand{\dtldisplayendtab}{\\\bottomrule}%
\renewcommand{\dtldisplayafterhead}{\midrule}%

\begin{document}
The book titles are shown in Table~\ref{tab:books}.

\begin{table}
 \caption{Books}
 \label{tab:books}
 \centering
 \DTLdisplaydb{books}
\end{table}

The country codes are shown in Table~\ref{tab:countries}.

% Make sure you load at least v2.21 of datatool
% for the rules to show correctly as earlier versions
% had a bug.
\DTLdisplaylongdb
 [%
    caption={Country Codes},% main caption
    contcaption={Country Codes (Continued)},% continuation caption
    label={tab:countries},% label
    foot={\bottomrule\multicolumn{2}{r}{\emph{Continued on next page}}},% table footer
    lastfoot={}% final table footer
 ]
 {countries}

\end{document}
