使用Delphi和Firedac设置sqlite数据库的相对路径 [英] Setting a relative path to sqlite database with Delphi and Firedac

查看:207
本文介绍了使用Delphi和Firedac设置sqlite数据库的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代替我的上一个问题令人困惑且措辞不佳,这是真正的问题。

In replacement of my previous question which was confusing and poorly formulated, here is the "real" question.

我想知道如何在运行时使用Firedac设置相对位于我的应用程序文件夹的子文件夹中的sqlite数据库的路径。

I would like to known how to set, with Firedac, at runtime, a relative path to a sqlite database located in a subfolder of my application folder.

Jerry Dodge 表示:


任何应用程序都绝对不应依赖同一目录中的可写数据。另外,即使您这样做,也应该确保所有路径至少都相对于应用程序。

Any application should never rely on writable data in the same directory anyway. Also, even if you did, you should make sure all your paths are relative to the application at least.

目前,我想到的应用程序是可移植的,我希望数据库文件存储在主exe文件夹的子文件夹中。

At the moment, the application I have in mind is portable and I would like the database file to be stored in a sub-folder of the main exe folder.

在Form.Create事件中我的主要形式是先使用

On the Form.Create event of my main form, is used first

path := ExtractFilePath(Application.ExeName);

然后在FDConnection中:

And in then for FDConnection :

with FDConnection1 do begin
  Close;
  with Params do begin
    Clear;
    Add('DriverID=SQLite');
    Add('Database='+path+'Data\sqlite.db');
  end;
  Open;
end;

我一直收到一个错误消息,说无法打开数据库文件。

I keep on getting an error saying "unable to open database file".

我不想在FiredDac Connection Editor中设置数据库文件的路径,因为那样它将是绝对路径并绑定到我的机器上,对吗?

I don't want to set the path to the database file in the FiredDac Connection Editor because then it would be absolute and bound to my machine, right ?

我如何设置此路径到数据库文件,以便在用户将应用程序文件夹放在任何位置的任何配置下都可以使用?

How could I set this path to the database file so that it would work in any configuration, wherever the user puts the application folder ?

提前谢谢大家

数学

推荐答案

简介

恕我直言

应用程序中不应对数据库路径和服务器名称进行硬编码

Database path and server name should not be hardcoded in applivcation.

为什么?


  1. 在项目上工作时,您需要执行许多数据库连接,设置数据集,查询等操作。通常,这是在工作数据库。然后,服务器名称和数据库路径与真实数据库的服务器名称和数据库路径不同。

  2. 您应该能够轻松设置服务器名称和数据库路径,而无需编译项目。这允许在随机计算机上正确设置数据库连接参数。

解决方案:


  1. 在设计时设置数据库连接组件,不要在运行时创建它。将所有参数(包括服务器名称,数据库路径,字符集等)设置为数据库的工作副本。这将允许您在设计时设置与此数据库关联的其他组件。
    (在您的回答中,我看到您做的几乎相同。)

  1. Setup the database connection component on design time, do not create it in runtime. Setup all parameters including server name , database path, charset etc. to your working copy of database. This will allow you to set up the other components associated with this database on design time. (In your answer I see you have done almost the same.)

保存服务器名称,数据库路径和您想要的任何其他参数,以外部资源,ini文件,Windows注册表或其他内容。然后在应用程序启动时或在连接数据库之前获取这些参数。

Save server name, database path and any other parameter you want, to an exterrnal resource, ini file, windows registry or something else. Then get these parameters when application started or before connect to database.

在您的情况下,您使用本地服务器,并且与应用程序使用相同的路径,因此无需

In your case, you use local server and the same path as application, so you don't need to store nothing.

关于该问题

代码:

with FDConnection1 do begin
  Close;
  with Params do begin
    Clear;  <-- this removes all parameters
    Add('DriverID=SQLite');
    Add('Database='+path+'Data\sqlite.db');
  end;
  Open;
end;

删除除 DriverID 数据库。可能是由此引起的错误。

removes all other parameters except DriverID and Database. Probably the error arise from that.

如果已经在FDConnection中设置了所有参数:

请勿使用:

FDConnection.Params.Add('Database='+path+'Data\sqlite.db');

这将添加具有相同名称的新参数,但连接将使用第一个。

This will add new parameter with the same name, but connection will use the first one.

这说明了一切都在您的答案中起作用的原因,因为您没有在设计时设置参数数据库:

This explains why everything works in your answer, because you did not set a parameter 'Database' on design time:


第三步是在数据模块上删除FDConnection并设置所有
参数 EXCEPT 数据库文件。

代替使用:

FDConnection.Params.Database := 'Database='+path+'Data\sqlite.db'; 

您可以在
OnDataModuleCreate FDConnectionBeforeConnect 事件

我希望这会很有用。

这篇关于使用Delphi和Firedac设置sqlite数据库的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