I have this SLOT:
void ConfSetup::setNextPage()
{
int currentIndex = ui->stackedWidget->currentIndex();
switch(currentIndex)
{
case 1:
{
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
case 2:
{
db.setDatabaseName("mysql");
db.setHostName(ui->serverEdit->text());
db.setPort(ui->portEdit->text().toInt());
db.setUserName(ui->userEdit->text());
db.setPassword(ui->passwordEdit->text());
if(!db.open())
{
QMessageBox::critical(0, trUtf8
("Fail to login"), trUtf8
("Wrong user or password"));
}
else
{
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
}
break;
case 3:
{
query.prepare("SELECT user FROM user WHERE user=:user");
// query.prepare("SELECT user FROM user WHERE user='root'");
query.bindValue(":user", ui->userDbEdit->text());
query.exec();
if(query.numRowsAffected() > 0)
QMessageBox::critical(0, trUtf8
("User exist"), trUtf8
("This user already token"));
else
{
qDebug("%s" , query.executedQuery().toStdString().c_str());
qDebug() << db.databaseName();
qDebug() << db.isOpen();
qDebug() << query.lastError();
qDebug("%s" , query.lastQuery().toStdString().c_str());
qDebug("%d" , query.isActive());
qDebug("%s" , db.driverName().toStdString().c_str());
qDebug() << db.lastError();
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
}
break;
case 4:
{
query.prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
query.bindValue(":database", ui->dbNameEdit->text());
query.exec();
if(query.numRowsAffected() == 1)
QMessageBox::critical(0, trUtf8
("Fail to create database"), trUtf8
("This database already exist"));
else
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
case 5:
{
query.prepare("INSERT INTO Users(User, Password)" \
"VALUES(:user, :password)");
query.bindValue(":user", ui->userEdit->text());
query.bindValue(":password", ui->passwordEdit->text());
query.exec();
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
}
// ui->comboBox->addItem(ui->userDbEdit->text());
// ui->lineEdit->setText(addSlashes(ui->slashesEdit->text()));
}
void ConfSetup::setNextPage()
{
int currentIndex = ui->stackedWidget->currentIndex();
switch(currentIndex)
{
case 1:
{
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
case 2:
{
db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("mysql");
db.setHostName(ui->serverEdit->text());
db.setPort(ui->portEdit->text().toInt());
db.setUserName(ui->userEdit->text());
db.setPassword(ui->passwordEdit->text());
if(!db.open())
{
QMessageBox::critical(0, trUtf8("Fail to login"), trUtf8("Wrong user or password"));
}
else
{
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
}
break;
case 3:
{
QSqlQuery query;
query.prepare("SELECT user FROM user WHERE user=:user");
// query.prepare("SELECT user FROM user WHERE user='root'");
query.bindValue(":user", ui->userDbEdit->text());
query.exec();
if(query.numRowsAffected() > 0)
QMessageBox::critical(0, trUtf8("User exist"), trUtf8("This user already token"));
else
{
qDebug("%s" , query.executedQuery().toStdString().c_str());
qDebug() << db.databaseName();
qDebug() << db.isOpen();
qDebug() << query.lastError();
qDebug("%s" , query.lastQuery().toStdString().c_str());
qDebug("%d" , query.isActive());
qDebug("%s" , db.driverName().toStdString().c_str());
qDebug() << db.lastError();
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
}
break;
case 4:
{
QSqlQuery query;
query.prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
query.bindValue(":database", ui->dbNameEdit->text());
query.exec();
if(query.numRowsAffected() == 1)
QMessageBox::critical(0, trUtf8("Fail to create database"), trUtf8("This database already exist"));
else
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
case 5:
{
QSqlQuery query;
query.prepare("INSERT INTO Users(User, Password)" \
"VALUES(:user, :password)");
query.bindValue(":user", ui->userEdit->text());
query.bindValue(":password", ui->passwordEdit->text());
query.exec();
ui->stackedWidget->setCurrentIndex(currentIndex + 1);
}
break;
}
// ui->comboBox->addItem(ui->userDbEdit->text());
// ui->lineEdit->setText(addSlashes(ui->slashesEdit->text()));
}
To copy to clipboard, switch view to plain text mode
And the connection like so:
connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
//etc
connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
//etc
To copy to clipboard, switch view to plain text mode
The query is correct because I test it in phpMyAdmin, qDebug output show this:
SELECT user FROM user WHERE user=? //should be ... user = user, but formated like this because MySQL doesn't support native prepared statment
"mysql"
true
SELECT user FROM user WHERE user=:user
1
QMYSQL
SELECT user FROM user WHERE user=? //should be ... user = user, but formated like this because MySQL doesn't support native prepared statment
"mysql"
true
QSqlError(-1, "", "")
SELECT user FROM user WHERE user=:user
1
QMYSQL
QSqlError(-1, "", "")
To copy to clipboard, switch view to plain text mode
Even if I execute this query:
query.prepare("SELECT user FROM user WHERE user='root'");
query.prepare("SELECT user FROM user WHERE user='root'");
To copy to clipboard, switch view to plain text mode
query still return -1.
Bookmarks