如何使用 Inno Setup 使用 .sql 脚本更新数据库

发布于 2024-08-19 22:29:17 字数 269 浏览 4 评论 0原文

我想编译一个设置,该设置将使用用户提供的凭据连接到远程数据库,然后使用 .sql 脚本安装一些数据库组件。

使用 Inno Setup 可以吗?

更多详细信息:

我想要一个自定义表单,要求用户输入数据库地址和凭据,然后运行一个命令来执行更新远程数据库服务器的 sql 脚本。

如果更新成功 - 则成功完成安装。

这是一个相当普遍的问题 - 我有很多定制的设置,应该连接到不同的服务器/运行不同的脚本 - 这个想法是构建一个提供此功能的通用表单。

I'd like to compile a setup that will connect to a remote database using the credentials provided by the user, then install few db components using .sql script.

Is that possible using Inno Setup?

More details:

I'd like to have a custom form, asking the user to enter the database address and credentials, then run a command that will execute an sql script that will update the remote database server.

If the update is successful - complete the installation with success.

This is rather general question - I have a lot of customized setups that should connect to different servers/run different scripts - the idea is to build a generic form that will provide this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-08-26 22:29:18

我不认为你可以有一个完全通用的形式,因为对于不同的服务器,你可能需要一个连接字符串,或者一个服务器名称和一个(可选)端口;对于某些服务器,您将使用系统身份验证,对于其他服务器,您将使用用户名密码元组。

话虽如此,我将为您提供一个小型演示 Inno 脚本,该脚本询问服务器名称和端口、用户名和密码,然后进行一些测试,然后执行一个应用程序,该应用程序(通过代码)提取到临时目录并将被删除由安装程序。您可以使用它作为脚本的起点。拥有一些这样的片段,并根据需要将它们包含在脚本中可能就是您所需要的:

[Setup]
AppID=DBUpdateTest
AppName=Test
AppVerName=Test 0.1
AppPublisher=My Company, Inc.
DefaultDirName={pf}\Test
DefaultGroupName=Test
DisableDirPage=yes
DisableProgramGroupPage=yes
OutputBaseFilename=setup
PrivilegesRequired=none

[Files]
Source: "isql.exe"; DestDir: "{tmp}"; Flags: dontcopy
Source: "update_V42.sql"; DestDir: "{tmp}"; Flags: dontcopy

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Code]
var
  DBPage: TInputQueryWizardPage;

procedure InitializeWizard;
begin
  DBPage := CreateInputQueryPage(wpReady,
    'Database Connection Information', 'Which database is to be updated?',
    'Please specify the server and the connection credentials, then click Next.');
  DBPage.Add('Server:', False);
  DBPage.Add('Port:', False);
  DBPage.Add('User name:', False);
  DBPage.Add('Password:', True);

  DBPage.Values[0] := GetPreviousData('Server', '');
  DBPage.Values[1] := GetPreviousData('Port', '');
  DBPage.Values[2] := GetPreviousData('UserName', '');
  DBPage.Values[3] := GetPreviousData('Password', '');
end;

procedure RegisterPreviousData(PreviousDataKey: Integer);
begin
  SetPreviousData(PreviousDataKey, 'Server', DBPage.Values[0]);
  SetPreviousData(PreviousDataKey, 'Port', DBPage.Values[1]);
  SetPreviousData(PreviousDataKey, 'UserName', DBPage.Values[2]);
  SetPreviousData(PreviousDataKey, 'Password', DBPage.Values[3]);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  ResultCode: Integer;
begin
  Result := True;
  if CurPageID = DBPage.ID then begin
    if DBPage.Values[0] = '' then begin
      MsgBox('You must enter the server name or address.', mbError, MB_OK);
      Result := False;
    end else if DBPage.Values[2] = '' then begin
      MsgBox('You must enter the user name.', mbError, MB_OK);
      Result := False;
    end else if DBPage.Values[3] = '' then begin
      MsgBox('You must enter the user password.', mbError, MB_OK);
      Result := False;
    end else begin
      ExtractTemporaryFile('isql.exe');
      ExtractTemporaryFile('update_V42.sql');
      if Exec(ExpandConstant('{tmp}') + '\isql.exe', '--user ' + DBPage.Values[2]
        + ' --password ' + DBPage.Values[3] + ' --database ' + DBPage.Values[0]
        + ':foo --script update_V42.sql', '',
        SW_HIDE, ewWaitUntilTerminated, ResultCode)
      then begin
        // check ResultCode and set Result accordingly
        Result := ResultCode = 0;
      end else begin
        MsgBox('Database update failed:'#10#10 + SysErrorMessage(ResultCode),
          mbError, MB_OK);
        Result := False;
      end;
    end;
  end;
