Jump to content

Filewrite syntax assistance

Go to solution Solved by jchd,

Recommended Posts

In this script, the user is prompted for an email address to update a SQL database mail account named 'test' and saves the new email address to details.txt in which a SQLcmd script is created and saved for later. There are no problems getting the username ($username) but I cannot get all this to write to the file with the proper required syntax

The first line is an example command prompt SQLCmd script which works without issue and of course, not part of the Autoit script.

Autoit opens the required file and does the file write in the 3rd line. But  this does not match the required syntax in line one, and SQL will error out on the syntax when pasted into a cmd prompt. I have spent quite a bit of time trying different syntax, but have had no luck. Any advise is appreciated.

** SqlCmd -E -Q "EXEC msdb.dbo.sysmail_update_account_sp @account_name = 'test', @username = 'nobody@example.com'"

$file = FileOpen(@TempDir & "\details.txt", 1)

FileWrite($file, "SQLcmd -e -q" &  '"EXEC msdb.dbo.sysmail_update_account_sp @account_name = "test", @username = "' & $username & '"' & @CRLF)

** In the saved details.txt, note the different sytax (quotes) around "test" and "nobody@example.com" 
*** Need this to match the first line above

SQLcmd -e -q"EXEC msdb.dbo.sysmail_update_account_sp @account_name = "test", @username = "nobody@example.com"




Edited by Willow
Link to comment
Share on other sites

  • Solution

Your line of script should be like this:

Local $username = "nobody@example.com"
ConsoleWrite("SQLcmd -e -q " &  """EXEC msdb.dbo.sysmail_update_account_sp @account_name = 'test', @username = '" & $username & '''"' & @CRLF)

If a string literal is enclosed with single (resp. double) quotes, you can safely use double (resp. single) quotes inside, verbatim.

If you need to include quotes similar to the string-literal-enclosing quotes, double each of them like I did above.

Alternatively you can still paste them individually the pedestrian way:

Local $username = "nobody@example.com"
ConsoleWrite("SQLcmd -e -q " &  '"' & "EXEC msdb.dbo.sysmail_update_account_sp @account_name = 'test', @username = '" & $username & "'" & '"' & @CRLF)

You probably should add a space after -q even if just for safety, as not all command-line programs understand when options are adjacent to parameters.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...