David
David

Reputation: 13580

How do you specify the SQLite database file in FireDAC's TFDPhysSQLiteDriverLink component?

I'm trying to modify one of the FireDAC sample projects in order to use an existing SQLite file as the database source. The sample works fine unmodified and connects to its database. However, I can't figure out where the database it connects to is specified, in order to change it.

According to the documentation, there should be a Database property on the TFDPhysSQLiteDriverLink component. There isn't: it doesn't exist. I even converted the form to text and looked through all components' customized properties, and there's no path defined anywhere. Nor is there in code - the sample is very small and there's no path defined at all.

The other option on the documentation is to include the FireDAC.Phys.SQLite unit, although that doesn't explain how to set the database, since as far as I can tell that unit just includes the component. And when I search for Database properties (see attached image) none of them in any class in that unit seem to be quite what I'm after. The closest is a string that's for a backup component - I doubt that's what I need. There is a SQLiteDatabase property in the TFDPhysSQLiteConnection class but that's read-only.

List of all Database properties defined in the FireDAC.Phys.SQLite  unit

List of all Database properties defined in the FireDAC.Phys.SQLite unit

I also tried creating a temporary connection definition at runtime, by double-clicking the TFDConnection component. That only gives an exception:

Exception double-clicking the TFDConnection component

Exception double-clicking the TFDConnection component

The only solution to this I found is in the XE5 documentation, where it says to set the $(PUBLICDOCUMENTSDIR) environment variable. I already had to do that to get the demo to run (previously, it threw the same exception on the line FDConnection1.Connected := True;; it doesn't now, the demo runs perfectly at runtime.) That change obviously hasn't affected the designer, and I don't even know if I'm looking in the right place, since after all the documentation talks about setting the Database property.

So I'm stumped. Where does it set the database? It's not in the DFM or any streamed properties; it's not in the property defined by the documentation (TFDPhysSQLiteDriverLink.Database doesn't exist, nor does anything that looks like it); it's not in the TFDConnection designtime editor (even though it throws an exception, a file specified as a property here would appear in the streamed DFM, I'd think); it's not in code; ...where else can it be?

(I have never used FireDAC before so am a complete noob, btw. I'm self-teaching via the documentation and samples.)

Upvotes: 1

Views: 2901

Answers (2)

MartynA
MartynA

Reputation: 30715

You don't actually need a TFDPhysSQLiteDriverLink for a minimalist FireDAC project, and using one rather confuses the issue if you're trying to make a connection to a database for the first time.

Try this:

  • Make a note of the name including path of a Sqlite db.

  • Start a new VCL project and drop a TFDconnection, TFDQuery, TDataSource & TDBGrid onto its form and connection them up. Set the TDFQuery's Sql to select * from some table you know exists in the db.

  • Right-click the TFDConnection and select Connection editor from the pop-up.

  • Set the DriverID to SQLite and insert your db name into the Database Value box.

  • Open the FDQuery.

If you compile and run the project, you'll get an exception telling you a class factory for a TFDGUIxWaitCursor is missing (this is the sort of thing I love about FireDAC), but that's easily fixed by dropping one onto your form. Notice that you don't have to connect it using the Object Inspector to any of the other FD components.

After that, you can add a TFDPhysSQLiteDriverLink and set its DriverID to the same as for the TFDConnection.

Upvotes: 2

Agustin Ortu
Agustin Ortu

Reputation: 240

I ussualy roll my own class and handle the OnBeforeConnect event

Something like this

procedure TSQLiteConnection.SQLiteConnectionBeforeConnect(Sender: TObject);
begin
  if not(TFile.Exists(DatabaseFilePath)) then
    Params.Values['OpenMode'] := 'CreateUTF16'
  else
    Params.Values['OpenMode'] := 'ReadWrite';

  Params.Values['Database'] := DatabaseFilePath;
  DriverName := 'SQLite';
end;

The DatabaseFilePath is just a string field of the class, so basically you can put any file path there

TSQLiteConnection is, of course, a TFDConnection descendant

Upvotes: 1

Related Questions