Estoy haciendo un proyecto para aprobar una asignatura de la universidad en C++
. Esta, entre otras funciones inserta Hoteles en una BBDD
desde un .csv
pero añade valores basura.
Alguien me puede ayudar?
void BaseDeDatos::insertarTodosLosHoteles() {
ifstream archivo("ListaHotelesCSV.csv");
string l;
char delim = ';';
int numHabi;
float pNoche;
getline(archivo, l);
while (getline(archivo, l)) {
stringstream stream(l);
string CodHotel, Nombre, NumH, Localidad, Direccion, prN, codP, codB;
getline(stream, CodHotel, delim);
getline(stream, Nombre, delim);
getline(stream, NumH, delim);
getline(stream, Localidad, delim);
getline(stream, Direccion, delim);
getline(stream, prN, delim);
getline(stream, codP, delim);
getline(stream, codB, delim);
char *sch = new char[CodHotel.length() + 1];
copy(CodHotel.begin(), CodHotel.end(), sch);
sch[CodHotel.length()] ='\0';
char *sn = new char[Nombre.length() + 1];
copy(Nombre.begin(), Nombre.end(), sn);
sn[Nombre.length()] ='\0';
char *sl = new char[Localidad.length() + 1];
copy(Localidad.begin(), Localidad.end(), sl);
sl[Localidad.length()] ='\0';
char *sd = new char[Direccion.length() + 1];
copy(Direccion.begin(), Direccion.end(), sd);
sd[Direccion.length()] ='\0';
char *snh = new char[NumH.length() + 1];
copy(NumH.begin(), NumH.end(), snh);
snh[NumH.length()] ='\0';
char *spn = new char[prN.length() + 1];
copy(prN.begin(), prN.end(), spn);
spn[prN.length()] ='\0';
char *scp = new char[codP.length() + 1];
copy(codP.begin(), codP.end(), scp);
scp[codP.length()] ='\0';
char *scb = new char[codB.length() + 1];
copy(codB.begin(), codB.end(), scb);
scb[codB.length()] ='\0';
sscanf(snh, "%d", &numHabi);
sscanf(spn, "%f", &pNoche);
insertarHotel(sch, sn, numHabi, sl, sd, pNoche, scp, scb);
}
archivo.close();
}
insertarHotel()
es una funcion que inserta un Hotel a la base de datos y tiene este codigo:
void BaseDeDatos::insertarHotel(const char *cd, const char *nom, int numH,
const char *loc, const char *dir, float prec, const char *codP,
const char *codB) {
char query[100];
sprintf(query,
"INSERT INTO Hotel VALUES('%s','%s',%d,'%s','%s',%f,'%s','%s')", cd,
nom, numH, loc, dir, prec, codP, codB);
cout << query << endl;
sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
Y el .csv que recibe no tiene ningun tipo de caracte especial ni nada
string
en lugar dechar *
.insertarHotel
?