Link to home
Start Free TrialLog in
Avatar of Vrtnar
Vrtnar

asked on

Open database at runtime

My database is in my app.exe folder (and I want my app. to allways look for it there and display error otherwise).
Also I need to tell my app that the table to open is in that database.How?
What is the code supposed to look like?

Avatar of Russell Libby
Russell Libby
Flag of United States of America image


What kind of database are you using (pdox, access, dbase, etc)?

Russell

Avatar of Vrtnar
Vrtnar

ASKER

I am using absolute database (*.abs)
well.. assuming you are using a TDataset descendent such as TTable then you would do

table1.databasename := ExtractFilePath(Paramstr(0)); // same dir as .exe

if you want to do a search in the current dir and find the files yourself, you can do this :-

procedure TForm1.Button1Click(Sender: TObject);
    var
        searchrec: tsearchrec;
        databasefiles: tstringlist;
        searchresult: integer;
    begin
        databasefiles := TStringList.Create;
        try
            searchresult := findfirst(extractfilepath(paramstr(0)) + '*.abs', faAnyFile, SearchRec);
            while searchresult = 0 do
            begin
                if pos('.', SearchRec.Name) <> 1 then
                  databasefiles.Add(SearchRec.Name);
                searchresult := FindNext(SearchRec);
            end;
            memo1.lines.assign(databasefiles); // show the list of databse files in a memo
        finally
            databasefiles.Clear;
            databasefiles.Free;
        end;
    end;
if you have 1 database file (file.abs) you can do a simple check like

if FileExists(ExtractFilePath(Paramstr(0)) + 'file.abs') then
  showmessage('file is here, i can continue')
else
  raise exception.create('file.abs does not exist');
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vrtnar

ASKER


procedure TForm1.FormCreate(Sender: TObject);
var
DataBaseName,DatabaseFileName: string;
begin
Path := ExtractFilePath(Application.ExeName);
DatabaseName:= ExtractFilePath(ParamStr(0)) + 'Mydatabase.abs';

should go something like that...?


Avatar of Vrtnar

ASKER

ExcludeTrailingbackslash is specific to a platform ?!
Nope, included in sysutils and my personal coding preference;

szDbFile:=ExcludeTrailingbackslash(ExtractFilePath(Application.ExeName))+'\'+DbFile;

(notice i include the '\' between the path and file name). You can change it to the following if it suits you:

szDbFile:=ExtractFilePath(Application.ExeName)+DbFile;

---
Russell




Avatar of Vrtnar

ASKER

well libby,i did your suggestion and it worked.what i do not understand is the
szDbFile thing.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

The szDbFile "thing" is the Application's path PLUS the database file name which are used to build an absolute file name. The only change required in the example I gave was the 2 constansts: database name and table name.

Russell
Avatar of Vrtnar

ASKER

Loki :

procedure TForm1.FormCreate(Sender: TObject);
var
  DataBaseName, DatabaseFileName: string;
begin
  DatabaseName := 'Mydatabase';
  DatabaseFileName:= ExtractFilePath(ParamStr(0)) + Mydatabase.abs';
  if not FileExists() then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
    end else
ABSDatabase1.DatabaseName:= DatabaseName;     ABSDatabase1.DatabaseFileName:=DatabaseFileName;
ABSDatabase1.Open;
     ABSTable1.DatabaseName:= DatabaseName;  
     ABSTable1.TableName:= 'Mytable';
     ABSTable1.Open;
  end;
  I get stopped here : if not FileExists() then
[Error] Unit1.pas(234): Not enough actual parameters

if not FileExists(DatabaseFileName) then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
    end else