Search code examples
sqlitedelphizeos

Strange ZQuery behavior


I'm using Zeos and SQLite3 DB in Delphi

ZQuery2.Close;
ZQuery2.SQL.Clear;
ZQuery2.SQL.Add('SELECT * FROM users WHERE un = ' + QuotedStr( UserName ) );

ZQuery2.Open;

OutputDebugString(PWideChar( ZQuery2.FieldDefList.CommaText )); // log : id,un,pw
OutputDebugString(PWideChar(ZQuery2.FieldByName('pw').AsString)); //causes error sometimes

the code is working but sometimes I get the following error message Exception class EDatabaseError with message 'ZQuery2:Field'pw' not found'.


Solution

  • This is odd because a field of a dataset shouldn't just disappear while the app is in the middle of running, especially if other fields are still operating normally. So, I would suspect something like a memory overwrite being the cause.

    Memory overwrites usually happen when something is written to the wrong place in memory, overwriting what is there, usually because of an incorrect pointer value or a so-called "buffer overrun" where the writing operation carries on beyond where is should stop. Usually, the pointer value is so wildly wrong that the OS can detect it and raise an AV, but sometimes it is less obvious.

    Delphi's memory manager has a 'full debug mode' which adds special checks for this condition, see here.

    I suggest you enable full debug mode as per the linked document and wait for the exception to occur.