****** Please HELP! File & SQL ******
-
Hi Everyone, I'm using Visual C++6 and have this problem and hope you can help me out! What I'm trying to do is to read data from a TEXT FILE and plug the values into a CString, which will be used as a SQL statement. For example, let's say I have the follwing data in a TEXT FILE: CustomerName, ID, INTEGER, FirstName, STRING, LastName, STRING, etc... How can I read the values from the TEXT FILE one by one so at the end, I'll come up with a statement such as: SQL="CREATE TABLE CustomerName(ID INTEGER, FirstName STRING, LastName STRING, etc...)" I would like to put the reading of the values from the TEXT FILE into a loop so that no matter how many FIELDS the text files contain, the loop will be able to handle it and put all the values into a SQL statment. For example, if I have the following int the TEXT FILE: (3 FILEDS and 3 VALUES OF THE FIELD) CustomerName, ID, INTEGER, FirstName, STRING, LastName, STRING I would like: SQL="CREATE TABLE CustomerName(ID INTEGER, FirstName STRING, LastName STRING)" BUT if I only have 1 FIELD AND VALUES: CustomerName, ID, INTEGER The reading of the TEXT FILE will be dynamic and the SQL would be: SQL="CREATE TABLE CustomerName(ID INTEGER)" Hope you can understand what I'm trying to do. I've tried to do it in a loop myself but I had some trouble, so If anyone has any solutions, PLMK! Thanks! Steve
-
Hi Everyone, I'm using Visual C++6 and have this problem and hope you can help me out! What I'm trying to do is to read data from a TEXT FILE and plug the values into a CString, which will be used as a SQL statement. For example, let's say I have the follwing data in a TEXT FILE: CustomerName, ID, INTEGER, FirstName, STRING, LastName, STRING, etc... How can I read the values from the TEXT FILE one by one so at the end, I'll come up with a statement such as: SQL="CREATE TABLE CustomerName(ID INTEGER, FirstName STRING, LastName STRING, etc...)" I would like to put the reading of the values from the TEXT FILE into a loop so that no matter how many FIELDS the text files contain, the loop will be able to handle it and put all the values into a SQL statment. For example, if I have the following int the TEXT FILE: (3 FILEDS and 3 VALUES OF THE FIELD) CustomerName, ID, INTEGER, FirstName, STRING, LastName, STRING I would like: SQL="CREATE TABLE CustomerName(ID INTEGER, FirstName STRING, LastName STRING)" BUT if I only have 1 FIELD AND VALUES: CustomerName, ID, INTEGER The reading of the TEXT FILE will be dynamic and the SQL would be: SQL="CREATE TABLE CustomerName(ID INTEGER)" Hope you can understand what I'm trying to do. I've tried to do it in a loop myself but I had some trouble, so If anyone has any solutions, PLMK! Thanks! Steve
Hi, try this:
CString BuildSQL( const CString& sStringFromFile )
{
CString sSQL, sTemp1, sTemp2;sSQL = \_T("CREATE TABLE "); // string at position 0 is the table name if( AfxExtractSubString( sTemp1, sStringFromFile, 0, ',' ) ) { sSQL += sTemp1; sSQL += "( "; // now add the column names and the column types for( int i = 1; ; i +=2 ) { if( AfxExtractSubString( sTemp1, sStringFromFile, i, ',' ) && AfxExtractSubString( sTemp2, sStringFromFile, i + 1, ',' ) ) { sSQL += sTemp1; sSQL += sTemp2; sSQL += \_T(", "); } else { break; } } // remove the last ", " sSQL.ReleaseBuffer( sSQL.GetLength() - 2 ); sSQL += \_T(" )"); } return sSQL;
}
Regards Holger Persch