Hello,
Sorry for my English. I'm trying to select some data from a database table in order to insert the into a table from another database.
Clarifications:

- The table from where the data is extracted (selected) is named "Partes" and belongs to the "Hospital" database.

- The table where I want to insert the data is named
"Solicitudes_Trabajo" and belongs to the "Empresa" database.

- "seleccionar2" extracts (selects) all the data that belongs to the ID
written by the user in the lineEdit. These data belongs to the table
"Partes" from "Hospital" database.

- "guardar_s2" tries to select (between the data extracted with
"seleccionar2") the data "N_Solicitud" and "Fecha_Emision" that
belong to the "Hospital" database and correspond to the ID written by
the user, and tries to insert them in "Solicitudes_Trabajo" table
from the "Empresa" database.

Qt Code:
  1. hospital=QSqlDatabase::addDatabase("QSQLITE");
  2. hospital.setDatabaseName("C:/Sqlite3/Hospital.sqlite");
  3.  
  4. if(hospital.open()){
  5. qDebug()<<"11.Se ha conectado a la base de datos Hospital";
  6. }else{
  7. qDebug()<<"11.ERROR. No se ha conectado a la base de datos Hospital";
  8. }
  9.  
  10. QSqlQuery seleccionar2;
  11. seleccionar2.prepare("SELECT*FROM Partes WHERE N_Parte=:ID");
  12. seleccionar2.bindValue(":ID",ui->lineEditN_Parte->text());
  13.  
  14. if(seleccionar2.exec())
  15. {
  16. qDebug()<<"12.Los datos del parte se han seleccionado correctamente";
  17. }else{
  18. qDebug()<<"12.ERROR. Los datos del parte no se han seleccionado correctamente";
  19. qDebug()<<"12.ERROR:"<<seleccionar2.lastError();
  20. }
  21.  
  22. seleccionar2.next();
  23.  
  24. //-----------------------------------------------------------------
  25. //-----------------------------------------------------------------
  26. //-----------------------------------------------------------------
  27.  
  28. QSqlQuery guardar_s2;
  29. guardar_s2.prepare("INSERT INTO Solicitudes_Trabajo (N_Solicitud, Fecha_Emision) "
  30. "VALUES (:N_Solicitud, :Fecha_Emision)");
  31. guardar_s2.bindValue(":N_Solicitud", seleccionar2.value(0).toByteArray().constData());
  32. guardar_s2.bindValue(":Fecha_Emision", seleccionar2.value(1).toByteArray().constData());
  33.  
  34. if(guardar_s2.exec( ))
  35. {
  36. ui->label_Guardar->setText("Solicitud guardada correctamente");
  37. qDebug()<<"13.Los datos del parte se han guardado en la Solicitud de Trabajo";
  38. }
  39. else
  40. {
  41. ui->label_Guardar->setText("La solicitud no se ha guardado correctamente");
  42. qDebug()<<"13.ERROR. Los datos del parte no se han guardado en la Solicitud de Trabajo";
  43. qDebug()<<"13.ERROR:"<<guardar_s2.lastError();
  44. }
To copy to clipboard, switch view to plain text mode 

This code shows this error:
Qt Code:
  1. 13.ERROR: QSqlError("", "Parameter count mismatch", "")
To copy to clipboard, switch view to plain text mode 

It's obvious because the database that is open when it tries to insert the data is "Hospital" database and not "Empresa" database that is where it should insert the data.
One of my attempts to solve it was opening "Empresa" database once again in the place that I've written the three lines of dashes with this code:
Qt Code:
  1. empresa=QSqlDatabase::addDatabase("QSQLITE");
  2. empresa.setDatabaseName("C:/Sqlite3/Empresa.sqlite");
  3.  
  4. if(empresa.open()){
  5. qDebug()<<"8.Se ha conectado a la base de datos Empresa";
  6. }else{
  7. qDebug()<<"8.ERROR. No se ha conectado a la base de datos Empresa";
  8. }
To copy to clipboard, switch view to plain text mode 

But it was expected that it doesn't solve the problem.

Comes to my mind as a possible solution the idea of saving the value:
Qt Code:
  1. seleccionar2.value(0).toByteArray().constData()
To copy to clipboard, switch view to plain text mode 
in a variable that doesn't need to access to the database.
But I don't know how it could be possible to save this value inside a variable.

Could anyone help me with this last possible solution of saving the value in a variable?

Does anyone comes up with a better idea?

Thank you very much!