Freitag, 11. Dezember 2009

Creating a history table using SQL triggers

Just for the record and for posterity, this is how to insert a row into table B after a row has been inserted in table a (e.g. something like a foreign key) using a sqlite3 trigger (the trigger would probably work on other databases as well). Add this to a file - call the file "trigger.txt" or something like that:

CREATE TRIGGER mytrigger AFTER INSERT ON table_a
    BEGIN
        INSERT INTO table_b VALUES(NULL,new.primary_id)
END;

Easy, non? Now you have to import the trigger into the sqlite database. Assuming you have already got a database called mydb.db, use this command:
sqlite3 mydb.db < trigger.txt



Now, when you enter a row into table a, a row should automagically be appended to table b.

Montag, 7. Dezember 2009

python-perlmodule in the openSUSE build service

If you've ever wanted to embed perl code directly in python code, or execute perl code or functions directly from python, then python-perlmodule may be exactly what you are looking for. This package basically compiles and embeds the perl interpreter inside python. Once installed, it is possible to directly access perl by doing (e.g.):

import perl
x = perl.eval('3+3')
print x
6
print type(x)
'int'

Neat, eh? Theoretically, you could paste any long string of perl code into the perl.eval() function and have it executed as perl. The return value is a perl object, with python attributes. Sounds strange, huh? Well, consider this code:

import perl
x = perl.eval('[12,23,34,45,{ foo=>999,bar=>666},56,67]')
print x

for i in x: print i
...
12
23
34
45

56
67
>>> print x[4]['foo']
999


Quite handy, non? You can also access perl modules using syntax such as:
import perl
perl.require('Digest::MD5')

You can download the rpm directly from my openSUSE build service repository.