错误 :"+" 无法添加两个指针
Error :'+' cannot add two pointers
出现错误"'+'不能添加两个指针"。
谁能解释我到底出了什么问题/如何解决?
信息:movedb 得到包含User_ID(整数(和密码(文本(的表用户。现在生成错误的行,之前返回了 false,所以我认为User_ID由于类型(Qstring 和整数(而无法与用户名进行比较并进行了转换。
登录.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+"'"))
您正在添加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();
相关文章:
- 一个对象的两个指针.删除了一个指针,对象仍然存在
- 如何将值添加到嵌套结构中,该结构在C++中有两个指针
- 比较两个指针时">="运算符的奇怪行为
- 根据下面的作者,如果两个指针指向不同的数组,则比较的第一个版本将未定义
- 为什么 fdump-class-hierarchy 为虚函数提供了两个指针 int vtable
- 错误 :"+" 无法添加两个指针
- 使与同一数组无关的两个指针减去未定义行为的基本原理是什么?
- 为什么当我在同一内存中各有两个指针时,我必须调用"delete"两次?
- 如何加入两个指针以将其串在一起并将其存储在数组或字符中
- 如何在多线程 c++ 17 程序中交换两个指针
- 这两个指针怎么能有相同的值
- 为什么两个指针都有相同的内存地址
- 两个指针之间的差异是合法的 c++17 常量表达式
- 为什么两个指向相同链接的两个指针在这里表现不同
- 这两个指针代码有什么区别
- 使用指针在Qt Creator UI中的其他两个指针之间切换
- 重写虚函数协变返回类型(两个指针)
- 想要一种在C++中交换两个指针的有效方法
- 在delete()之后不会断言两个指针都指向NULL
- 更改指针指向指针的位置,同时保持其他两个指针的值