# Merging different dbtex database files

I want to merge 2 database files in one file . I tried loading files 11g.dbtex and 2005it.dbtex as shown below

\documentclass[a4paper,9pt]{article}
\usepackage{amssymb,amsthm}
\usepackage{amsmath}
\usepackage{setspace}
\usepackage[resetfonts]{cmap}
\usepackage{bytefield}
\usepackage{datatool}
\newcommand{\altura}{.45cm}
\usepackage{fancyvrb}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usepackage{fancyvrb}
\usepackage{fancyhdr}
\usepackage{mathtools}
\usepackage{longdata}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,decorations.markings,arrows}
\usepackage{bm}
\usepackage{qtree}
\usepackage{graphicx}
\usepackage{slashbox}
\usepackage{qtree}
\usepackage{fancybox}
\pagestyle{plain}
\usepackage{latexsym}
\usepackage[resetfonts]{cmap}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{qtree}
\usepackage{xcolor}

\newif\ifsolns \solnsfalse

\input{11g.dbtex} \input{2005it.dbtex}

\begin{document}
\begin{enumerate}
\DTLforeach[\DTLisclosedbetween{\seq}{1}{100}]% {finitemath}%
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}%
{\DTLifstringeq{\sub}{DM}{ {\DTLifstringeq{\top}{logic}{ \item \qus }{}} }{}}
\end{enumerate} \end{document}


But after compilation, it is showing result corresponding to 2005it.dbtex(last database loaded). I tried many combinations but every time it is showing result corresponding to the last database loaded. Is it possible to get a result as the combination of all database files?

Each .dbtex contains a single database with a unique label. From your previous question, I'm basing this on file 11g.tex:

