K
K
Kocka2018-12-12 16:02:24
Qt
Kocka, 2018-12-12 16:02:24

Why is the database creating problems and not being displayed in the tableView?

Good afternoon!
This is the third forum on which I write about my problem, there is no solution yet.
I attached the database to my project, everything is successful, there are no errors. It is worth trying to display in tableview - 0 rows.
I checked the code itself in a simpler application with another database: everything works. I tried to open the first database in it - again 0. And then in a simple program the same thing began to happen with the second database. Than it can be caused?
Connection:

spoiler
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //connecting
    auto databasePath = qApp->applicationDirPath() + QStringLiteral("/literatura.sql"); //rewritten path
    qDebug() << "database location:" << databasePath;
    db.setDatabaseName(databasePath);
    if(!db.open())
    {
        qDebug()<<"ERROR WITH DB!!";
    }
    else {qDebug()<<"Successful loading";} //error message

Query+output in tableView:
spoiler
QSqlQueryModel * moddel=new QSqlQueryModel();
   QSqlQuery* qry=new QSqlQuery();
   qry->prepare("SELECT count(*) from literatura");//tady join s tabulkou jazyku a mozna predmetu
   qry->exec();
   moddel->setQuery(*qry);
   ui->tableView->setModel(moddel);
   qDebug() << (moddel->rowCount());

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2018-12-12
@electronik777

please show code example

K
Konstantin Tsvetkov, 2018-12-12
@tsklab

qdebug does not give any, how else can I check this?
QSqlQuery::lastError() .

K
Kocka, 2018-12-12
@Kocka

Actually, the solution to the problem is elementary:
When receiving basic data from another person, you should check these data themselves. This database was not such, because I received the ".sql" format from a classmate, and this is the code for writing in order to create a database. Having run everything through the DB Browser, I got the right answers, it remains only to check the execution of queries. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question