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()
{
}
db.setHostName("localhost");
db.setDatabaseName("miniaplicacion");
db.setUserName("root");
db.setPassword("medina");
bool ok = db.open();
if(ok == true){
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." );
}
}
#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." );
}
}
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.
Bookmarks