\DTLnewdb{finitemath}
\long\def\newproblem#1#2#3#4#5#6#7#8#9{%
\DTLnewrow{finitemath}%
\DTLnewdbentry{finitemath}{seq}{#1}%
\DTLnewdbentry{finitemath}{yr}{#2}%
\DTLnewdbentry{finitemath}{sub}{#3}%
\DTLnewdbentry{finitemath}{top}{#4}%
\DTLnewdbentry{finitemath}{mar}{#5}%
\DTLnewdbentry{finitemath}{qus}{#6}%
\DTLnewdbentry{finitemath}{url}{#7}%
\DTLnewdbentry{finitemath}{exp}{#8}%
\DTLnewdbentry{finitemath}{ans}{#9}%
}

\newproblem{1}{2018}{DM}{comb}{1}{question 1}{url 1}{exp 1}{ans 1}
% lots more \newproblem commands


But what about file 2005it.tex? You haven't provided enough information. Does it start in the same way? Like this:

\DTLnewdb{finitemath}
\long\def\newproblem#1#2#3#4#5#6#7#8#9{%
\DTLnewrow{finitemath}%
\DTLnewdbentry{finitemath}{seq}{#1}%
\DTLnewdbentry{finitemath}{yr}{#2}%
\DTLnewdbentry{finitemath}{sub}{#3}%
\DTLnewdbentry{finitemath}{top}{#4}%
\DTLnewdbentry{finitemath}{mar}{#5}%
\DTLnewdbentry{finitemath}{qus}{#6}%
\DTLnewdbentry{finitemath}{url}{#7}%
\DTLnewdbentry{finitemath}{exp}{#8}%
\DTLnewdbentry{finitemath}{ans}{#9}%
}

\newproblem{2}{2018}{DM}{comb}{1}{question 2}{url 2}{exp 2}{ans 2}


If this is the case then you can't input both 11g.tex and 2005it.tex as \DTLnewdb{finitemath} is in conflict. So I'm assuming this isn't the case. Perhaps 2005it.tex is more like this:

\DTLnewdb{somethingelse}
\long\def\newproblem#1#2#3#4#5#6#7#8#9{%
\DTLnewrow{somethingelse}%
\DTLnewdbentry{somethingelse}{seq}{#1}%
\DTLnewdbentry{somethingelse}{yr}{#2}%
\DTLnewdbentry{somethingelse}{sub}{#3}%
\DTLnewdbentry{somethingelse}{top}{#4}%
\DTLnewdbentry{somethingelse}{mar}{#5}%
\DTLnewdbentry{somethingelse}{qus}{#6}%
\DTLnewdbentry{somethingelse}{url}{#7}%
\DTLnewdbentry{somethingelse}{exp}{#8}%
\DTLnewdbentry{somethingelse}{ans}{#9}%
}

\newproblem{2}{2018}{DM}{comb}{1}{question 2}{url 2}{exp 2}{ans 2}


Or do you have a single definition of \newproblem and a single instance of \DTLnewdb in one .tex file that's used across the document?

The second case seems the most likely. In which case, I'm guessing you obtained the .dbtex files by first changing \long\def\newproblem#1#2#3#4#5#6#7#8#9 in the .tex files to \providecommand{\newproblem}[9] and then used:

datatooltk --in 11g.tex --output 11g.dbtex
datatooltk --in 2005it.tex --output 2005it.dbtex


This creates 11g.dbtex that contains the definition of the database extracted from 11g.tex, which is identified by the label finitemath, and 2005it.dbtex that contains the definition of the database extracted from 2005it.tex, which is identified by whatever label was used in that file (somethingelse).

This means that in the document, if you want to reference the data in 11g.dbtex then you need to identify it by finitemath and if you want to reference the data in 2005it.dbtex then you need to identify it by whatever the associated label is (somethingelse).

\documentclass{article}

\usepackage{datatool}

\input{11g.dbtex}
\input{2005it.dbtex}

\begin{document}
\DTLforeach{finitemath}% data from 11g.dbtex
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\DTLforeach{somethingelse}% data from 2005it.dbtex
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\end{document}


If you don't want to keep track of the label you can use \DTLloaddbtex instead, which allows you to assign a command in which to store the label:

\documentclass{article}

\usepackage{datatool}

\begin{document}
\DTLforeach{\dataI}% data from 11g.dbtex
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\DTLforeach{\dataII}% data from 2005it.dbtex
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\end{document}


You can iterate over the second database and append it to the first, but that rather defeats the purpose of using datatooltk to improve the document build.

A simpler solution is to move \DTLnewdb and the definition of \newproblem to a new file, called, say problems.tex:

\newcommand{\databaselabel}{finitemath}

\DTLnewdb{\databaselabel}
\providecommand{\newproblem}[9]{%
\DTLnewrow{\databaselabel}%
\DTLnewdbentry{\databaselabel}{seq}{#1}%
\DTLnewdbentry{\databaselabel}{yr}{#2}%
\DTLnewdbentry{\databaselabel}{sub}{#3}%
\DTLnewdbentry{\databaselabel}{top}{#4}%
\DTLnewdbentry{\databaselabel}{mar}{#5}%
\DTLnewdbentry{\databaselabel}{qus}{#6}%
\DTLnewdbentry{\databaselabel}{url}{#7}%
\DTLnewdbentry{\databaselabel}{exp}{#8}%
\DTLnewdbentry{\databaselabel}{ans}{#9}%
}

\input{11g.tex}
\input{2005it.tex}


And remove those lines from 11g.tex and 2005it.tex.

Here's the modified 11g.tex:

\newproblem{1}{2018}{DM}{comb}{1}{question 1}{url 1}{exp 1}{ans 1}
% lots more \newproblem


and here's the modified 2005it.tex:

\newproblem{2}{2018}{DM}{comb}{1}{question 2}{url 2}{exp 2}{ans 2}
% lots more \newproblem


Now you just need to change the first line of problems.tex to change the database label, and add or remove \input lines to merge extra files (assuming they all simply contain multiple instances of \newproblem). This means that only one call of datatooltk is required:

datatooltk --in problems.tex --output problems.dbtex


This creates a database containing all the problems. Since you're using datatooltk it's more efficient to apply the filtering at this point instead of using the optional argument of \DTLforeach:

datatooltk --in problems.tex --output problems.dbtex --filter-and --filter seq ge 1 --filter seq le 100 --filter sub eq DM --filter top eq logic


You only need to run datatooltk whenever the data or the condition changes. Now when problems.dbtex is input in the document, it only contains the filtered selection. This makes the loop much faster (even more so if you use the starred read-only version of \DTLforeach):

\documentclass{article}

\usepackage{datatool}

\begin{document}
\DTLforeach*{\problemdb}%
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\end{document}


If you have another loop with a different condition, you just need another datatooltk call. For example:

datatooltk --name problems1 --in problems.tex --output problems1.dbtex --filter-and --filter seq ge 1 --filter seq le 100 --filter sub eq DM --filter top eq logic
datatooltk --name problems2 --in problems.tex --output problems2.dbtex --filter-and --filter seq ge 1 --filter seq le 100 --filter sub eq DM --filter top eq algebra


The --name switch assigns a new label, which can be used to avoid label conflict.

The modified document:

\documentclass{article}

\usepackage{datatool}

\begin{document}
\DTLforeach*{\problemIdb}%
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\DTLforeach*{\problemIIdb}%
{\seq=seq,\yr=yr,\sub=sub, \top=top, \mar=mar,\qus=qus, \ans=ans}
{%
%... whatever
}

\end{document}

• Thank you so much Nicola Talbot. It was really heapfull. Is it possible to export dbtex database to Mysql database? – Garvesh Gv Feb 28 '18 at 14:34
• @GarveshGv No, I'm sorry. datatooltk can only import from MySQL not export. – Nicola Talbot Feb 28 '18 at 14:37