Question

Generating 2 files, 1 of which contains data for storing into a database, the second of which contains the stuff necessary to upload it to SQL. The code is not compiling and I'm not sure why. As a side note: I'm uploading 10,000 of one relation, 20000 of another, and 200 of another.

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
    static const char alphanum[] =
    "0123456789"
    "!@#$%^&*"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz";
    int i;
    ofstream outFile;
    outFile.open("out.txt");

    for ( i = 1 ; i <= 30200 ; i++ )
{       
  if(i<=10000)
    outFile << rand() % 10  << "|" << alphanum[rand() % 8]<< "|" << alphanum[rand() % 10]<< "|" << rand() % 9 << "\n";

  else if(i>10000 && i<=30000)
    outFile << rand() % 11 << "|" << alphanum[rand() % 200] << "|" << rand() % 10  << "|" << rand() % 12 << "|" << "Nov 11 2013\n";
  else if(i>30000)
    outFile << rand() % 10 << "|" << alphanum[rand() % 8] << "\n";
}
    outFile.close("out.txt");

    outFile.open("populate.txt");
    for(i=1; i<=30200; i++)
      {
      if( i<=10000 )
        outFile << "LOAD DATA\n" << 
          "LOCAL INFILE "data/out.txt"\n" << 
          "REPLACE INTO TABLE Users\n" << 
          "FIELDS TERMINATED BY '|'\n"<<
          "(UserID, Username, Name, NOF);\n";

      else if ( i>10000 && i<=30000 )

        outFile >> "LOAD DATA\n"<< 
          "LOCAL INFILE "data/out.txt"\n" << 
          "REPLACE INTO TABLE Tweets\n" << 
          "FIELDS TERMINATED BY '|'\n" <<
          "(ID, Text, Length, Rating, Timestamp);\n";

      else if (i>30000)

        outFile  >> "LOAD DATA\n" << 
          "LOCAL INFILE "data/out.txt"\n" << 
          "REPLACE INTO TABLE Follows\n" << 
          "FIELDS TERMINATED BY '|'\n" <<
          "(UserID, Username);\n";
      }
    return 0;
}

Thanks for any help

Was it helpful?

Solution

The problem is that you're mixing you output (<<) and input (>>) operators when attempting to write to outFile. That is, you use the input operator instead of the output.

You also have this problem

"LOCAL INFILE "data/out.txt"\n"

Don't you mean

"LOCAL INFILE data/out.txt\n"

or

"LOCAL INFILE \"data/out.txt\"\n"

or (knowing SQL strings are in single quotes)

"LOCAL INFILE 'data/out.txt'\n"

As it's quite obvious you have just done copy-paste with some replacements, you might want to consider making something that put all common code into one place, like say a function? This way, when you change the common code you only have to do it in one place and not in multiple (with the chance of forgetting one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top