Error passing an object.



  • I have created a function initdb.h with the following methods:

    class initDB
    {
    public:
        initDB();
        QSqlError startDB();
        QSqlError insertDB(Person persona);
    };
    

    and also I have created initdb.cpp with the following insert method:

    QSqlError insertDB(Person p)
    {
        QSqlQuery query;
    
        query.prepare("insert into person values(?, ?, ?)");
    
        query.addBindValue(p.GetNumber());
        query.addBindValue(p.Getnombre());
        query.addBindValue(p.Getapellidos());
      
        query.exec();
        return QSqlError();
    }
    

    The output gives me this error:

    ..\initdb.cpp:42:51: error: no matching function for call to 'QSqlQuery::addBindValue(QString)'
    query.addBindValue(p.GetNumber());

    I work with Java and I am not an expert developer. Could anyone help me? with p.GetNumber is getting an error.

    Thank you very much in advance.

    Edit - Code tags - p3c0



  • @iyustlop Can you show us the header for the Person class? My guess is it isn't returning a value that can be put into a QVariant.

    Here is the definition of addBindValue:

    addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In)

    So the compiler is saying p.GetNumber() is returning something that doesn't fit into that const QVariant &val parameter.



  • class Person
    {
    public:
        QString GetNumber();
        void SetNumber (QString number);
        QString GetName();
        void SetName (QString name);
        QString GetSurname();
        void SetSurname (QString surname);
        Person();
    private:
        QString number;
        QString name;
        QString surname;
    };
    


  • @iyustlop That's weird. The QVariant should handle QString just fine. That error is weird.

    I will need to throw that scenario into a compiler to see if I can duplicate it. I'm heading to bed right now but will check it out in the morning.

    In the meantime can you post the full build log? Just in case there is something other than that error that would explain it.



  • The log gives me the following information.

    C:\Qt\5.7\mingw53_32\include\QtSql/qsqlquery.h:107:10: note: no known conversion for argument 1 from 'QString' to 'const QVariant&'



  • Hi,
    maybe you just have to include the <QVariant> header file.
    -Michael.


  • Qt Champions 2016

    Also I would try directly with QString to see if error still comes.

    query.addBindValue(p.GetNumber());
    ->
    QString Numb("0000000000");
    query.addBindValue(Numb);



  • Another thing to try is to force a QVariant by doing something like:

    query.addBindValue(QVariant(p.GetNumber()));

    With your new error message it's definitely having trouble with the QString->QVariant conversion. So by putting the string directly into a QVariant you should be ok.



  • Thank very much for your effort, indeed.

    I included QVariant and it was a mess. A lot errors appears.

    I have decided rebuild everything from scratch focus on this problem.

    I am following the example of books, the problem is that in this example the implementation of the database is in the header file and I am trying to write all the code in the source file.

    A suggestion: Is it possible update this example not using the header file for all the connection to the data base??

    link to the example: http://doc.qt.io/qt-5/qtsql-books-example.html



  • @iyustlop Sure here ya go I'll make it into header/cpp files for you. You could even make it into a class to be more C++ like which is what I did:

    initdb.h:

    #ifndef INITDB_H
    #define INITDB_H
    
    #include <QVariant>
    #include <QSqlError>
    
    class QString;
    class QSqlQuery;
    class QDate;
    
    class InitDB
    {
    public:
            QSqlError initDb();
    
            void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
                                     const QVariant &genreId, int rating);
            QVariant addGenre(QSqlQuery &q, const QString &name);
            QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate);
    };
    
    #endif
    

    initdb.cpp:

    #include "initdb.h"
    #include <QSqlDatabase>
    #include <QString>
    #include <QSqlQuery>
    #include <QDate>
    
    void InitDB::addBook(QSqlQuery &q, const QString &title, int year, const QVariant &autho
    rId,
                 const QVariant &genreId, int rating)
    {
        q.addBindValue(title);
        q.addBindValue(year);
        q.addBindValue(authorId);
        q.addBindValue(genreId);
        q.addBindValue(rating);
        q.exec();
    }
    
    QVariant InitDB::addGenre(QSqlQuery &q, const QString &name)
    {
        q.addBindValue(name);
        q.exec();
        return q.lastInsertId();
    }
    
    QVariant InitDB::addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
    {
        q.addBindValue(name);
        q.addBindValue(birthdate);
        q.exec();
        return q.lastInsertId();
    }
    
    QSqlError InitDB::initDb()
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");
    
        if (!db.open())
            return db.lastError();
    
        QStringList tables = db.tables();
        if (tables.contains("books", Qt::CaseInsensitive)
            && tables.contains("authors", Qt::CaseInsensitive))
            return QSqlError();
    
        QSqlQuery q;
        if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar,
     author integer, genre integer, year integer, rating integer)")))
            return q.lastError();
        if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar
    , birthdate date)")))
            return q.lastError();
        if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)
    ")))
            return q.lastError();
    
        if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
            return q.lastError();
        QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
        QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2))
    ;
        QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4,
     28));
    
        if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
            return q.lastError();
        QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
        QVariant fiction = addGenre(q, QLatin1String("Fiction"));
        QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
    
        if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating)
    values(?, ?, ?, ?, ?)")))
            return q.lastError();
        addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
        addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
        addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
        addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
        addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
        addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
        addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
        addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
        addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
        addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
    
        return QSqlError();
    }
    


  • I have started from scratch following your indications.

    Now I have a method to insert and I am finishing a method to get from database (the cause of the delay).

    As I mentioned before, thank you very much indeed for your effort.

    I put to solved this post.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.