Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]QSQLITE
Forum Update on Monday, May 27th 2025

[solved]QSQLITE

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AntonZelenin
    wrote on 30 Aug 2015, 11:26 last edited by AntonZelenin
    #1

    I had the same problem as many people with "QMYSQL driver not loaded" and I couldn't solve it. Instead of MySQL i decided to use SQLite, because it seems have been working. SQLite folder is on my desktop, there I have database logs_database. I added QT += sql into .pro file. And wrote some code:

    #include <QCoreApplication>
    #include <QtSql>
    #include <QtCore>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    
    db.setDatabaseName("logs_database");
    

    // I tried to use such way: moved SQLite folder into debug folder and wrote this:
    // QString pathToDB = QDir::currentPath()+QString("\SQLite\logs_database");
    // db.setDatabaseName(pathToDB);

    if(db.open())
       {
          qDebug() << "Opened!";
          QSqlQuery query;
          query.exec("insert into people (Name, id) values ('Michael', 5)");
    }
    else
    {
        qDebug() << "Error: " << db.lastError();
    }
    return a.exec();
    

    }

    Every time it says to me Opened! But command "insert into people (Name, id) values ('Michael', 5)" has no effect

    A 1 Reply Last reply 30 Aug 2015, 12:24
    0
    • M Offline
      M Offline
      mrdebug
      wrote on 30 Aug 2015, 11:34 last edited by
      #2

      I suggest you to insert
      qDebug() << query.lastError().text();
      after exec to have the detailed error or use try - catch

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      A 1 Reply Last reply 30 Aug 2015, 11:45
      0
      • D Offline
        D Offline
        David.G
        wrote on 30 Aug 2015, 11:40 last edited by
        #3

        Do you see the insert in the database? (exec() returns boolean to let you know if it was successfully executed) It doesn't return the record of what was inserted. What effect were you expecting?

        You could do

        #include <QDebug>
        ...
        // add after the insert exec();
         QSqlQuery query("SELECT Name, id FROM people");
        
        while(query.next()) {
        // where value(0) and value(1) represents name, id columns respectively
        qDebug() <<  "name: " << query.value(0).toString();
        qDebug() << "id: " << query.value(1).toString(); 
        
        }
        
        

        I hope this helps you.

        A 1 Reply Last reply 30 Aug 2015, 11:57
        0
        • M mrdebug
          30 Aug 2015, 11:34

          I suggest you to insert
          qDebug() << query.lastError().text();
          after exec to have the detailed error or use try - catch

          A Offline
          A Offline
          AntonZelenin
          wrote on 30 Aug 2015, 11:45 last edited by
          #4

          @mrdebug
          It says: no such table. I'm sure it's because program doesn't work with my database, actually I don't know which database it uses because I can putt some trash like aiefhah instead logs_database and it still says Opened!

          1 Reply Last reply
          0
          • D David.G
            30 Aug 2015, 11:40

            Do you see the insert in the database? (exec() returns boolean to let you know if it was successfully executed) It doesn't return the record of what was inserted. What effect were you expecting?

            You could do

            #include <QDebug>
            ...
            // add after the insert exec();
             QSqlQuery query("SELECT Name, id FROM people");
            
            while(query.next()) {
            // where value(0) and value(1) represents name, id columns respectively
            qDebug() <<  "name: " << query.value(0).toString();
            qDebug() << "id: " << query.value(1).toString(); 
            
            }
            
            

            I hope this helps you.

            A Offline
            A Offline
            AntonZelenin
            wrote on 30 Aug 2015, 11:57 last edited by AntonZelenin
            #5

            @David.G
            If I understood you correctly, I expected that after compilation of this program I will open sqlite, then write select * from people and I will see a new values in this table, but there is no change. I guess it's because of that error: no such table: people Unable to execute statement

            1 Reply Last reply
            0
            • A AntonZelenin
              30 Aug 2015, 11:26

              I had the same problem as many people with "QMYSQL driver not loaded" and I couldn't solve it. Instead of MySQL i decided to use SQLite, because it seems have been working. SQLite folder is on my desktop, there I have database logs_database. I added QT += sql into .pro file. And wrote some code:

              #include <QCoreApplication>
              #include <QtSql>
              #include <QtCore>

              int main(int argc, char *argv[])
              {
              QCoreApplication a(argc, argv);

              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              
              db.setDatabaseName("logs_database");
              

              // I tried to use such way: moved SQLite folder into debug folder and wrote this:
              // QString pathToDB = QDir::currentPath()+QString("\SQLite\logs_database");
              // db.setDatabaseName(pathToDB);

              if(db.open())
                 {
                    qDebug() << "Opened!";
                    QSqlQuery query;
                    query.exec("insert into people (Name, id) values ('Michael', 5)");
              }
              else
              {
                  qDebug() << "Error: " << db.lastError();
              }
              return a.exec();
              

              }

              Every time it says to me Opened! But command "insert into people (Name, id) values ('Michael', 5)" has no effect

              A Offline
              A Offline
              AntonZelenin
              wrote on 30 Aug 2015, 12:24 last edited by
              #6

              Thaks everyone and sorry for stupid questions, it was my mistake. I just put the database file into debug folder and now everything works fine=)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                David.G
                wrote on 30 Aug 2015, 12:24 last edited by
                #7

                @AntonZelenin Ah, alright I think I understand now, my bad if I misunderstood you.

                • When the application starts, did you create the schema? The database will indeed open, or rather create a new one in the case of QSLITE if it doesn't exists, but it will be empty. Thus:
                • If you already have a working database then you would need to point the path there, however, if you have the schema (SQL file with CREATE TABLE IF NOT EXISTS etc) you can execute at the start of the application before it starts all the transactions.
                A 1 Reply Last reply 30 Aug 2015, 12:50
                1
                • D David.G
                  30 Aug 2015, 12:24

                  @AntonZelenin Ah, alright I think I understand now, my bad if I misunderstood you.

                  • When the application starts, did you create the schema? The database will indeed open, or rather create a new one in the case of QSLITE if it doesn't exists, but it will be empty. Thus:
                  • If you already have a working database then you would need to point the path there, however, if you have the schema (SQL file with CREATE TABLE IF NOT EXISTS etc) you can execute at the start of the application before it starts all the transactions.
                  A Offline
                  A Offline
                  AntonZelenin
                  wrote on 30 Aug 2015, 12:50 last edited by
                  #8

                  @David.G
                  No, I didn't create any schema and I not fully understand how to set the path but I'll deal with it later, now my brain needs a rest) Thank you for advices.

                  1 Reply Last reply
                  0

                  2/8

                  30 Aug 2015, 11:34

                  topic:navigator.unread, 6
                  • Login

                  • Login or register to search.
                  2 out of 8
                  • First post
                    2/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved