Qt - 如何使用 SQL SELECT COUNT 参数?
Qt - How to use SQL SELECT COUNT with parameters?
我正在用 QT 编写程序,但在编写 SQL 查询 Select 时遇到了问题。
我有一个简单的 table,其中包含如下列:ID、name_or_nickname、姓氏、职业。我有 3 个要在查询中使用的变量:
QString name = "Peter";
QString surname = "Smith";
QString occupation = "New York";
我想运行这个查询使用这些变量:
QSqlDatabase sql;
QSqlQuery query(sql);
QString execute = "SELECT COUNT(name) FROM table1 WHERE name_or_nickname='?' AND surname='?' AND occupation='?';";
query.prepare(execute);
query.bindValue(0, name);
query.bindValue(1, _surname);
query.bindValue(2, _occupation);
query.exec();
if (query.next())
{
rows= query.value(0).toInt();
return true;
}
else
{
qDebug() << query.lastError();
return false;
}
但是它不起作用并且 isActive() 函数 returns 为假,所以查询有问题 - 可能是括号。你能告诉我一个例子我应该如何处理它吗?先感谢您!
=========================================== ==============================
我这里post需要的代码:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <person.h>
#include <QMainWindow>
#include <QString>
#include <QtDebug>
#include <QtSql>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_1_clicked();
private:
Ui::MainWindow *ui;
QSqlDatabase sql;
bool open_or_not;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sql = QSqlDatabase::addDatabase("QSQLITE", "db");
sql.setDatabaseName("E:\folder\database.sqlite3");
sql.close(); // this was called beacuse of the problem with first query
sql.open();
QSqlQuery query(sql);
QString execute = "CREATE TABLE IF NOT EXISTS table1 (id INTEGER UNIQUE PRIMARY KEY, name_or_nickname TEXT, surname TEXT, occupation TEXT);";
query.exec(execute);
qDebug() << "isActive: CREATING TABLE" << query.isActive();
query.clear();
open_or_not = sql.open();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_1_clicked()
{
if(open_or_not)
{
Person person1(_sql, Johnny, Walker, California, this);
bool result = false;
result = person1.search_in_database();
qDebug() << result;
}
}
Person.h:
#ifndef PERSON_H
#define PERSON_H
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtDebug>
#include <QDebug>
#include <QJsonDocument>
#include <QtSql>
namespace Ui {
class Person;
}
class Person : public QDialog
{
Q_OBJECT
public:
explicit Person(QSqlDatabase & sql, QString name_or_nickname, QString surname, QString occupation, QWidget *parent);
~Person();
bool search_in_table();
private:
Ui::Person *ui;
QSqlDatabase sql;
QString name_or_nickname;
QString surname;
QString occupation;
};
#endif // PERSON_H
Person.cpp:
#include "person.h"
#include "ui_person.h"
Person::Person(QSqlDatabase & sql, QString name_or_nickname, QString surname, QString occupation, QWidget *parent) :
QDialog(parent),
ui(new Ui::Person)
{
ui->setupUi(this);
this->sql = sql;
this->name_or_nickname = name_or_nickname;
this->surname = surname;
this->occupation = occupation;
}
Person::~Person()
{
delete ui;
}
bool Person::search_in_table()
{
QSqlQuery query(sql);
int rows = 0;
QString name = this->name_or_nickname;
QString _surname = this->surname;
QString _occupation = this->occupation;
QString execute = "SELECT COUNT(name) FROM table1 WHERE name_or_nickname='?' AND surname='?' AND occupation='?';";
query.prepare(execute);
query.bindValue(0, name);
query.bindValue(1, _surname);
query.bindValue(2, _occupation);
query.exec();
if (query.next())
{
rows= query.value(0).toInt();
return true;
}
else
{
qDebug() << query.lastError();
return false;
}
}
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
您忘记在语句末尾加上双引号
你的:
QString execute = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND occupation='%2';
应该是:
QString execute = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND occupation='%2';"
试试这个代码:
QSqlDatabase _sql;
QSqlQuery _query(_sql);
QString execute_stat = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND
occupation='%2';";
_query.prepare(execute_stat.arg(surname, occupation));
q.exec();
int rows= 0;
if (q.next()) {
rows= q.value(0).toInt();
}
最好使用带参数的查询来防止 SQL
中的不同类型转换问题
QString execute = "SELECT COUNT(name) FROM table1 WHERE surname=? AND occupation=?;";
QSqlQuery query;
query.prepare(execute);
query.bindValue(0, surname);
query.bindValue(1, occupation);
query.exec();
if (query.next()) {
rows= query.value(0).toInt();
} else {
qDebug() << query.lastError(); //check your error here
}
老实说,我不知道为什么上面建议的方法都不起作用。
然而,最简单的版本对我有用:
QString execute = "SELECT COUNT(*) FROM table1 WHERE (name_or_nickname='" + name + "') AND (surname='" + surname + "') AND (occupation='" + occupation + "');";
感谢您的帮助。
我正在用 QT 编写程序,但在编写 SQL 查询 Select 时遇到了问题。 我有一个简单的 table,其中包含如下列:ID、name_or_nickname、姓氏、职业。我有 3 个要在查询中使用的变量:
QString name = "Peter";
QString surname = "Smith";
QString occupation = "New York";
我想运行这个查询使用这些变量:
QSqlDatabase sql;
QSqlQuery query(sql);
QString execute = "SELECT COUNT(name) FROM table1 WHERE name_or_nickname='?' AND surname='?' AND occupation='?';";
query.prepare(execute);
query.bindValue(0, name);
query.bindValue(1, _surname);
query.bindValue(2, _occupation);
query.exec();
if (query.next())
{
rows= query.value(0).toInt();
return true;
}
else
{
qDebug() << query.lastError();
return false;
}
但是它不起作用并且 isActive() 函数 returns 为假,所以查询有问题 - 可能是括号。你能告诉我一个例子我应该如何处理它吗?先感谢您!
=========================================== ==============================
我这里post需要的代码:
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <person.h>
#include <QMainWindow>
#include <QString>
#include <QtDebug>
#include <QtSql>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_1_clicked();
private:
Ui::MainWindow *ui;
QSqlDatabase sql;
bool open_or_not;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sql = QSqlDatabase::addDatabase("QSQLITE", "db");
sql.setDatabaseName("E:\folder\database.sqlite3");
sql.close(); // this was called beacuse of the problem with first query
sql.open();
QSqlQuery query(sql);
QString execute = "CREATE TABLE IF NOT EXISTS table1 (id INTEGER UNIQUE PRIMARY KEY, name_or_nickname TEXT, surname TEXT, occupation TEXT);";
query.exec(execute);
qDebug() << "isActive: CREATING TABLE" << query.isActive();
query.clear();
open_or_not = sql.open();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_1_clicked()
{
if(open_or_not)
{
Person person1(_sql, Johnny, Walker, California, this);
bool result = false;
result = person1.search_in_database();
qDebug() << result;
}
}
Person.h:
#ifndef PERSON_H
#define PERSON_H
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtDebug>
#include <QDebug>
#include <QJsonDocument>
#include <QtSql>
namespace Ui {
class Person;
}
class Person : public QDialog
{
Q_OBJECT
public:
explicit Person(QSqlDatabase & sql, QString name_or_nickname, QString surname, QString occupation, QWidget *parent);
~Person();
bool search_in_table();
private:
Ui::Person *ui;
QSqlDatabase sql;
QString name_or_nickname;
QString surname;
QString occupation;
};
#endif // PERSON_H
Person.cpp:
#include "person.h"
#include "ui_person.h"
Person::Person(QSqlDatabase & sql, QString name_or_nickname, QString surname, QString occupation, QWidget *parent) :
QDialog(parent),
ui(new Ui::Person)
{
ui->setupUi(this);
this->sql = sql;
this->name_or_nickname = name_or_nickname;
this->surname = surname;
this->occupation = occupation;
}
Person::~Person()
{
delete ui;
}
bool Person::search_in_table()
{
QSqlQuery query(sql);
int rows = 0;
QString name = this->name_or_nickname;
QString _surname = this->surname;
QString _occupation = this->occupation;
QString execute = "SELECT COUNT(name) FROM table1 WHERE name_or_nickname='?' AND surname='?' AND occupation='?';";
query.prepare(execute);
query.bindValue(0, name);
query.bindValue(1, _surname);
query.bindValue(2, _occupation);
query.exec();
if (query.next())
{
rows= query.value(0).toInt();
return true;
}
else
{
qDebug() << query.lastError();
return false;
}
}
Main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
您忘记在语句末尾加上双引号
你的:
QString execute = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND occupation='%2';
应该是:
QString execute = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND occupation='%2';"
试试这个代码:
QSqlDatabase _sql;
QSqlQuery _query(_sql);
QString execute_stat = "SELECT COUNT(name) FROM table1 WHERE surname='%1' AND
occupation='%2';";
_query.prepare(execute_stat.arg(surname, occupation));
q.exec();
int rows= 0;
if (q.next()) {
rows= q.value(0).toInt();
}
最好使用带参数的查询来防止 SQL
中的不同类型转换问题QString execute = "SELECT COUNT(name) FROM table1 WHERE surname=? AND occupation=?;";
QSqlQuery query;
query.prepare(execute);
query.bindValue(0, surname);
query.bindValue(1, occupation);
query.exec();
if (query.next()) {
rows= query.value(0).toInt();
} else {
qDebug() << query.lastError(); //check your error here
}
老实说,我不知道为什么上面建议的方法都不起作用。
然而,最简单的版本对我有用:
QString execute = "SELECT COUNT(*) FROM table1 WHERE (name_or_nickname='" + name + "') AND (surname='" + surname + "') AND (occupation='" + occupation + "');";
感谢您的帮助。