更新数据工具中的值

更新数据工具中的值

我尝试在数据工具数据库中更新单个值。不幸的是,它只在第一次有效。当我更新第二个值时,第一个值也将被设置为第二个值。当第三个值更新时,第一个和第二个值也被设置为第三个值。同样,当我继续更新时,它总是使用最后一个值并将所有先前更新的值更改为最后一个值。我在这里做错了什么?

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{datatool}
\newcounter{searchID}


\begin{document}
\DTLnewdb{myDB}

  \DTLnewrow{myDB}  
  \DTLnewdbentry{myDB}{ID}{5}%
  \DTLnewdbentry{myDB}{counter}{2}%

  \DTLnewrow{myDB}  
  \DTLnewdbentry{myDB}{ID}{15}%
  \DTLnewdbentry{myDB}{counter}{3}%

  \DTLnewrow{myDB}  
  \DTLnewdbentry{myDB}{ID}{21}%
  \DTLnewdbentry{myDB}{counter}{6}%

orig tab

\DTLdisplaydb{myDB}
\bigskip

first update (of ID==5)

    \setcounter{searchID}{5}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current counter
    \xdef\newCounter{7} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \dtlupdateentryincurrentrow{counter}{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}
    \bigskip


secund update (of ID==15)

    \setcounter{searchID}{15}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current cunter
    \xdef\newCounter{8} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \dtlupdateentryincurrentrow{counter}{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}
    \bigskip


fthird update (of ID==21)

    \setcounter{searchID}{21}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current cunter
    \xdef\newCounter{9} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \dtlupdateentryincurrentrow{counter}{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}

\end{document}

在此处输入图片描述

我必须使用\setcounter{searchID}{15}构造,因为我要在foreachtikz 中的循环中更新一大堆值。所以我不能简单地将搜索值直接作为宏的参数写入。

答案1

\dtlupdateentryincurrentrow其行为就像列中已经有一个值一样。请注意,文档中\dtlreplaceentryincurrentrow有一个警告:\dtlreplaceentryincurrentrow

新值不会扩大。

因此这里是建议,但也许有更好的方法来扩展新值:

\documentclass{article}
%\usepackage[utf8]{inputenc}% needed for older TeX Distributions

\usepackage{datatool}
\newcounter{searchID}
\newcommand*\dtlupdatecounterincurrentrow
  {\dtlupdateentryincurrentrow{counter}}

\begin{document}
\DTLnewdb{myDB}

  \DTLnewrow{myDB}
  \DTLnewdbentry{myDB}{ID}{5}%
  \DTLnewdbentry{myDB}{counter}{2}%

  \DTLnewrow{myDB}
  \DTLnewdbentry{myDB}{ID}{15}%
  \DTLnewdbentry{myDB}{counter}{3}%

  \DTLnewrow{myDB}  
  \DTLnewdbentry{myDB}{ID}{21}%
  \DTLnewdbentry{myDB}{counter}{6}%

orig tab

\DTLdisplaydb{myDB}
\bigskip

first update (of ID==5)

    \setcounter{searchID}{5}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current counter
    \xdef\newCounter{7} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \expandafter\dtlupdatecounterincurrentrow\expandafter{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}
    \bigskip

second update (of ID==15)

    \setcounter{searchID}{15}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current cunter
    \xdef\newCounter{8} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \expandafter\dtlupdatecounterincurrentrow\expandafter{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}
    \bigskip

third update (of ID==21)

    \setcounter{searchID}{21}
    \DTLgetvalueforkey{\oldCounter}{counter}{myDB}{ID}{\thesearchID}%get current cunter
    \xdef\newCounter{9} %dummy for some inc like this \newCounter = \oldCounter + 1
    \edtlgetrowforvalue{myDB}{\dtlcolumnindex{myDB}{ID}}{\thesearchID}
    \expandafter\dtlupdatecounterincurrentrow\expandafter{\newCounter}% make local modification to current row
    \dtlrecombine% merge modification into database.
    \DTLdisplaydb{myDB}
\end{document}

结果:

在此处输入图片描述

相关内容