Recent

Author Topic: [Solved!] Lazarus swears FileExists when it doesn't  (Read 2641 times)

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
[Solved!] Lazarus swears FileExists when it doesn't
« on: April 17, 2021, 02:08:59 pm »
Hi all,

Here is my first draft attempt at writing a routine to (1) check if a userdata database already exists, and (2) create it if it doesn't.  It's an SQLite database, presently just located in the same directory as the programme. PrefsUserDataConnection is the SQLite connection and DBInitialisation is the transaction.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button12Click(Sender: TObject);
  2. var
  3.   NewFile: boolean;
  4. begin
  5.      PrefsUserDataConnection.Close;
  6.      try
  7.        NewFile := not FileExists(PrefsUserDataConnection.DatabaseName);
  8.        if NewFile then
  9.             begin
  10.             try
  11.               PrefsUserDataConnection.Open;
  12.               DBInitialisation.Active :=True;
  13.               PrefsUserDataConnection.ExecuteDirect('CREATE TABLE `User1` ('+
  14.         '`SessionNum`   INTEGER PRIMARY KEY AUTOINCREMENT,'+
  15.         '`Date` TEXT,'+
  16.         '`LoC`  INTEGER);');
  17.               DBInitialisation.Commit;
  18.               ShowMessage('Database file created successfully.');
  19.               except
  20.                 ShowMessage('Whoops! Screwed up!');
  21.               end;
  22. end
  23.             else
  24.             ShowMessage('Else statement!');
  25.             except
  26.              ShowMessage('Error!');
  27.                        end;
  28. end;        

Worked perfectly the first time - created the database correctly and all.  Second time it returned the else statement, because the file already existed.  Trouble is, if you delete the file, it still returns the else statement.  I managed to nudge it back to reality once by deleting the file then restarting the system, but this only worked once.  Generally it still swears the file exists even when it's long gone.  Any ideas? Thank you!
« Last Edit: April 20, 2021, 10:28:46 am by heebiejeebies »
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

WooBean

  • Sr. Member
  • ****
  • Posts: 277
Re: Lazarus swears FileExists when it doesn't
« Reply #1 on: April 17, 2021, 02:40:25 pm »
Hi,

it seems to me that you should read SQLite documentation first (ie. from sqlite.org website) and then write some code. In SQlite one database (which is a file) can contain many tables. A table once created exists until removed from database or its database is deleted.

To the point, check if the directory and file you use in "PrefsUserDataConnection.DatabaseName" is the same as in PrefsUserDataConnection setting (catalog, file?).
« Last Edit: April 17, 2021, 02:48:31 pm by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

alpine

  • Hero Member
  • *****
  • Posts: 1302
Re: Lazarus swears FileExists when it doesn't
« Reply #2 on: April 17, 2021, 03:12:28 pm »
On which OS? Windows has annoying file protection mechanisms and treats some file extensions in an unexpected way as system files.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 8030
Re: Lazarus swears FileExists when it doesn't
« Reply #3 on: April 17, 2021, 04:26:51 pm »
it seems to me that you should read SQLite documentation first (ie. from sqlite.org website) and then write some code. In SQlite one database (which is a file) can contain many tables. A table once created exists until removed from database or its database is deleted.

To the point, check if the directory and file you use in "PrefsUserDataConnection.DatabaseName" is the same as in PrefsUserDataConnection setting (catalog, file?).

How exactly was the file deleted? What have you done to check it was deleted?

And FFS, tell us what OS you're using, and what version of Lazarus and FPC...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #4 on: April 18, 2021, 01:14:55 am »


And FFS, tell us what OS you're using, and what version of Lazarus and FPC...

MarkMLl

Your input is not required in this thread if you're going to be rude, thank you.

I did actually put that in my initial draft post, but then accidentally deleted it and unfortunately forgot to retype the details!  Simple memory error (mine, not the computer's!)
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #5 on: April 18, 2021, 01:16:21 am »
On which OS? Windows has annoying file protection mechanisms and treats some file extensions in an unexpected way as system files.

Not Windows in this case.

Ubuntu 18.04/Lazarus 2.0.10 FPC 3.2.0.  :)
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Lazarus swears FileExists when it doesn't
« Reply #6 on: April 18, 2021, 07:06:20 am »
heebiejeebies, I, or rather an end user of mine, experienced something like this and we eventually decided it was because he was using an open source file encryption tool, the name of which I cannot remember.  (A new version of that file encryption fixed his problem, U18.04 is starting to show its age.) Do you have anything like that running ?  Or is the file in question mounted as some sort of share, smb, nfs etc ?

