Solved

Open database at runtime

Posted on 2006-07-16
13
314 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
Communication Between RC4 Delphi <-> PHP 3 101
Delphi selector screen 2 70
Delphi...Split view - idea? 1 42
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now