0

I'm on fedora linux, and wanted to try sqlite
I don't understand what is wrong here... What I've made is a simple database and wanted to show contents.

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if (!QSqlDatabase::isDriverAvailable("QSQLITE"))
    {
        qDebug() << "Error on init";
        return -1;
    }

    QSqlDatabase bd = QSqlDatabase::addDatabase("QSQLITE");
    bd.setDatabaseName("Students.db");
    if (!bd.open())
    {
        qDebug() << bd.lastError().text();
        return -2;
    }

    QSqlQuery q;
    if (!q.exec("create table if not exists students"
           "(id integer not null primary key autoincrement,"
           " name varchar(255) not null,"
           " age integer not null)"))
    {
        qDebug() << q.lastError().text();
        return -3;
    }

    q.exec("insert into students (name, age) values ('Renato', 24 )");
    q.exec("insert into students (name, age) values ('Olive', 40 )");
    q.exec("insert into students (name, age) values ('Cucuo', 19 )");
    q.exec("insert into students (name, age) values ('Renzen', 32 )");
    q.exec("insert into students (name, age) values ('Polai', 21 )");
    q.exec("insert into students (name, age) values ('Mogo', 29 )");

    q.exec("select * from students");
    while (q.next())
    {
        qDebug() << q.value("id").toInt();
        qDebug() << q.value("name").toString();
        qDebug() << q.value("age").toInt();
    }

    bd.close();
    return a.exec();
}

The program compiles perfectly without errors and just shows me an empty console, no line is prompted.

Update

I can access the database via command line and it shows that the queries are successfuly done.
now I'm wondering if the qDebug() function is working correctly

Egon Stetmann.
  • 461
  • 6
  • 14

1 Answers1

0

Your insert query is not valid. The if not exists cannot be used in an insert query. You can see the lastError().text() after your inserting queries. Then, if you remove the if not exists parts in all insert queries, all will be done well. However, it seems that you do not want to insert duplicate values. Right? If so, you must define the "name" column as a "unique" column. By this way, if a student is already inserted, it will never be duplicated on insert queries.

if (!QSqlDatabase::isDriverAvailable("QSQLITE"))
{
    qDebug() << "Error on init";
    return -1;
}
qDebug() << "1";
QSqlDatabase bd = QSqlDatabase::addDatabase("QSQLITE");
bd.setDatabaseName("Students.db");
if (!bd.open())
{
    qDebug() << bd.lastError().text();
    return -2;
}
qDebug() << "2";
QSqlQuery q;
if (!q.exec("create table if not exists students"
       "(id integer not null primary key autoincrement,"
       " name varchar(255) not null unique,"
       " age integer not null)"))
{
    qDebug() << q.lastError().text();
    return -3;
}
qDebug() << "3";
q.exec("insert into students (name, age) values ('Renato', 24 )");
q.exec("insert into students (name, age) values ('Olive', 40 )");
q.exec("insert into students (name, age) values ('Cucuo', 19 )");
q.exec("insert into students (name, age) values ('Renzen', 32 )");
q.exec("insert into students (name, age) values ('Polai', 21 )");
q.exec("insert into students (name, age) values ('Mogo', 29 )");
qDebug() << "4";
q.exec("select * from students");
while (q.next())
{
    qDebug() << q.value("id").toInt();
    qDebug() << q.value("name").toString();
    qDebug() << q.value("age").toInt();
}
qDebug() << "5";
bd.close();
return 0;

Output is as follows: Output

  • yeah, I forgot to remove that... the code still doesn't work. it's the same issue, the console is empty looks like it's waiting for something – Egon Stetmann. May 25 '20 at 20:41
  • also entering to the database via command line with "if not exists" works. So I'm writing correctly to the database, what I don't understand is why q.next() doesnt read – Egon Stetmann. May 25 '20 at 20:43
  • The reason is that you have not inserted any record in your database (your database is empty). You can verify it by printing the ```q.size()``` before the while loop. Please put ```qDebug() << q.lastError().text();``` after one of your insert queries. You will see that you have a syntax error in your insert query. The error indicates to the ```if not exists``` part. – Saeed Sayyadipour May 25 '20 at 21:19
  • I ran my recommended code and inserted the output to my answer. Would you please tell me that in your code, the problem occurs in which stage? Does it reach to the lines ```qDebug() << "1";```, or ```qDebug() << "2";```, or ```qDebug() << "3";``` and ...? – Saeed Sayyadipour May 25 '20 at 21:36
  • Please go to the directory of your project and delete the "Student" database before running my code. – Saeed Sayyadipour May 25 '20 at 21:41
  • yeah I tested it, thanks for your time. I think the code is ok. qDebug() does not work even on other proyects and I don't understand why. I think the problem is fedora and I need to reinstall it correctly because the same code runs on windows – Egon Stetmann. May 25 '20 at 23:09