Jump to content
William23668

Unable to create sqlite database at runtime with firedac

Recommended Posts

Hi

 

I am using this code to create the database bute I get error on connect :


 

var
 MyDBFile: string;
begin
  MyDBFile := 'C:\db5.sqlite3';

  if FileExists(MyDBFile) then
    DeleteFile(MyDBFile);

  FDConnection1.Params.Values['Database'] := MyDBFile;
  FDConnection1.Params.Values['DriverID'] := 'SQLite';
  //Connection.Params.Values['Encrypt']  := 'aes-128';
  //Connection.Params.Values['Password'] := '1234';
  FDConnection1.Connected := True;



---------------------------
[FireDAC][Phys][SQLite] ERROR: unable to open database file.
---------------------------

 

Share this post


Link to post

1.use FDConnection1

2.FDConnection1BeforeConnect  >>>>FDConnection1.Params.Values['Database'] := 'Applications.db';

3. FDConnection1AfterConnect>>>>

 FDConnection1.ExecSQL('CREATE TABLE IF NOT EXISTS Files (SectionsId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,MainDirectory TEXT NULL, Files TEXT NULL)');
  FilesFDTable.TableName := 'Files';
  FilesFDTable.Active := True;
 Now you can use table or sql

 

Edited by limelect

Share this post


Link to post
18 minutes ago, limelect said:

1.use FDConnection1

2.FDConnection1BeforeConnect  >>>>FDConnection1.Params.Values['Database'] := 'Applications.db';

3. FDConnection1AfterConnect>>>>

 FDConnection1.ExecSQL('CREATE TABLE IF NOT EXISTS Files (SectionsId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,MainDirectory TEXT NULL, Files TEXT NULL)');
  FilesFDTable.TableName := 'Files';
  FilesFDTable.Active := True;
 Now you can use table or sql

 

I dont understand !! the code display error on connect so it does not connect or create a file.

Share this post


Link to post

Do you have the CreateDatabase param set. e.g.

 

FDConnection1.Params.Values['CreateDatabase']:=BoolToStr(not FileExists(FDConnection1.Params.Database),True);

 

Edited by KenR
  • Thanks 2

Share this post


Link to post
36 minutes ago, KenR said:

Do you have the CreateDatabase param set. e.g.

 

FDConnection1.Params.Values['CreateDatabase']:=BoolToStr(not FileExists(FDConnection1.Params.Database),True); 

 

wow this solved the problem, I never read about this parameter before. many thanks

Share this post


Link to post
14 minutes ago, William23668 said:

wow this solved the problem, I never read about this parameter before. many thanks

Great. Pleased I could help.

Share this post


Link to post

Your code does indeed work without modification.

 

*****Provided you run it with permission to write to the root of "C" drive (i.e. run as Adminstrator).*** 

 

It's not a bug, it's a feature! 🙂

 

 

 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×