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

C++ 错误:'+';无法添加两个指针,c++,mysql,qt,C++,Mysql,Qt,出现错误“'+'无法添加两个指针” 谁能给我解释一下到底出了什么问题/怎么解决 信息:movedb获得包含用户ID(整数)和密码(文本)的表用户。现在生成错误的行,早些时候返回false,所以我认为由于类型(Qstring和integer)的原因,无法将User_ID与username进行比较,并进行了转换 login.cpp #include "login.h" #include "ui_login.h" Login::Login(QWidget *parent) : QMainWi

出现错误“'+'无法添加两个指针”

谁能给我解释一下到底出了什么问题/怎么解决

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

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 ");
    }
}

您正在添加
char*
int
char*
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();

拥有原始指针?有智能指针或简单的成员变量。
qry.prepare("select * from user where User_ID=:userid AND password=':password'");
qry.bindValue(":userid",username);
qry.bindValue(":password",password);
qry.exec();