end;

注意:我还没有完全测试这一点,因此可能需要更多代码来正确清理所有内容。错误处理绝对缺失!

I don't think you can have a completely generic form, as for different servers you may need either a single connection string, or a server name and an (optional) port; for some servers you will use system authentication, for others a user name password tuple.

Having said that I will give you a small demo Inno script that asks for server name and port, user name and password, then makes a few tests, then executes an application that is extracted (by code) to the temp directory and will be deleted by the installer. You can use this as a starting point for your scripts. Having a few of such snippets, and including them in your scripts as necessary will probably be all you need:

[Setup]
AppID=DBUpdateTest
AppName=Test
AppVerName=Test 0.1
AppPublisher=My Company, Inc.
DefaultDirName={pf}\Test
DefaultGroupName=Test
DisableDirPage=yes
DisableProgramGroupPage=yes
OutputBaseFilename=setup
PrivilegesRequired=none

[Files]
Source: "isql.exe"; DestDir: "{tmp}"; Flags: dontcopy
Source: "update_V42.sql"; DestDir: "{tmp}"; Flags: dontcopy

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Code]
var
  DBPage: TInputQueryWizardPage;

procedure InitializeWizard;
begin
  DBPage := CreateInputQueryPage(wpReady,
    'Database Connection Information', 'Which database is to be updated?',
    'Please specify the server and the connection credentials, then click Next.');
  DBPage.Add('Server:', False);
  DBPage.Add('Port:', False);
  DBPage.Add('User name:', False);
  DBPage.Add('Password:', True);

  DBPage.Values[0] := GetPreviousData('Server', '');
  DBPage.Values[1] := GetPreviousData('Port', '');
  DBPage.Values[2] := GetPreviousData('UserName', '');
  DBPage.Values[3] := GetPreviousData('Password', '');
end;

procedure RegisterPreviousData(PreviousDataKey: Integer);
begin
  SetPreviousData(PreviousDataKey, 'Server', DBPage.Values[0]);
  SetPreviousData(PreviousDataKey, 'Port', DBPage.Values[1]);
  SetPreviousData(PreviousDataKey, 'UserName', DBPage.Values[2]);
  SetPreviousData(PreviousDataKey, 'Password', DBPage.Values[3]);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  ResultCode: Integer;
begin
  Result := True;
  if CurPageID = DBPage.ID then begin
    if DBPage.Values[0] = '' then begin
      MsgBox('You must enter the server name or address.', mbError, MB_OK);
      Result := False;
    end else if DBPage.Values[2] = '' then begin
      MsgBox('You must enter the user name.', mbError, MB_OK);
      Result := False;
    end else if DBPage.Values[3] = '' then begin
      MsgBox('You must enter the user password.', mbError, MB_OK);
      Result := False;
    end else begin
      ExtractTemporaryFile('isql.exe');
      ExtractTemporaryFile('update_V42.sql');
      if Exec(ExpandConstant('{tmp}') + '\isql.exe', '--user ' + DBPage.Values[2]
        + ' --password ' + DBPage.Values[3] + ' --database ' + DBPage.Values[0]
        + ':foo --script update_V42.sql', '',
        SW_HIDE, ewWaitUntilTerminated, ResultCode)
      then begin
        // check ResultCode and set Result accordingly
        Result := ResultCode = 0;
      end else begin
        MsgBox('Database update failed:'#10#10 + SysErrorMessage(ResultCode),
          mbError, MB_OK);
        Result := False;
      end;
    end;
  end;
end;

Beware: I haven't fully tested this, so there may be more code necessary to properly clean everything up. Error handling is definitely missing!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文