Page 1 of 1

Recursive insert into mysql database from StringGrid

Posted: 19.03.2008, 18:31
by akdadoc
Hello Community as this is my first post I will like to say I appreciate the existence of this forum and i will in future hope to contribute greatly.

I have search through forum and other links and i have a dilemma with adding records from a string grid. I have successfully been able to add one record from the string grid but i need to be able to insert all rows from string grid.
here is my sample code as of now to insert the single row and also a failed commented loop to try to add recursively help!!

Thanks and for Future post,

procedure TForm2.frm2ContbtnClick(Sender: TObject);

repeat
begin
rowNum := 1;
// j:=0;
// repeat
Tracks1InsertQuery.SQL.Text:='SELECT * FROM `tblTracks1`';
Tracks1InsertQuery.Active;
Tracks1InsertQuery.Open;
Tracks1InsertQuery.Insert;
Tracks1InsertQuery.FieldByName('Title').AsString:= ImportTrackGrid.Cells[0,rowNum];
Tracks1InsertQuery.FieldByName('Artist').AsString:=ImportTrackGrid.Cells[1,rowNum];
Tracks1InsertQuery.FieldByName('Album').AsString:=ImportTrackGrid.Cells[2,rowNum];
Tracks1InsertQuery.FieldByName('Year').AsString:=ImportTrackGrid.Cells[3,rowNum];
Tracks1InsertQuery.FieldByName('Genre').AsString:=ImportTrackGrid.Cells[4,rowNum];
Tracks1InsertQuery.FieldByName('TrackNo').AsString:=ImportTrackGrid.Cells[5,rowNum];
Tracks1InsertQuery.FieldByName('Format').AsString:=ImportTrackGrid.Cells[6,rowNum];
Tracks1InsertQuery.FieldByName('Comment').AsString:=ImportTrackGrid.Cells[7,rowNum];
Tracks1InsertQuery.FieldByName('Time').AsString:=ImportTrackGrid.Cells[8,rowNum];
Tracks1InsertQuery.FieldByName('Size').AsString:=ImportTrackGrid.Cells[9,rowNum];
Tracks1InsertQuery.FieldByName('Layer').AsString:=ImportTrackGrid.Cells[10,rowNum];
Tracks1InsertQuery.FieldByName('BitRate').AsString:=ImportTrackGrid.Cells[11,rowNum];
Tracks1InsertQuery.FieldByName('SampleRate').AsString:= ImportTrackGrid.Cells[12,rowNum];
Tracks1InsertQuery.FieldByName('Frames').AsString:= ImportTrackGrid.Cells[13,rowNum];
Tracks1InsertQuery.FieldByName('FileName').AsString:=ImportTrackGrid.Cells[14,rowNum];
//TBlobField(Tracks1InsertQuery.FieldByName('Content')).LoadFromStream(fs);
Tracks1InsertQuery.Post;
Tracks1InsertQuery.Close;
// until rowNum := -1;
close;

end;
:) :)

Posted: 19.03.2008, 21:17
by mdaems
Hi,

I suppose you have a good reason for not using a DBGrid? As that would be lots easier because in that case zeoslib handles all inserts automatically.

Having said that the code seems alright.
Some trick (I learned from btrewern, thanks Ben) in case you are not adding to an empty table. Add a where close to your query that's always false (like 'SELECT * FROM `tblTracks1` where 2 = 1'). This avoids your server has to read all records in the table before you can add records.

Mark