Old 30th October 2018, 13:57   #1
benjasperson
Junior Member
 
Join Date: Oct 2018
Posts: 7
Building SQL Databse

I'm trying to use SQLCMD in an installer to build a local database. At one point, I was at least able to display the SQL errors, and after removing only (as far as I can remember) unrelated lines of code, I can't even see the errors. I know SQLCMD is still doing something because the output.txt file is being created. It's just blank. There are obviously errors because (and of course, this is the main issue) the database is not being created.

code:
OutFile "db_installer.exe"
!include psexec.nsh
!include LogicLib.nsh

Section

InitPluginsDir
SetOutPath $PLUGINSDIR
File /r SQLCMD
ExecWait '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i createdbonfile.sql -o output.txt'
FileOpen $4 "$PLUGINSDIR\SQLCMD\output.txt" r
FileSeek $4 0
ClearErrors
${DoUntil} ${Errors}
FileRead $4 $1
DetailPrint $1
${LoopUntil} 1 = 0
FileClose $4

# default section end
SectionEnd



I don't think I'm missing any necessary SQLCMD files because I can build the database when just run SQLCMD from command line.

Thanks in advance for the help.
benjasperson is offline   Reply With Quote
Old 30th October 2018, 20:52   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,106
Change "ExecWait" to "MessageBox mb_ok". As the next line, add "ExecWait 'cmd.exe /k'" and the copy the command-line from the messagebox and try it in the terminal and see what happens...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 31st October 2018, 15:41   #3
benjasperson
Junior Member
 
Join Date: Oct 2018
Posts: 7
I don't think I understand your suggestion. I replaced the line containing ExecWait with these two lines:
code:
MessageBox mb_ok '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i createdbonfile.sql -o output.txt'
ExecWait '"cmd.exe" /K $PLUGINSDIR\SQLCMD\sqlcmd.exe -S Cmp-000000\SQLEXPRESS -i $PLUGINSDIR\SQLCMD\createdbonfile.sql -o output.txt'



Doing this I got pretty much the same outcome. Of course, I had to indicate the location of createdbonfile.sql since I was working from the directory where cmd.exe was located.
benjasperson is offline   Reply With Quote
Old 31st October 2018, 16:00   #4
benjasperson
Junior Member
 
Join Date: Oct 2018
Posts: 7
UPDATE: Building SQL Databse

I've started seeing errors again, but they seem random. For example, if I misspell the name of the computer, I would expect to get a failed connection error. Instead, I keep getting an error that the .mdf (referenced inside the createdbonfile.sql file) cannot be opened.

I even tried replacing '-i createdbonfile.sql' with '-Q "SELECT 1"'. Even though there is no reason sqlcmd should be trying to access that .mdf anymore, I still get the error that it cannot be opened.
benjasperson is offline   Reply With Quote
Old 31st October 2018, 16:03   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,106
I meant just ExecWait '"cmd.exe" /K' so you can run your command manually in that terminal to see if it works. It is unlikely that ExecWait is the issue.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 31st October 2018, 20:33   #6
benjasperson
Junior Member
 
Join Date: Oct 2018
Posts: 7
Solution:

It turns out I was doing a couple of things together to cause this issue. First, I forgot to delete the output.txt file I made in a test run before compiling the .nsi file, so the installer was loading that and reading it. Second, I was thinking that my SQLCMD directory was the working directory. Once I added the file path before the file names, things started working.

Thanks for the help.
benjasperson is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Tags
database, sqlcmd

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump