What I am trying to do is generate a string for the sqlcmd statement and pass it to the system() function, thus transferring data from a table to a CSV. When I hardcode the string I can access the SQL server and everything runs smoothly. Ultimately what I am trying to do is make a string of concatenated string variables and then pass it to the system() function. When I do that, my CSV file is created with nothing in it except for an error message saying that I am unable to contact the SQL server. It seems that the problem might be with my use of the system() function. Below is my code. Could some please tell me what I’m doing wrong? Thanks in advance for your help.
...
bool CharacterParser(char c)
{…
}
string cleanString(string param){
…
return param;
}
std::string command = cleanString(argv[1]);
std::string sqlServerName = cleanString(argv[2]);
std::string dbName = cleanString(argv[3]);
std::string tableName = cleanString(argv[4]);
std::string csvOut1 = cleanString(argv[5]);
std::string csvOut2 = cleanString(argv[5]);
/*fields for SELECT statement*/
std::string selectString = cleanString(argv[6]);
/*Generate SELECT string*/
for(int i = 7;i<argc;i++)
{
selectString = selectString + ", " + cleanString(argv[i]);
}
/*the generate sql command*/
std::string wholeCommand;
wholeCommand = command + " -S " + sqlServerName + " -d " + dbName + " -E -Q \"SET NOCOUNT ON; SELECT " + selectString + " from " + tableName +"\" -o \"" + csvOut +"\" -s\",\" -W -w 700";
/*Build the string with individual statements-------------to be tested-----*/
/*wholeCommand += command;
wholeCommand += " -S ";
wholeCommand += sqlServerName;
wholeCommand += " -d "
wholeCommand += dbName;
wholeCommand += " -E -Q \"SET NOCOUNT ON; SELECT ";
wholeCommand += selectString;
wholeCommand += " from ";
wholeCommand += tableName;
wholeCommand += "\" -o \""
wholeCommand += csvOut;
wholeCommand += "\" -s\",\" -W -w 700";*/
/*Generate findstr command(to remove dashes from header)*/
std::string csvFix;
csvFix = "\"findstr/r/v/C:\"^[-,]*$\""+csvOut 1+ ">" + csvOut2+"\"";
cout<<endl<<endl<<endl;
/*Preview the sqlcmd with single slashes*/
cout<<"the entire sql command: "<<endl<<wholeCommand<<endl<<endl<<endl;
boost::replace_all(wholeCommand, "\\", "\\\\");/*Add second slashes*/
/*Preview the sqlcmd with double slashes*/
cout<<"the entire sql command after transform: "<<endl<<wholeCommand<<endl<<endl<<endl;
cout<<"entire findStr command before transform: "<<endl<<csvFix<<endl<<endl<<endl;
boost::replace_all(csvFix, "\\", "\\\\");/*Add second slashes*/
cout<<"entire findStr command after transform: "<<endl<<csvFix<<endl<<endl<<endl;
/*Known Working (Hard-coded) sqlcmd code*/
/*system("sqlcmd -S DCS-DBS-SVR01\\SAP_DEV -d DS_CW_PDS_Global -E -Q \"set nocount on;select ObjectCSVName, FieldName , SequenceNum from PST_CSV_DEFINITION \" -o \"F:\\SAP_BOBJ_BODS_SHARED\\Xforce\\CppTesting\\Xforce_Step_4_Skeleton_CSV_Defintion_Table_Frame2dash.csv\" -s\",\" -W -w 700");*/
/*Call sqlcmd using generated string*/
/*system(wholeCommand.c_str());*/
/*Known working (Hard-coded) findStr code*/
/*system("findstr/r/v/C:\"^[-,]*$\" F:\\SAP_BOBJ_BODS_SHARED\\Xforce\\CppTesting\\Xforce_Step_4_Skeleton_CSV_Defintion_Table_Frame2dash.csv>F:\\SAP_BOBJ_BODS_SHARED\\Xforce\\CppTesting\\\\Xforce_Step_4_Skeleton_CSV_Defintion_Table_Frame2noDash.csv");*/
/*Call findStr using generated string*/
system(csvFix.c_str());
cout<<"Press enter to exit:";
getchar();
return 0;
}
Concatenate the sqlcmd, send it to the system() function C++
Moderator: DosItHelp
Re: Concatenate the sqlcmd, send it to the system() function
This forum is primarily about batch files.
Re: Concatenate the sqlcmd, send it to the system() function
Oh. I hadn't realized. I'm new to forums. Thanks for the reply!
Re: Concatenate the sqlcmd, send it to the system() function
Although this is C++:
There are some errors in the 2 strings you are building (wholeCommand, csvFix).
I think you may see all, if you cout the working hardcoded string(s) and the builded one(s) in two lines.
In the "wholeCommand ="-line you are using csvOut (not defined/declared in the given sample source).
In the "csvFix ="-line the "+csvOut 1+ " 'looks' incorrect
These two lines seem to be semantically wrong:
There is no difference in how you are using the system() function in both cases (hardcoded vs dynamically created),
so this cannot be the cause:
It is purely a different string that is passed to this function.
penpen
There are some errors in the 2 strings you are building (wholeCommand, csvFix).
I think you may see all, if you cout the working hardcoded string(s) and the builded one(s) in two lines.
In the "wholeCommand ="-line you are using csvOut (not defined/declared in the given sample source).
In the "csvFix ="-line the "+csvOut 1+ " 'looks' incorrect
These two lines seem to be semantically wrong:
Code: Select all
std::string csvOut1 = cleanString(argv[5]);
std::string csvOut2 = cleanString(argv[5]);
There is no difference in how you are using the system() function in both cases (hardcoded vs dynamically created),
so this cannot be the cause:
It is purely a different string that is passed to this function.
penpen