Reading a little bit more, I knew triggers can be used to implement
foreign keys when Sqlite version does not allow this feature. This
is my attempt:|
create table annotation_master
id integer primary key not null,
entry datetime not null,
cathegory integer not null,
subject varchar(40) not null,
description text not null
create table annotation_cathegory
id integer primary key not null,
title varchar(30) not null
create trigger fkcathegory_id
before insert on annotation_master
for each row
when((select id from annotation_cathegory where id = new.cathegory) is null)
raise(ABORT, 'This cathegory does not exist')
And the Lua script:
filename = 'calau.dbl'
obj = self
self.env = luasql.sqlite3()
self.con = self.env:connect(filename)
self.cur = self.con:execute('select sqlite_version()')
function annotation:append(cathegory, subject, description)
local err, msg = obj.con:execute(string.format("insert into annotation_master(entry, cathegory, subject, description) values(datetime('now'), %d, '%s', '%s')", cathegory, subject, description))
return err, msg
reg = annotation()
err, msg = reg:append(3, 'Assunto 1', 'Descrição 1')
When I print msg, Lua says that a Sqlite error occurs or because a database is missing or because of a logical Sqlite error. The database is correctly referenced and printing the schema in database, I can see the trigger and all the structure shown before was created.
As a logical error is a general message, I could not identify my error. This is the first time I use triggers, so it is not properly surprising if I understand something completely wrong.
In Sqlite website, I knew trigger support was added in 2.5 version and regarding Luasql works with 3.3.17, I presume this strategy can be used.
-------- Mensagem original --------
Yes, you are right. Using sqlite3.exe, I had created the database with these commands: sqlite3 test.dbl sqlite> pragma foreign_keys = on; sqlite> .read test.sql sqlite> sqlite_version(); 3.7.5 and after: lua test.lua 3.3.17 I created the database with 3.7.5 version, but when running the called version was 3.3.17. I placed sqlite3.exe and Sqlite3.dll in ./clibs and I though it would be sufficient to update my version. However, if I understood the version embedded in luasql is older and there is no support for foreign keys. In Lua script, I tried also: cur = con:execute('pragma foreign_keys = on') But this attempt to enable via code the foreign key for the section doesn't result. Yes, the problem seems to be the old version of Sqlite used by Luasql. Is the conclusion that I can't use foreign keys with Luasqlite.sqlite3()? If the answer is yes, the alternatives could be: 1. To manage relations among tables not in the database, but in the Lua code. When the number of tables or the number of relations are few, perhaps it would be the easier approach. 2. To change to another database more powerful, supporting foreign keys. But in this case, I don't want a separated process like Mysql. Regarding a database which can be distributed together with the program without additional instalations and foreign keys support, is there another option of database? Could I use Luasql or should use another Lua module? Em 13/03/2011 00:11, Bob Chapman escreveu: > On Sat, Mar 12, 2011 at 6:20 PM, Luciano de Souza<email@example.com> wrote: >> Hi all, >> >> I want to use foreign keys in Sqlite3. I am using the modules available in >> Lua for Windows and I downloaded the DLL and the commandline tool for >> Sqlite3. >> Does someone has an idea why I can't use foreign keys? >> > Does your example work for the command line tool? If so, this might be > the problem: > > Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio >> require "luasql.sqlite3" >> env = luasql.sqlite3() >> con = env:connect("") >> cur = con:execute("select sqlite_version()") >> print(cur:fetch()) > 3.3.17<==== > > [Run using the Lua for Windows package] > > -- >