B
B
BogBel2015-05-14 22:35:08
Qt
BogBel, 2015-05-14 22:35:08

What is the correct way to write QSqlQuery in Qt?

Question in requests.
here is my db ed8cecc337fc45ff9cbd4c20cf9e5cd8.jpg
main:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qDebug() << QSqlDatabase::drivers();
    Widget w;
    w.show();

    return a.exec();
}

widget constructor:
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
   ui->setupUi(this);
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("D:/Qt projects/History/Base.db");
    if(!db.open())
        qDebug()<<"Cant open";
    QSqlQuery query(db);
            query.exec("select Qestion from History where id=0 and id=1 ");
            bool res=query.prepare("select Qestion from History where id=0 and id=1 ");
            qDebug()<<"res="<<res;
            qDebug()<<"select"<<query.isSelect();
            qDebug()<<"valid"<<query.isValid();
            QString name;

           name = query.value(0).toString();
           qDebug()<<"name::"<<name;
            ui->textEdit->setText(name);
            ui->textEdit->setReadOnly(true);
}

}

the output to qDebug looks like this:
Running D:\Qt projects\build-History-Desktop_Qt_5_4_1_MinGW_32bit-Debug\debug\History.exe...
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
res= true
select false
valid false
QSqlQuery::value: not positioned on a valid record
name:: ""
D:\Qt projects\build-History-Desktop_Qt_5_4_1_MinGW_32bit-Debug\debug\History.exe ended with code 0
I can’t understand, I connected the database, I wrote the request, but there is no result (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-05-14
@vilgeforce

Why query.prepare() after query.exec()?
Try query.next() after exec()

B
BlackFregat, 2017-07-08
@BlackFregat

where id=0 and id=1
will never be true..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question