Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Open database at runtime

Posted on 2006-07-16
13
Medium Priority
?
334 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 600 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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

598 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