2017-06-10 66 views
-5

出現錯誤「'+'無法添加兩個指針」。錯誤:'+'無法添加兩個指針

任何人都可以解釋我什麼是錯的/如何解決它?

信息:movedb獲取包含User_ID(整數)和密碼(文本)的表用戶。現在生成錯誤的行,之前返回false,所以我認爲User_ID不能與類型(Qstring和整數)進行比較,並進行轉換。

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+"'")) 
+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();