Recent

Author Topic: [Solved] Dumb SQLite question asked by an idiot  (Read 1548 times)

heebiejeebies

  • Full Member
  • ***
  • Posts: 139
[Solved] Dumb SQLite question asked by an idiot
« on: December 13, 2024, 09:44:28 pm »
Hi all,

Just fixed the previous bug with the DBLookupListBox, now another bizarre one has appeared. Like the last one, I haven’t changed any of my code and this had been working for 2 years prior to upgrading Free Pascal/Lazarus version. I can’t see how fixing the last error could have affected it.

Anyway, this SQLite query:

Code: Pascal  [Select][+][-]
  1. UserfileLoad.Open;
  2. UserFileLoad.SQL.Text:=('SELECT * FROM MasterProtocol WHERE _rowid_ = :RowParam');
  3. UserFileLoad.Params.ParamByName('RowParam').Value := IssueIndex;      
  4. if UserFileLoad.Fieldbyname('Workspace').AsString = 'True' then WorkspaceCheckbox.Checked:= true;
  5.  
     
Now complains that the field Workspace doesn’t exist. I’ve added some debugging checks just before the code is executed, which confirm the following regarding UserFileLoad:

The database name is correct;
The query is correct;
The query is opened without error;
RowParam returns a valid record ID;
The query returns all the correct fields;
The dataset is active /1 record returned; and
Correct data is read from the fields!

My code above appears directly after the end of the successful checks; there’s nothing else in between the checks and the above.  Yet the checks work and the above code fails due to missing field.

Strangest one I’ve ever seen I think?! Where do I even start?
« Last Edit: December 13, 2024, 10:05:00 pm by heebiejeebies »
Mint 22 Cinnamon / FPC 3.3.1 / L 3.6.0

dsiders

  • Hero Member
  • *****
  • Posts: 1327
Re: Bizarre SQLite error
« Reply #1 on: December 13, 2024, 09:55:13 pm »
Why are you opening the dataset before setting the SQL and parameter values?
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

heebiejeebies

  • Full Member
  • ***
  • Posts: 139
Re: Bizarre SQLite error
« Reply #2 on: December 13, 2024, 10:04:26 pm »
Haha, because I'm a moron, clearly. I was so hung up on why this worked previously that I didn't even notice that.

Seriously though, WHY DID THIS WORK PREVIOUSLY??!!? I didn't change ANYTHING in the code. I wonder if it's something to do with the changes to the sqlite3connection class.
Mint 22 Cinnamon / FPC 3.3.1 / L 3.6.0

dsiders

  • Hero Member
  • *****
  • Posts: 1327
Re: Bizarre SQLite error
« Reply #3 on: December 14, 2024, 12:44:26 am »
Haha, because I'm a moron, clearly. I was so hung up on why this worked previously that I didn't even notice that.

Seriously though, WHY DID THIS WORK PREVIOUSLY??!!? I didn't change ANYTHING in the code. I wonder if it's something to do with the changes to the sqlite3connection class.

I don't know, but we can't debug your code without having your code. But I will add that 'SELECT * FROM' is bad idea. Use an explicit column selection List. The design-time editors with even do it for you.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Zvoni

  • Hero Member
  • *****
  • Posts: 2798
Re: Bizarre SQLite error
« Reply #4 on: December 16, 2024, 08:48:51 am »
But I will add that 'SELECT * FROM' is bad idea.
Another bad idea in SQLite is to filter on rowid.
Use an explicit PrimaryKey-Column

EDIT: And don't use "Value" on the Params (Line 3). Use explicit DataType... "AsInteger"
« Last Edit: December 16, 2024, 01:06:02 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018