Results 1 to 3 of 3

Thread: OOP in QML, trying to insert data in MySQL with an object

  1. #1
    Join Date
    Nov 2012
    Location
    Cádiz, Andalucía
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default OOP in QML, trying to insert data in MySQL with an object

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: OOP in QML, trying to insert data in MySQL with an object

    Hello KDEuser, welcome to QtCentre

    Is there a problem you would like help with?

  3. #3
    Join Date
    Nov 2012
    Location
    Cádiz, Andalucía
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OOP in QML, trying to insert data in MySQL with an object

    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.
    Attached Files Attached Files

Similar Threads

  1. How to insert BLOB into MySQL with Qt?
    By babygal in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2010, 13:26
  2. Fast insert record into MySql
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2010, 10:08
  3. unable to insert into mysql table
    By mohanakrishnan in forum Newbie
    Replies: 4
    Last Post: 5th December 2009, 06:02
  4. INSERT query with MySQL problem
    By timmyg in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2008, 22:52
  5. MYSQL insert
    By allensr in forum Newbie
    Replies: 4
    Last Post: 14th August 2006, 17:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.