WRT your message to Mark, I sort of agree but you really do need to understand how frustrating it can be when people ask questions without at least telling us the very, very basic information needed.  But we all get along pretty well here usually ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #7 on: April 18, 2021, 10:38:13 am »
heebiejeebies, I, or rather an end user of mine, experienced something like this and we eventually decided it was because he was using an open source file encryption tool, the name of which I cannot remember.  (A new version of that file encryption fixed his problem, U18.04 is starting to show its age.) Do you have anything like that running ?  Or is the file in question mounted as some sort of share, smb, nfs etc ?

WRT your message to Mark, I sort of agree but you really do need to understand how frustrating it can be when people ask questions without at least telling us the very, very basic information needed.  But we all get along pretty well here usually ....

Davo

Thank you Davo!  The file is mounted in a subfolder of my regular home folder, which has no encryption on it as per the 18.04 default.  I do have Gnome Encfs installed, as well as TrueCrypt and VeraCrypt, but the file is not on one of those volumes.  But I take it from this that there's no issue with my code, and it's a bug in either Lazarus or the OS...

As for Mark, I really don't intend a prolonged debate on it, but just a final word:  if he'd said exactly what you said: "Please provide us with OS details, it's very frustrating", then there would have been no issue.  Part of being an adult in a civilised society is that sometimes things annoys us, but hopefully we respond in a mature and respectable way anyway.  It seems to be only on the internet that greeting a complete stranger with "FFS!" is ever considered a justifiable or expected response.  %)
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Lazarus swears FileExists when it doesn't
« Reply #8 on: April 18, 2021, 01:24:08 pm »
> VeraCrypt

yep, that was it.  I suggest you look at updating (if possible) or disabling VeraCrypt and see if that affects your issue.  I the case I was involved with, again, the particular file was no subject itself to Veracrypt but Veracrypt was still loaded into memory and was still hooked into the filesystem interface.

Its a suggestion anyway.

WRT Mark, he is a very knowledgeable and very helpful person in my experience.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #9 on: April 18, 2021, 03:39:34 pm »
> VeraCrypt

yep, that was it.  I suggest you look at updating (if possible) or disabling VeraCrypt and see if that affects your issue.  I the case I was involved with, again, the particular file was no subject itself to Veracrypt but Veracrypt was still loaded into memory and was still hooked into the filesystem interface.

Its a suggestion anyway.

Thanks Davo!  I'll try and update it, but at least it seems it's a conflict rather than a programmer error.  That's reassuring.  Maybe I shouldn't take that attitude and should actually follow up on all conflicts though.  Is that what real programmers have to do??  :o %)

Quote
WRT Mark, he is a very knowledgeable and very helpful person in my experience.

I'm sure he is.  Personally, I'll deal with people who can be polite and civil to me, and if I have to forgo some expert knowledge as a result of that preference, I'll take that hit.  He has every right to avoid my threads if he doesn't want to conform to my stringent British standards of politeness.  :D  Although I will reiterate, I think my standards are just the same standards anyone accepts IRL and I think all internet misunderstandings would be instantly resolved if we all just imagined addressing our words to others directly face-to-face before we posted them.
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #10 on: April 18, 2021, 03:42:04 pm »
In the Windows world these days, some file types can be deleted however, if that file is actively opened it is not fully deleted until who ever has it opened closes it or that application closes and the OS will then officially mark it as deleted otherwise a file check may return as still being there.

 This is basically a shell operation that handles this. Low level calls can make a better determination otherwise.

Thanks Jamie!  Hopefully I won't have the issue in the Windows compile.  Which reminds me I should actually install Win Lazarus and compile this from time to time just to see what bugs pop up.  So I can annoy you guys with a whole new series of issues no doubt...  :D
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

heebiejeebies

  • Full Member
  • ***
  • Posts: 129
Re: Lazarus swears FileExists when it doesn't
« Reply #11 on: April 20, 2021, 10:28:24 am »
Guys!  I figured it out.

Despite valiant and very much appreciated attempts, everyone was wrong.  :D  I discovered that I had the "Connected" property of the database connection set to "True" by default.  Thus, as soon as you opened my programme, it immediately created a blank text file with the same filename as the database in order to stay "connected".  And thus the file always existed.  Now the only question is, how on earth did it work at all, since it did actually perform correctly about once or twice?  %)

Ah, but that's a story for another day, kids.....

Thanks again everyone!  Legends!  When this programme makes me the next Bill Gates, I will personally transfer billions of dollars into all of your accounts*.

*Conditions apply.
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

 

TinyPortal © 2005-2018