PDA

View Full Version : OOP in QML, trying to insert data in MySQL with an object



KDEuser
1st November 2012, 18:28
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:

#include "gestionclientes.h"
/* INCLUDED */
#include <QtSql/QMYSQLDriver>
#include "string.h"
#include "iostream"
#include <QtSql/QSqlDatabase>
#include <QtDebug>
#include <QString>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>



gestionClientes::gestionClientes()
{


}



void insertarCliente(QString n, QString a, QString p) {

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("miniaplicacion");
db.setUserName("root");
db.setPassword("medina");
bool ok = db.open();

if(ok == true){
QSqlQuery query;
query.prepare("INSERT INTO clientes (nombre_cliente,apellido_cliente,localidad_cliente ) VALUES ( :nom, :ape, :local)");
query.bindValue(":nom",n);
query.bindValue(":ape",a);
query.bindValue(":local",p);
query.exec();
}else{
qDebug() << db.lastError();
qFatal( "Failed to connect." );
}
}


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.

ChrisW67
2nd November 2012, 00:19
Hello KDEuser, welcome to QtCentre

Is there a problem you would like help with?

KDEuser
3rd November 2012, 18:15
Yes, I want to create a customer object in the QML file to insert new customers.

This is my Qt Creator project with the MySQL, sorry for the words written in Spanish.