错误:'+'无法添加两个指针

时间:2017-06-10 16:31:27

标签: c++ mysql qt

得到错误“'+'无法添加两个指针”。

任何人都可以解释我究竟是错误的/如何解决它?

信息:movedb获得了包含User_ID(整数)和密码(文本)的表用户。现在生成错误的行,之前返回false,所以我认为由于类型(Qstring和整数)而无法将User_ID与用户名进行比较并进行转换。

login.cpp

#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("");
    db.setDatabaseName("movedb");
    if(!db.open())
    {
        ui->Status->setText("Status: Failed to connect with database");
    }
    else
    {
        ui->Status->setText("Status: Ready to LogIn");
    }
}

Login::~Login()
{
    delete ui;
}

void Login::on_Login_2_clicked()
{
    int username;
    QString password;
    username=ui->lineEdit_Username->text().toInt();
    password=ui->lineEdit_Password->text();
    if(!db.isOpen())
    {
        qDebug()<<"Failed to open database";
        return;
    }
    QSqlQuery qry;
    if(qry.exec("select * from user where User_ID='"+username+"' AND password'"+password+"'"))
    {
        int count=0;
        while(qry.next())
        {
           count++;
        }
        if(count==1)
        {
            ui->Login_status->setText("You have logged in");
        }
        if(count>1)
        {
            ui->Login_status->setText("Something went wrong - please contact with admin");
        }
        if(count<1)
        {
            ui->Login_status->setText("Failed to LogIn");
        }
    }


    else
    {
        ui->label->setText("Something is very Wrong ");
    }
}

- 生成错误的行:

 if(qry.exec("select * from user where User_ID='"+username+"' AND password'"+password+"'"))

1 个答案:

答案 0 :(得分:2)

您要添加char*intchar*QString。此外,查询中的=丢失了,并且数字不应该在引号中。它应该是:

if(qry.exec("select * from user where
    User_ID="+QString::number(username)+" AND password='"+password+"'"))

但更好的想法是准备您的查询以避免这种情况:

qry.prepare("select * from user where User_ID=:userid AND password=':password'");
qry.bindValue(":userid",username);
qry.bindValue(":password",password);
qry.exec();