Mega Code Archive

 
Categories / Delphi / Examples
 

Sqlinsert

INSERT RECORDS INTO A DATABASE TABLE -NOTRE THAT WE COULD HAVE DONE A 'BATCH INSERT' HERE... assumes the existence of a database table in a place where the program can find it also these constants up at the top of your code: const TablesDataDir = 'name_of_directory'; foxOrdersFileName + 'name_of_file'; procedure TMainForm.AddDataButtonClick(Sender: TObject); {open Foxorders.db and add (temporary, test data) records...} var dbFullFileName: String; odTQuery: TQuery; exeFileName, pathToExe: String; newRecCount: Integer; refNo: Integer; refNoStr: String; bmpNameStr: String; begin odTQuery := TQuery.Create(nil); exeFileName := Application.ExeName; pathToExe := ExtractFilePath(exeFileName); dbFullFileName := pathToExe + TablesDataDir + foxOrdersFileName; odTQuery.DatabaseName := ExtractFileDir(dbFullFileName); if (FileExists(dbFullFileName)) then begin refNo := 1; for newRecCount := 1 to 60 do begin refNoStr := '000000' + IntToStr(refNo); bmpNameStr := 'A' + 'MOD' + '0' + IntToStr(refNo) + '.BMP'; Inc(refNo); odTQuery.Close; odTQuery.SQL.Clear; odTQuery.sql.Add('INSERT INTO ' + #39 + dbFullFileName + #39 + ' (OrderReference, ThumbFileName) VALUES (' + #34 + refNoStr + #34 + ',' + #34 + bmpNameStr + #34 + ')'); {execute the query} odTQuery.ExecSql; end; odTQuery.sql.SaveToFile('Test.sql'); end; odTQuery.Free; end;