Can't connect to database



  • @bool signin::isSuccess(QString email, QString pass)
    {
    if (!db.isOpen()) db.open();
    QSqlQuery * query = new QSqlQuery(db);
    if (!query->isActive()) qDebug()<<"active";
    query->prepare("SELECT secret FROM usr WHERE email = '?';");
    query->addBindValue(email);
    qDebug()<<query->numRowsAffected();
    while (query->next())
    {
    qDebug()<<"im here";
    QString secret = query->value(0).toString();
    qDebug()<<secret;
    if (secret == QString(QCryptographicHash::hash(QString(pass+email).toUtf8(), QCryptographicHash::Md5).toHex()))
    return true;
    }
    return false;
    }

    bool signin::isSuccess(QString name, QString surname, QString birth, QString email,QString pass)
    {
    if (!db.isOpen()) db.open();
    QSqlQuery * query = new QSqlQuery(db);
    query->prepare("INSERT INTO usr (name, surname, birth, email, secret) VALUES (?,?,?,?,?);");
    query->addBindValue(name);
    query->addBindValue(surname);
    query->addBindValue(birth);
    query->addBindValue(email);
    query->addBindValue(QString(QCryptographicHash::hash(QString(pass+email).toUtf8(), QCryptographicHash::Md5).toHex()));
    db.close();
    return query->exec();
    }@

    I call isSuccess (QString, QString, QString, QString,QString) and it works right (I check result in the db).
    Then I call isSuccess (QString, QString) and it can't to connect to database. What's wrong.
    db: Postgres.


  • Lifetime Qt Champion

    Hi,

    Are you sure it can't connect ?

    What does
    @query->lastError()->text()@

    return ?



  • [quote author="SGaist" date="1369049921"]Hi,

    Are you sure it can't connect ?

    What does
    @query->lastError()->text()@

    return ?[/quote]

    nothing,
    QSqlError(-1, "", "")


  • Lifetime Qt Champion

    Well that strange, since when there's no error, it returns a valid QSqlError with "No Error" as a text...

    By the way, did you see that you don't exec your query ?



  • What I see is that in

    @bool signin::isSuccess(QString email, QString pass)@

    you first ask for isActive (with strange logic), then prepare the SQL statement but never execute.

    In

    @bool signin::isSuccess(QString name, QString surname, QString birth, QString email,QString pass)@

    you close the database before executing but you say it works...



  • @bool signin::isSuccess(QString email, QString pass)
    {
    if (db.open())
    {
    QSqlQuery query = db.exec("SELECT secret FROM usr WHERE email = '"+ email +"';");
    while (query.next())
    if (query.record().value("secret") == QString(QCryptographicHash::hash(QString(pass+email).toUtf8(), QCryptographicHash::Md5).toHex()))
    return true;
    }
    db.close();
    return false;
    }@

    hurrra, it works


  • Lifetime Qt Champion

    Why do you open and close the connection to the database in each function ?


Log in to reply
 

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