Recent

Author Topic: [SOLVED] Error while saving a bitmap to a SQLite table  (Read 8086 times)

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
[SOLVED] Error while saving a bitmap to a SQLite table
« on: January 03, 2013, 02:50:43 pm »
Hi,

Once more I'm struggling to get a picture saved in a SQLite record. I have the following code to save a jpg file that was selected via a file dialog, but it always ends in a (meaningful) message "JPEG error" (see screenshot).
Debugging I found out that the error occurs at the Post statement.
I'm using a SQLite Connection with a Transaction and a Query in a Datamodule and I did look up other forum posts concerning this subject but cannot find the solution there..

Code: [Select]
procedure TfrmStripalbums.btnCoverClick(Sender: TObject);
begin
  if (SelectCover.Execute)
  then begin
       DataModule1.sqlStripalbums.Edit;

       try
         tblobfield(DataModule1.sqlStripalbums.fieldbyname('Cover')).LoadFromFile(SelectCover.FileName);
       except
         on e:exception do showmessage(e.Message);
       end;

       DataModule1.sqlStripalbums.Post;


I must be overlooking something but cannot find the mistake, can anyone help me out here?
Thanks!

Using Lazarus 1.05 & FPC 2.6.0
« Last Edit: January 20, 2013, 03:08:03 pm by JanRoza »
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Error while saving a bitmap to a SQLite table
« Reply #1 on: January 06, 2013, 10:24:43 pm »
Is there really no one who has any ideas on this?
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: Error while saving a bitmap to a SQLite table
« Reply #2 on: January 06, 2013, 10:37:34 pm »
Is some DB control (like TDBImage) connected direct to field 'Cover'? Probably Post call refresh on this control and image format saved in bloob is wrong. Try to detach visual controls from sqlStripalbums and we will know where is the problem

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Error while saving a bitmap to a SQLite table
« Reply #3 on: January 06, 2013, 11:03:18 pm »
That was the answer I was looking for, Dibo you were right. I used a tdbimage component on my form, now it's changed to a Timage component the Post and ApplyUpdates go without a hitch.
Only now the connection.transaction.commitretaining operation gives an error saying that the database is locked.

After closing the form where the updates and inserts on the table were done is closed I do the following:

Code: [Select]
table.ApplyUpdates
table.Close
connection.transaction.commitretaining

Is this not correct? Why should it say "db locked"?
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: Error while saving a bitmap to a SQLite table
« Reply #4 on: January 06, 2013, 11:31:31 pm »
Don't know. I'm newbie in SQLDb, it has different logic than other DB conpoments which I know (like ZEOS etc). I'm not SQLDB logic fan. Is it possible that some other SQLQuery has modified and not commited data? Or maybe another thread / connection?
« Last Edit: January 06, 2013, 11:33:57 pm by Dibo »

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Error while saving a bitmap to a SQLite table
« Reply #5 on: January 06, 2013, 11:52:57 pm »
Unfortunately I want to use a standard Lazarus installation without extra components for this as it's used for training purposes. I'll teach them later how to install other components.
So it's sqldb for now. All the actions on the table are done in my form/unit by table.Append, table.Edit/Post and table.Delete statements and on closing my form I do the table.ApplyUpdates so as far as I know ALL transactions on that tabel are done after the ApplyUpdates.
Beats me why it still complaints about a locked database.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: Error while saving a bitmap to a SQLite table
« Reply #6 on: January 07, 2013, 12:20:23 am »
Have you tried without "table.Close"? Or call close after commit. Maybe here something strange

teos

  • Full Member
  • ***
  • Posts: 157
Re: Error while saving a bitmap to a SQLite table
« Reply #7 on: January 19, 2013, 11:47:17 am »
if you do table.close and after that you do a "commit", it's not more than normal you get this error since the table you try to work on is closed. The table.close statement should be the last you call.

As for your dbImage problem: the current implementation writes the type of image as string in the blob field. That breaks the functionality: if you load the image from blob in a stream and the image was saved in the stream by a dbimage, you get the JPEG error. The other way around: if you put a image in a blob field in code without the dbImage, the type marker is not set and that also destroys the working.

It's an old discussion, the only way to use image blobs is to avoid the dbimage. I have filed a changer request but don't have SVN so I doubt if my proposal will ever be implemented.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Error while saving a bitmap to a SQLite table
« Reply #8 on: January 19, 2013, 12:25:12 pm »
@teos: it might help to check the bug tracker where that change request is... it's status (i.e. open, wontfix, resolved) indicates a lot and when closing the bug most devs will write a clear description of what happened to the bug.

IIRC, there were 2 different suggestions on how to solve this problem in the bugtracker. IIRC (again) the devs involved agreed that implementing one of them would be a good idea...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

teos

  • Full Member
  • ***
  • Posts: 157
Re: Error while saving a bitmap to a SQLite table
« Reply #9 on: January 19, 2013, 08:56:47 pm »
@BigChimp: I know this is offtopic.. My change request dates from september 2012. I have to start using SVN for doing a diff but I'm not gonna use SVN (I use Jedivcs for 12 years now) and seen only trouble and stress with SVN. My issue is just as offtopic as this reply because of the SVN discussion.. ;)

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Error while saving a bitmap to a SQLite table
« Reply #10 on: January 19, 2013, 10:48:46 pm »
@teos: I already tried with just the ApplyUpdates followed by the CommitRetaining but the result is the same: "database locked".
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

teos

  • Full Member
  • ***
  • Posts: 157
Re: Error while saving a bitmap to a SQLite table
« Reply #11 on: January 19, 2013, 10:56:05 pm »
@teos: I already tried with just the ApplyUpdates followed by the CommitRetaining but the result is the same: "database locked".

I'm not sure 100% but if you have designtime connection to the database (if you see data in a dbgrid in Lazarus at designtime) that connection is a connection to the database, AFAIK one that locks the table you have open.


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Error while saving a bitmap to a SQLite table
« Reply #12 on: January 20, 2013, 09:32:12 am »
@BigChimp: I know this is offtopic.. My change request dates from september 2012. I have to start using SVN for doing a diff but I'm not gonna use SVN (I use Jedivcs for 12 years now) and seen only trouble and stress with SVN. My issue is just as offtopic as this reply because of the SVN discussion.. ;)
Mmm yes, let's not have that discussion then ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Error while saving a bitmap to a SQLite table
« Reply #13 on: January 20, 2013, 02:54:48 pm »
@teos: You really made my day! That was it, the connection was active in the Lazarus IDE and running the program from the IDE obviously made a second connection. Never thought about the designtime at all.
Sometimes it are the most simple things that take the longest time to catch  %)

Thanks!
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

EgonHugeist

  • Jr. Member
  • **
  • Posts: 78
Re: [SOLVED] Error while saving a bitmap to a SQLite table
« Reply #14 on: January 20, 2013, 07:48:36 pm »
@JanRose

Quote
I already tried with just the ApplyUpdates followed by the CommitRetaining but the result is the same: "database locked".
This is a SQLitte protocol issue. Is theire any other ResultSet which uses the Table? AFAIK does the SQLDB components fetch only 10 rows per DataSetrequest. But you can only edit the table with another statment if this fetch is completed until the last row and the stmt-handle will be reseted.

I guess you check this, execute TSQLQuery.Last (this is what Zeos allways does for SQLite to prevent this case) and than you should be able to update this table.

Hope it helps.

 

TinyPortal © 2005-2018