![]() |
|
Csv to sql - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Off-Topic (The Outer World) (https://lckb.dev/forum/forumdisplay.php?fid=198) +---- Forum: General Discussion (https://lckb.dev/forum/forumdisplay.php?fid=147) +---- Thread: Csv to sql (/showthread.php?tid=269) |
- Bubbles - 10-15-2011 Dose any 1 a idea how i can convter a csv table to a sql file ? - BashVendetta - 10-15-2011 2 - Bubbles - 10-15-2011 Ty but the convert only the first 50 :/ - BashVendetta - 10-15-2011 Google it there will be tools, guides. - someone - 10-16-2011 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(); } - Bubbles - 10-16-2011 o.O i dont understand this ;// - someone - 10-16-2011 o.O i dont understand this ;// It reads the file line by line, on CSV usually first line is the columns line, and the rest are the data rows. The code above is C++ code that reads a CSV file and writes an SQL file. You can still try this: 2 - Bubbles - 10-16-2011 ;( oh have no knowlege in c# or c++ :/ i try goggle but they a no working tools or TuT ;/ - Bubbles - 10-16-2011 i try goggle but they a no working tools or TuT ;/ |