Hi, I'm new in the forum and I'm really bored trying to create an object to insert customers in a MySQL database.

I made a C++ class called gestionclientes (customer management) to make costumers objects. I made a void method called insertarCliente to insert the customers in the database. This is the code:
Qt Code:
  1. #include "gestionclientes.h"
  2. /* INCLUDED */
  3. #include <QtSql/QMYSQLDriver>
  4. #include "string.h"
  5. #include "iostream"
  6. #include <QtSql/QSqlDatabase>
  7. #include <QtDebug>
  8. #include <QString>
  9. #include <QtSql/QSqlError>
  10. #include <QtSql/QSqlQuery>
  11.  
  12.  
  13.  
  14. gestionClientes::gestionClientes()
  15. {
  16.  
  17.  
  18. }
  19.  
  20.  
  21.  
  22. void insertarCliente(QString n, QString a, QString p) {
  23.  
  24. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  25. db.setHostName("localhost");
  26. db.setDatabaseName("miniaplicacion");
  27. db.setUserName("root");
  28. db.setPassword("medina");
  29. bool ok = db.open();
  30.  
  31. if(ok == true){
  32. QSqlQuery query;
  33. query.prepare("INSERT INTO clientes (nombre_cliente,apellido_cliente,localidad_cliente) VALUES ( :nom, :ape, :local)");
  34. query.bindValue(":nom",n);
  35. query.bindValue(":ape",a);
  36. query.bindValue(":local",p);
  37. query.exec();
  38. }else{
  39. qDebug() << db.lastError();
  40. qFatal( "Failed to connect." );
  41. }
  42. }
To copy to clipboard, switch view to plain text mode 

Sorry if my code is a shit, I'm a newbie and I want to learn . I need to dominate the creation of classes in QML to develop comfortable and more easy my Qt applications.

I need to create an object in the main QML file to insert new customers.

I program with Qt Creator.