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

Report any Hollywood bugs here
Post Reply
obiwan73
Posts: 9
Joined: Sun Jun 14, 2020 9:49 am

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

Post 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
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

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

Post 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
I'm on registered MorphOS using FlowStudio.
obiwan73
Posts: 9
Joined: Sun Jun 14, 2020 9:49 am

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

Post 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!
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post by airsoftsoftwair »

What about HaveItem()? Like so:

Code: Select all

If HaveItem(row, "TITRE_LIVRE")
 ...
EndIf 
obiwan73
Posts: 9
Joined: Sun Jun 14, 2020 9:49 am

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

Post 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.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

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

Post by SamuraiCrow »

@obiwan73

Probably because HaveItem() is returning False if the row is Nil.
I'm on registered MorphOS using FlowStudio.
obiwan73
Posts: 9
Joined: Sun Jun 14, 2020 9:49 am

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

Post 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.
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post 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
Post Reply