Page 1 of 1

Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Mon Jun 15, 2020 11:11 pm
by obiwan73
Hi Andreas,

When importing a database from a small local library. I found a slight bug related to the different logical interpretation between LUA and Hollywood.

Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

In case a field in a database has a Null value (Permitted in SQL), Lua perceives it/transforms it into a NIL(LUA)

Hollywood receives the NIL value and turns into a NIL (Hollywood) which destroys the variable it affects, giving an error that the variable does not exist.

Lua and Hollywood therefore behave differently. (Tested on WIN/LINUX and LUA)

Code: Select all

/* Test Code Hollywood*/
@require "sqlite3"
local db = sqlite3.open("Database/MundoBIB.db")

for row in db:nrows("SELECT * FROM boeken")
  debugprint(row["ID_LIVRE"].." : "..row["TITRE_LIVRE"])
next
WaitLeftMouse

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Tue Jun 16, 2020 12:11 am
by SamuraiCrow
That's a known behavior. Us the IsNil() function instead of comparing it to NIL the constant.

https://hollywood-mal.com/docs/html/hol ... peNil.html

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Tue Jun 16, 2020 1:03 pm
by obiwan73
Thank you SamuraiCrow for your reaction here and elsewhere :-)

I had already tried this solution but if you use IsNil() or GetType(), it doesn't work.

Code: Select all

DebugPrint(row["ID_LIVRE"].." : "..IsNil(row["TITRE_LIVRE"]))
DebugPrint(row["ID_LIVRE"].." : "..GetType(row["TITRE_LIVRE"]))
(Console Output)

....
5710 : 0
5710 : 3
5711 : 0
5711 : 3

The following message arrives for line 5712, I get this error message here.
Error in line 4 (main.hws): Table field "TITRE_LIVRE" was not initialized!

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Tue Jun 16, 2020 10:22 pm
by airsoftsoftwair
What about HaveItem()? Like so:

Code: Select all

If HaveItem(row, "TITRE_LIVRE")
 ...
EndIf 

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Wed Jun 17, 2020 3:49 am
by obiwan73
Hi Andreas,

It's very strange, the result for row is always zero.

Code: Select all

test={}
test.pi = 3.14
test.nothing = {}

/* Simple Test for HaveItem())*/
DebugPrint("PI: " .. HaveItem(test,"pi") .." - nothing: ".. HaveItem(test, "nothing").." - Keyboard random: ".. HaveItem(test, "hkjhfjkshfj"))

/*reality check */
For row In db:nrows("SELECT * FROM boeken")
	DebugPrint(row["ID_LIVRE"]..":" .. HaveItem(row,"ID_LIVRE") .."-".. HaveItem(row, "TITRE_LIVRE"))
Next
Output for Simple HaveItem() test
PI: 1 - nothing: 1 - Keyboard random: 0


OUTPUT FOR DebugPrint(row["ID_LIVRE"]..":" .. HaveItem(row,"ID_LIVRE") .."-".. HaveItem(row, "TITRE_LIVRE")....
4547:0-0
4548:0-0
4549:0-0
4550:0-0
4551:0-0
4552:0-0
.....

It's very strange, the result for row is always zero.

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Wed Jun 17, 2020 6:27 am
by SamuraiCrow
@obiwan73

Probably because HaveItem() is returning False if the row is Nil.

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Wed Jun 17, 2020 9:32 am
by obiwan73
In the case of my example here, he's not nil.

Code: Select all

DebugPrint(row["ID_LIVRE"])
gives me the value 4547

But on the same line and the same variable with HaveItem commando gives me zero for

Code: Select all

 DebugPrint(HaveItem(row,"ID_LIVRE"))
The good news is that this command I learned is useful to see if a variable in a table exists, I'm testing it at the beginning of the program with the following code.

Code: Select all

test={}
test.pi = 3.14
test.nothing = {}
And the result is correct. But strangely not with my row built with the db:nrows method.

If I do

Code: Select all

If HaveItem(row, "ID_LIVRE")
 ...
EndIf 
I don't have any results because it's always 0 so no treatment in any case.

Re: Sqlite: Bug related to the difference in interpretation of the NIL between Lua and Hollywood.

Posted: Sat Jun 20, 2020 1:44 pm
by airsoftsoftwair
I think the problem is that you're passing an upper-case string to HaveItem(). Hollywood will always convert table indices to lower case, except when using string indices. So...

Code: Select all

If HaveItem(row, "id_livre")
 ...
EndIf 
...should give you the right result.

See also here: viewtopic.php?p=5367#p5367