Solved

i want ask about database in delphi 5.0

Posted on 2001-07-04
16
294 Views
Last Modified: 2010-04-06
hi...

i want make database about field (blob image), i want save my picture(*.jpg) in my field (BLOB Image)...
and update my picture in this field...


please help me...
0
Comment
Question by:Rudy_Rai
  • 5
  • 5
  • 3
  • +2
16 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6252157
for what database?

(i'm sure epsylon will give you a working sample,
he has delphi on hand, which i don't have yet)

meikl ;-)
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 150 total points
ID: 6252177
i'm working with one project, it work with blob filed in access and oracle


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 DBCtrls, StdCtrls, Mask, Db, DBTables, ExtCtrls, Grids, DBGrids,Jpeg;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Image1: TImage;
   Table1: TTable;
   DataSource1: TDataSource;
   Database1: TDatabase;
   Table1PIC: TBlobField;
   DBNavigator1: TDBNavigator;
   OpenDialog1: TOpenDialog;
   DBGrid1: TDBGrid;
   Table1ID: TStringField;
   Label1: TLabel;
   DBEdit1: TDBEdit;
   procedure Button1Click(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure DataSource1DataChange(Sender: TObject; Field: TField);
 private
   { Private declarations }
   FileName : String;
   procedure ReadPicFromDB;
 public
   { Public declarations }

 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}
uses shellapi;
procedure TForm1.ReadPicFromDB;
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Image1.Picture.Assign(nil);
 BlobStream := TBlobStream.Create(Table1Pic, bmRead);
 if BlobStream.Size =0 then
 begin
   BlobStream.Free;
   Exit;
 end;
 if FileName <> '' then
   DeleteFile(FileName);
 FileName := 'Img.jpg';
 FileStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
 FileStream.CopyFrom(BlobStream,BlobStream.Size);
 FileStream.Free;
 BlobStream.Free;
 Image1.Picture.LoadFromFile(FileName);
end;// end procedure radpicfromdb

procedure TForm1.Button1Click(Sender: TObject);
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Application.ProcessMessages;
 if OpenDialog1.Execute then
 begin
   Button1.Enabled := false;
   FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
   Table1.Edit;
   BlobStream := TBlobStream.Create(Table1Pic,bmReadWrite);
   BlobStream.Seek(0,soFromBeginning);
   BlobStream.Truncate;
   BlobStream.CopyFrom(FileStream,FileStream.Size);
   FileStream.Free;
   BlobStream.Free;
   Table1.Post;
   ReadPicFromDB;
   Button1.Enabled := True;
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 if FileName <> '' then
   DeleteFile(FileName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.Filter := 'Jpeg Files |*.jpg';
 OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
 Image1.Stretch := true;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
 if DataSource1.State = dsBrowse then
   ReadPicFromDb;
end;

end.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6252184
if you want, send me email at nasman@mogaza.org to send u the project if u like
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6252188
well, mnasman did it
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6252239
My standard answer  :o)

You can store for example jpeg images in the database by using a blob field (Ole Object field in Access):


var fs: TFileStream;
bs: TBlobStream;


Writing to database:

with Table1 do
begin
Append;
fs := TFileStream.Create('image1.jpg', fmOpenRead);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmWrite));
bs.CopyFrom(fs, 0);
bs.Free;
fs.Free;
Post;
end;


Reading from database:

with Table1 do
begin
fs := TFileStream.Create('image1.jpg', fmOpenWrite or fmCreate);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
fs.CopyFrom(bs, 0);
bs.Free;
fs.Free;
end;


If you want to display JPEG images you can use a TJPEGImage and then assign it to a TImage:


var j: TJPEGImage;

with Table1 do
begin
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
j := TJPEGImage.Create;
j.LoadFromStream(bs);
Image1.Picture.Assign(j);
bs.Free;
j.Free;
end;


Hope this helps...

Epsylon.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6252463
:-)))
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6252685
well, then i will post also my old version from my paq

first in the paradox structure the BlobField is type Binary
and now follows a sample-unit,
which stores and get the JPeg

unit db_pict_u;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, Menus, jpeg; // JPeg unit is used

type
 TForm1 = class(TForm)
   DBEdit1: TDBEdit;   // A other DBField
   DBNavigator1: TDBNavigator;
   Table1: TTable;
   DataSource1: TDataSource;
   PopupMenu1: TPopupMenu;     // a Popup linked to Image1
   LoadPicture1: TMenuItem;    // the MenuItem of the Popup
   OpenDialog1: TOpenDialog;   // OpenDialog
   Image1: TImage;             // Visual Output for the JPeg
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
   procedure Table1AfterScroll(DataSet: TDataSet); // Get JPeg and Display in Image1
 private
   { Private-Deklarationen }
 public
   { Public-Deklarationen }
 end;

