Csv to sql
#5

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();
}



Messages In This Thread
[No subject] - by Bubbles - 10-15-2011, 06:22 PM
[No subject] - by BashVendetta - 10-15-2011, 06:56 PM
[No subject] - by Bubbles - 10-15-2011, 07:00 PM
[No subject] - by BashVendetta - 10-15-2011, 07:43 PM
[No subject] - by someone - 10-16-2011, 10:23 AM
[No subject] - by Bubbles - 10-16-2011, 10:31 AM
[No subject] - by someone - 10-16-2011, 10:36 AM
[No subject] - by Bubbles - 10-16-2011, 10:40 AM
[No subject] - by Bubbles - 10-16-2011, 11:25 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)