LaTeXDB - Workshop

Integrating LaTeX and SQL databases

Now here's a little workshop that shows

  • how to create a simple database with MySQL (which is assumed to be installed and setup in a way that you can login with the mysql command line tool),
  • how to write a LaTeX document that uses some extra commands to access the database and loop over query result sets,
  • and finally process this "sort-of-latex" file through LaTeXDB.

Contents

1. Creating the database    
2. The LaTeX file

3. Example
4. Running latexdb


 \begin{document}
   \texdbfor{##AllUsers}{
     Dear ##Title ##Lastname,

     thanks for ordering ##Quant items of ##Product. 
     I'll ship it to your address in ##Town when I 
     find the time.

     Best regards,
     \newpage
   }
 \end{document}

1. Creating the database

First let's create the database and fill it with some values. We'll do a simple First name, last name database.

# mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 3.23.55-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database texdb;
Query OK, 1 row affected (0.00 sec)

mysql> use texdb
Database changed

mysql> create table Users ( id INT PRIMARY KEY, Vorname VARCHAR(40), Nachname VARCHAR(40) );
Query OK, 0 rows affected (0.00 sec)

id is the primary key. It's not really needed but more convenient. Vorname is german for "first name", Nachname is the "(last) name". We're now putting three people in there:

mysql> insert into Users values (0,"Hans-Georg","Eßer");
Query OK, 1 row affected (0.00 sec)
mysql> insert into Users values (1,"Stefan","Mustermann");
Query OK, 1 row affected (0.00 sec)
mysql> insert into Users values (2,"Sabine","Sauer");
Query OK, 1 row affected (0.01 sec)

Finally, let's check things worked well:

mysql> select * from Users;
+----+------------+------------+
| id | Vorname    | Nachname   |
+----+------------+------------+
|  0 | Hans-Georg | Eßer       |
|  1 | Stefan     | Mustermann |
|  2 | Sabine     | Sauer      |
+----+------------+------------+
3 rows in set (0.00 sec)

mysql> _

Yes, we've done it.

2. The LaTeX file

The basic idea behind LaTeXDB is that you can write a pretty standard LaTeX file, but this file can include loops over result sets from database queries.

Only three new commands are needed to do what we want:

  1. We need a way of defining what database to use, how to connect, etc. The command is
    \texdbconnection{DBType,host,user,passwd,db}
  2. Queries must be defined, and we must have a way to tell what names we will use in LaTeX later, because the table field names might conflict with something we need in TeX. So the syntax is
    \texdbdef{##query}{select var1,var2,... from table where...}{##VAR1,##VAR2,...}
    Now here with ##query we set a name for query we're just defining, and it will be reused later. The point is: You can define several queries in one go, and then later reference each of them separately. var1, ... are table field names, and they do correspond to the ##VAR1 names that appear in the end. The order must be the same: ##VAR1 belongs to var1, ##VAR2 to var2 etc. Finally table is a db table name.
  3. The last thing there is to do is use the query. The standard thing you'll want to do is write some LaTeX stuff for each element of the result set---that's a for loop:
    \texdbfor{##query}{... some LaTeX stuff with ##VAR1, ...}
    Here the ##query refers to the same query that was defined with the previous command. So in each for loop you can decide which of your several queries to use. They need to have different names, of course. The variables in the second { } block are going to be substituted with the corresponding values from the result rows, and for each row the { } block will be used once. That's it.

3. Example

OK, here's a simple example (example.tex) that uses the database table we've defined further up:

\documentclass[a4]{article}

% Standard LaTeX stuff
\usepackage{isolatin1}

% DB connection, SQL queries
\texdbconnection{MySQL,localhost,****,*****,texdb}
%\texdbconnection{File,-,-,-,/var/db/mydata/}
\texdbdef{##q1}{select Vorname,Nachname from Users}{##Vorname,##Nachname}
\texdbdef{##q2}{select CONCAT(Nachname,", ",Vorname),CONCAT(Vorname," ",Nachname) from Users}{##Vorname,##Nachname}

% Here the text begins
\begin{document}
This is a header for the page.

What you're seeing is \LaTeX{} with a MySQL extension.\\

\begin{tabular}{|l|l|}
\hline
Vorname & Nachname \\
\texdbfor{##q1}{\hline \textit{##Vorname} & \textbf{##Nachname}\\}
\hline
\hline
Nachname, Vorname & Vorname Nachname \\
\texdbfor{##q2}{\hline \textit{##Vorname} & \textbf{##Nachname}\\}

\hline
\end{tabular}\\

This is a footer for the page.
\end{document}

Note that there are only five line that make this document not be a regular LaTeX document:

4. Running latexdb

Now what will you have to do in order to process this file? Well, instead of the standard latex example.tex command, just issue

latexdb example.tex
If there is no error in preprocessing the file, this will just look like a regular run of latex.

The dvi files looks like this:

For debugging purposes, temporary files of latexdb are not deleted in this version. Next to your file.tex source, you will find two more files:

You can find this example and a further one in the examples/ tree of the package.


Hans-Georg Eßer
Last modified: Fri Aug 15 20:26:58 CEST 2003