We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

brrman
Posts: 11
Joined: Tue Jun 24, 2014 11:26 pm

SQL Script that loads from text File

Hi

I am trying to build an installer that executes a SQL script which gets its data from text files. I have added the script and the text files to the project, but it fails when it can't find the files.

Is there a way to use the Script Replacements function to replace the existing file path with a property like APPDIR or SourceDir? I am not sure what property to use to get to the text file location. The project is archiving using LZMA compression.

For instance, the sql script will look like the following:

TRUNCATE TABLE monograph;
Load Data INFILE 'C:/SQLDBGenerator/monographs.txt' INTO TABLE monograph CHARACTER SET LATIN1 FIELDS TERMINATED BY '|' LINES TERMINATED BY '\r\n' (monographid,class) ;

How can I replace the 'C:/SQLDBGenerator/' with the actual path to the data text files?

thanks
Bryan
Last edited by brrman on Mon Jul 07, 2014 11:21 pm, edited 1 time in total.
Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Re: SQL Script that loads from text File

Hello Bryan and welcome to Advanced Installer forums,

In your SQL Script code you can add a place holder instead of hardcoding your file path. (e.g. filePath). In the Script Replacement tab you can add a replacement as the following:
  • Find what: filePath
  • Replace with: [#yourFile.txt]
where yourFile.txt is the name of the component assigned to the file which you can retrieve from the Organization page.

However, if your database engine has an import tool which can be used to process your files you can launch it from your installation package by using the predefined Launch EXE with working directory custom action. It is recommended to use this approach for performance reasons.

If you have other questions, please let us know.

Best regards,
Dan
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
brrman
Posts: 11
Joined: Tue Jun 24, 2014 11:26 pm

Re: SQL Script that loads from text File

Dan, thanks for replying.

I think, essentially what you are saying is that I could replace C:/SQLDBGenerator// (or some other placeholder text) with a property like [TempFolder] ? (these text files are temp files so there is no component assigned to these files).

Even if I did this, my problem here is that those property paths do not work within MySQL. You'll notice my existing path uses "/" instead of "\" - this is because of how MySQL parses commands. So [TempFolder]MYFILE.txt will equate to C:\users\xxxname\temp and will not be parsed correctly in MYSQL. (an alternative is C:\\ instead of C:/ but the problem is the same since I need to change the property itself)

I need to end up with the following format:
Load Data INFILE 'C:/SQLDBGenerator/monographs.txt' INTO TABLE monograph ...
or
Load Data INFILE 'C:\\SQLDBGenerator\\monographs.txt' INTO TABLE monograph ...

Do multiple script replacements run sequentially? Could I do the above - replacing the placeholder with the property value - and then replace all \ with / using a second script replacement? Or do all the script replacements get applied to the original file?

I will also look at MySQL to see if it can import the scripts on its own. Thanks for that suggestion.

thanks - I hope I am making sense...
Bryan
Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Re: SQL Script that loads from text File

Hi Bryan,

The [TempFolder] is a reference to a Formatted Type so it is resolved at runtime while the script replacement operations from the SQL Script Replacement Dialog are resolved at buildtime.

So, you cannot replace the backslash (' \ ') character with the slash (' / ') character using the sql script replacement support.

Best regards,
Dan
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
brrman
Posts: 11
Joined: Tue Jun 24, 2014 11:26 pm

Re: SQL Script that loads from text File

AH I see - that makes sense, Dan. thanks.

So then, I am confused as to how I can read my data text files from my script.
Since I cannot change my scripts after build-time, how can I adjust my script so that I know where the data files will be installed before the installation happens?
Looks like I have no choice but to hard code my data file locations, though I would not even know if the target computer will even have a C drive.

Do you have any suggestions on what I would use in my script for the file path, and where I could put the data text files to keep it dynamic?

Surely I cannot be the only one that has done this, so I have to be missing something very obvious. :(
Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Re: SQL Script that loads from text File

Hello Bryan,

For this, you can create your own custom action which will parse your path and change the backslash (\) with slash (/). For example, you can create your custom action as a custom action written in C# or a custom action written in C++.

Then, you can go in the “Custom Actions” page and add a predefined Launch attached file custom action with sequence. This type of custom action can execute an application (EXE or DLL) or run a script (VBScript or Java script). Also, the source file will be embedded in the MSI file but will not be deployed at install time with the other application files.

Also, you can use our predefined Execute inline script code custom action and use the following code:

Code: Select all

filePathTemp = Session.Property("filePathProp")

filePathTempNew = Replace(filePathTemp, "\", "/")

Session.Property("filePathNew") = filePathTempNew
where:
  • filePathProp is a property which contains the path to your file;
  • filePathNew is the property used in the Replace with field from the SQL Script Replacement Dialog;
Take a look on the VBScript Replace Function article which may be useful to you.

Please note that you need to add your custom action before the InstallODBC standard action. The standard actions used by Advanced Installer can be revealed using the "Show Standard Action" button on the toolbar of the "Custom Actions" page.

If you need any help, please let me know.

Best regards,
Dan
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
brrman
Posts: 11
Joined: Tue Jun 24, 2014 11:26 pm

Re: SQL Script that loads from text File

Hi Dan

Been a while! :)

I understand the text replace. I will have the user browse to the location of the data files. I will then use vbscript to do a text replace to format that path to be MySQL-friendly.

I need to take that formatted path and do another text replace in the SQL Script file, to replace a text placeholder.

Now, what I don't know is the location of the sql scripts on the target machine. How to I do a replace on a placeholder in the SQL Script? Where does the installer put the SQL Scripts during install time so that I can load them, change them, and save them? From what you have said, the Script Replacement functionality is resolved at build time, so I can;t use that. How can change these scripts at install time?

thanks!
Bryan
brrman
Posts: 11
Joined: Tue Jun 24, 2014 11:26 pm

Re: SQL Script that loads from text File

OK! I have solved this issue, finally.

I could indeed use the Script Replacements function - just assign my placeholder to a public Installer property. My public property would contain the path I formatted via a vbscript inline custom action.

I think I ended up doing what you said in the beginning, Dan, but I was such a novice here that I had trouble with all the terms.

I did use this forum post as a guide and it works great!
How to use a custom property in a SQL script?
http://www.advancedinstaller.com/user-g ... cript.html


thanks for all your help!
Bryan
Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Re: SQL Script that loads from text File

You're welcome Bryan,

I'm glad you got this working.
I think I ended up doing what you said in the beginning, Dan, but I was such a novice here that I had trouble with all the terms.
Take your time Bryan, the learning curve is steep at first but once you get the hang of Advanced Installer you'll be set authoring packages with any requirements.

Our user guide has the exact steps but if you encounter any specific blockers, we're here to help.

Best regards,
Dan
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”