Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ****** Please HELP! File & SQL ******

****** Please HELP! File & SQL ******

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdatabasetutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Lai
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • S Steve Lai

      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

      H Offline
      H Offline
      HP
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups