Solved

Open database at runtime

Posted on 2006-07-16
13
311 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

9 Experts available now in Live!

Get 1:1 Help Now