Solved

Open database at runtime

Posted on 2006-07-16
13
330 Views
Last Modified: 2011-09-20
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?

0
Comment
Question by:Vrtnar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119372

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

Russell

0
 

Author Comment

by:Vrtnar
ID: 17119382
I am using absolute database (*.abs)
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17119420
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;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17119431
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');
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 150 total points
ID: 17119438
Example that uses Absolute database components

Russell

----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ABSMain;

type
  TForm1            =  class(TForm)
     ABSDatabase1:  TABSDatabase;
     ABSTable1:     TABSTable;
     procedure      FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1:            TForm1;

// Change to your database and table name
const
  DbFile            =  'Demos.abs';
  DbTable           =  'employee';

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var  szDbFile:      String;
begin

  szDbFile:=ExcludeTrailingbackslash(ExtractFilePath(Application.ExeName))+'\'+DbFile;
  if FileExists(szDbFile) then
  begin
     ABSDatabase1.DatabaseName:='Demos';
     ABSDatabase1.DatabaseFileName:=szDbFile;
     ABSDatabase1.Open;
     ABSTable1.DatabaseName:='Demos';
     ABSTable1.TableName:=DbTable;
     ABSTable1.Open;
  end
  else
     raise Exception.CreateFmt('Database file "%s" does not exist!', [DbFile]);

end;

end.

--- dfm ---
object Form1: TForm1
  Left = 285
  Top = 114
  Width = 529
  Height = 334
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ABSDatabase1: TABSDatabase
    CurrentVersion = '5.06 '
    DatabaseName = 'Demos'
    Exclusive = False
    MaxConnections = 500
    MultiUser = False
    SessionName = 'Default'
    Left = 112
    Top = 12
  end
  object ABSTable1: TABSTable
    CurrentVersion = '5.06 '
    InMemory = False
    ReadOnly = False
    Exclusive = False
    Left = 140
    Top = 12
  end
end




0
 

Author Comment

by:Vrtnar
ID: 17119444

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...?


0
 

Author Comment

by:Vrtnar
ID: 17119468
ExcludeTrailingbackslash is specific to a platform ?!
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119495
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




0
 

Author Comment

by:Vrtnar
ID: 17119501
well libby,i did your suggestion and it worked.what i do not understand is the
szDbFile thing.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 50 total points
ID: 17119503
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);

//and as russel points out it would look like

     ABSDatabase1.DatabaseName:= DatabaseName; // 'MyDatabase'
     ABSDatabase1.DatabaseFileName:=DatabaseFileName; // e.g. 'c:\...\Mydatabase.abs';
     ABSDatabase1.Open;
     ABSTable1.DatabaseName:= DatabaseName;  // 'MyDatabase'
     ABSTable1.TableName:= 'Users'; // or some other table in your database
     ABSTable1.Open;
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119531

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
0
 

Author Comment

by:Vrtnar
ID: 17119551
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
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119557

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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 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