10-16-2011, 10:23 AM
Ive used semicolumns in this example:
ifstream in("file.csv", ios::in);
ofstream out("file.sql",ios::out);
char str[1024];
if(in.is_open()){
//first line which is the table coulumns
in.getline(str,1024);
pch = strtok (str,";");
while (pch != NULL){
stream2.append(pch);
pch = strtok (NULL, ";");
if(pch != NULL){
stream2.append(",");
}
}
while(in.good(){
//getting the data
in.getline(str,1024);
stream.append("INSERT INTO t_table(");
stream.append(stream2.c_str());
stream.append(") VALUES(");
pch = strtok (str,";");
while (pch != NULL){
stream.append(pch);
pch = strtok (NULL, ";");
if(pch != NULL){
stream.append(",");
}
}
stream.append(");\n");
out << stream;
}
in.close();
out.close();
}
With comma its much easier:
ifstream in("file.csv", ios::in);
ofstream out("file.sql",ios::out);
char str[1024];
if(in.is_open()){
//first line which is the table columns
in.getline(str,1024);
stream2.assign(line);
while(in.good(){
//getting the data
in.getline(str,1024);
stream.append("INSERT INTO t_table(");
stream.append(stream2.c_str());
stream.append(") VALUES(");
pch = strtok (str,";");
while (pch != NULL){
stream.append(pch);
pch = strtok (NULL, ";");
if(pch != NULL){
stream.append(",");
}
}
stream.append(");\n");
out << stream;
}
in.close();
out.close();
}

