Integrating LaTeX and SQL databases
Now here's a little workshop that shows
Contents |
\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} |
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.
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:
\texdbconnection{DBType,host,user,passwd,db}
\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.
\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.
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:
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.texIf 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.