var
 Form1: TForm1;
 JPegImage : TJpegImage;  //A temporary JPeg-Object

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 JpegImage := TJPegImage.Create; // Create JPeg-Object
 Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 Table1.Close;
 JPegImage.Free;  // Free JPeg-Object
end;

procedure TForm1.LoadJPEG1Click(Sender: TObject);
Var
 MyBlobStream : TBlobStream;      // Streams
 MyJPegStream : TMemoryStream;
begin
 if Table1.State in [dsEdit,dsInsert] then    // Do only if in edit or insert mode
 begin
   If opendialog1.Execute then    // If file selected
   begin
     if Table1.FieldByName('Bild2').IsBlob then   // this line can deleted
     begin
       MyJPegStream := TMemoryStream.Create;      // Handle JPegFile
       JPegImage.LoadFromFile(opendialog1.filename);
       JPegImage.SaveToStream(MyJPegStream);
       // Prepare Blob
       MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmReadWrite);
       MyBlobStream.CopyFrom(MyJPegStream,0);  // Copy JPeg into BlobField
       Image1.Picture.Assign(JPegImage);  // Display
       MyJPegStream.Free;  //Free all
       MyBlobStream.Free;
     end;
   end;
 end else ShowMessage('Table is not in Edit Mode!');
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
 MyBlobStream : TBlobStream;
begin
 // Do Only if a JPeg available
 if (Table1.FieldByName('Bild2').IsBlob) and (not(Table1.FieldByName('Bild2').IsNull)) then
 begin
   // Prepare Streams
   MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmRead);
   JPegImage.LoadFromStream(MyBlobStream);
   Image1.Picture.Assign(JPegImage);  //Display JPeg
   MyBlobStream.Free;  // Free Stream
 end
 else image1.Picture := Nil;  // No JPeg saved disable display
end;

end.

meikl ;-)))

0
 

Expert Comment

by:ondertol
ID: 6252851
You must Load your image from disk to Table using this istruction:

Table1.Edit;
Table1Field.LoadFromFile('c:\mydir\myImage.jpg');
Table1.Post;

for use this istruction you must insert your table on your form and Add your graphic Fields (double click into Table1 component into your form + left click on the new window + Add Fields).

Table1Field is the name assigned at your graphic field, the first part is your Table name, the second part is your Fields name (you can see it on object into your unit program, the type is TGraphicField). The string "myImage.jpg" is the name of your image (you can insert the directory)!

If you want update this image on your table you must reload the image (using the same istruction).

If you desire save the image on your table into file disk, write this:

Table1Field.SaveToFile('c:\mydir\NewImage.jpg');


For load one file selected by user, can you write this code:

If Open.Execute then
begin
  Table1.Edit;
  Table1Field.LoadFromFile(Open.FileName);
  Table1.Post;
end;

Open is one TOpenDialog component!!!
Modify the Open.Filter property to list only *.jpg file (Open.Filter := 'JPEG Format |*.jpg').

!!! remember: insert the jpeg unit on your program (uses jpeg) !!!!!

bye bye

bye bye


0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6252879
>>>sigh<<<
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6253631
Epsylon,  :-/
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6253685
ondertol,
thats not the right way, even as i guess that would not work correctly
and also if it works it needs persistent fields

all comments above your answer, which are containing code are verified working samples,

so please be so kind, and withdraw your answer, and let the questioner decide,
(keep this also in mind for the future, because it was not the first  time you did this!)

if not,
then

Rudy_Rai, please reject ondertol's answer and grade mnasman,
which gives you first a correct answer

meikl ;-)
0
 

Author Comment

by:Rudy_Rai
ID: 6255405
sorry ondertol, because i will try first their program ok...
0
 

Author Comment

by:Rudy_Rai
ID: 6255908
i want in database paradox (*.db)....
and  halo mnasman what is table1pic and table1id ??
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6255924
it's two fields in the table
i made talbe with two fields to test
one is id and the other is pic
id : string
pic : blob
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6255938
the example i gave u it work fine with paradox table
i test it with oracle, access and paradox, and it work fine with them without any change in code
just change the table name
if you need a copy of this project, give me ur email

Mohammed
0
 

Author Comment

by:Rudy_Rai
ID: 6259065
my email is: rudy_r@mailcity.com...

can u give me your source code...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question