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. Sqlite connection weird thing
Forum Updated to NodeBB v4.3 + New Features

Sqlite connection weird thing

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 5 Posters 3.5k 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.
  • C Offline
    C Offline
    ChrisW67
    wrote on last edited by
    #2

    IIRC Sqlite does not actually create the file at open() but delays until something is written to it. If the file location is in an unwritable location it will fail at the time of the first CREATE TABLE etc. You are using a relative path to the database file. The file will go into the current working directory of the process at that time, which may not be writeable. This is especially the case with Windows when an executable is installed into Program Files. Often that directory is the default working directory and users cannot write there.

    Using an absolute path to a writable location will fix this.

    M 1 Reply Last reply
    3
    • C ChrisW67

      IIRC Sqlite does not actually create the file at open() but delays until something is written to it. If the file location is in an unwritable location it will fail at the time of the first CREATE TABLE etc. You are using a relative path to the database file. The file will go into the current working directory of the process at that time, which may not be writeable. This is especially the case with Windows when an executable is installed into Program Files. Often that directory is the default working directory and users cannot write there.

      Using an absolute path to a writable location will fix this.

      M Offline
      M Offline
      masa4
      wrote on last edited by
      #3

      @ChrisW67 Thanks for reply, i am using linux but i switched to absolute path.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Emre MUTLU
        wrote on last edited by
        #4

        Can you show your code in a little more detail?

        M 1 Reply Last reply
        1
        • M masa4

          I created an sqlite db in project folder. When I try to connect it like this:

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

          db.open() returns true. Whatever i write it in setDatabaseName function, db.open() returns true but actually its not connected, my sql queries does not run. When i put full path, it works correctly. Why this happens any idea?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @masa4 said in Sqlite connection weird thing:

          Why this happens any idea?

          Please add error handling: https://doc.qt.io/qt-6/qsqldatabase.html#lastError and https://doc.qt.io/qt-6/qsqlquery.html#lastError

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • E Emre MUTLU

            Can you show your code in a little more detail?

            M Offline
            M Offline
            masa4
            wrote on last edited by
            #6

            @Emre-MUTLU
            db.cpp:

            DB::DB(const QString &path)
            {
                m_db = QSqlDatabase::addDatabase("QSQLITE");
                m_db.setDatabaseName(path);
            
                if(!m_db.open())
                    qDebug() << "Error: connection with database failed";
                else
                    qDebug() << "Database: connection ok";
            }
            

            Its the constructor. im passing path as default value in header: db.h:

            //DB(const QString& path = "/home/projectfolder/mydbname"); //this one works
            DB(const QString& path = "mydbname"); //but i need this one
            
            jsulmJ 1 Reply Last reply
            0
            • M masa4

              @Emre-MUTLU
              db.cpp:

              DB::DB(const QString &path)
              {
                  m_db = QSqlDatabase::addDatabase("QSQLITE");
                  m_db.setDatabaseName(path);
              
                  if(!m_db.open())
                      qDebug() << "Error: connection with database failed";
                  else
                      qDebug() << "Database: connection ok";
              }
              

              Its the constructor. im passing path as default value in header: db.h:

              //DB(const QString& path = "/home/projectfolder/mydbname"); //this one works
              DB(const QString& path = "mydbname"); //but i need this one
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @masa4 I repeat myself: Please add error handling: https://doc.qt.io/qt-6/qsqldatabase.html#lastError and https://doc.qt.io/qt-6/qsqlquery.html#lastError

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • jsulmJ jsulm

                @masa4 said in Sqlite connection weird thing:

                Why this happens any idea?

                Please add error handling: https://doc.qt.io/qt-6/qsqldatabase.html#lastError and https://doc.qt.io/qt-6/qsqlquery.html#lastError

                M Offline
                M Offline
                masa4
                wrote on last edited by
                #8

                @jsulm Thank you for advice. I added it like:

                DB::DB(const QString &path)
                {
                    m_db = QSqlDatabase::addDatabase("QSQLITE");
                    m_db.setDatabaseName(path);
                
                    if(!m_db.open())
                        qDebug() << "Error: connection with database failed";
                    else
                        qDebug() << "Database: connection ok";
                    qDebug() << m_db.lastError().text();
                }
                

                When i used in this way and set the path's value to a wrong value it returns empty text. no error. and output is "Database: connection ok" but actually its not ok.

                jsulmJ 1 Reply Last reply
                0
                • M masa4

                  @jsulm Thank you for advice. I added it like:

                  DB::DB(const QString &path)
                  {
                      m_db = QSqlDatabase::addDatabase("QSQLITE");
                      m_db.setDatabaseName(path);
                  
                      if(!m_db.open())
                          qDebug() << "Error: connection with database failed";
                      else
                          qDebug() << "Database: connection ok";
                      qDebug() << m_db.lastError().text();
                  }
                  

                  When i used in this way and set the path's value to a wrong value it returns empty text. no error. and output is "Database: connection ok" but actually its not ok.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @masa4 You wrote before: "my sql queries does not run". So, please add error handling in the code where you're executing SQL queries to see what exactly the problem is (https://doc.qt.io/qt-6/qsqlquery.html#lastError).

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Emre MUTLU
                    wrote on last edited by
                    #10

                    maybe your quaries not work right cause your code work for me

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @masa4 You wrote before: "my sql queries does not run". So, please add error handling in the code where you're executing SQL queries to see what exactly the problem is (https://doc.qt.io/qt-6/qsqlquery.html#lastError).

                      M Offline
                      M Offline
                      masa4
                      wrote on last edited by masa4
                      #11

                      @jsulm
                      one function in db.cpp:

                      double DB::selectData()
                      {
                          QSqlQuery query;
                          query.prepare("SELECT col1 FROM mytable");
                      
                          if(query.exec()){
                              query.first();
                              return query.value(0).toDouble();
                          }
                          else{
                              query.lastError().text();
                              return -1;
                          }
                      }
                      

                      mainwidget.h:

                      public:
                      DB *db = new DB;
                      

                      mainwidget.cpp:

                      ui->labeldb->setText(QString::number(db->selectData()));
                      

                      When path value is setted to like "mydbname" the return value -1 show up on my label. If i corrected path with absolute value like "/home/projectfolder/mydbname" i got right value on my label(its 10)
                      But still no error messages. Error messages show as empty text, like this: ""

                      jsulmJ 1 Reply Last reply
                      0
                      • M masa4

                        @jsulm
                        one function in db.cpp:

                        double DB::selectData()
                        {
                            QSqlQuery query;
                            query.prepare("SELECT col1 FROM mytable");
                        
                            if(query.exec()){
                                query.first();
                                return query.value(0).toDouble();
                            }
                            else{
                                query.lastError().text();
                                return -1;
                            }
                        }
                        

                        mainwidget.h:

                        public:
                        DB *db = new DB;
                        

                        mainwidget.cpp:

                        ui->labeldb->setText(QString::number(db->selectData()));
                        

                        When path value is setted to like "mydbname" the return value -1 show up on my label. If i corrected path with absolute value like "/home/projectfolder/mydbname" i got right value on my label(its 10)
                        But still no error messages. Error messages show as empty text, like this: ""

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @masa4 said in Sqlite connection weird thing:

                        But still no error messages

                        Of course not - you are not printing the error message...

                        qDebug() << query.lastError().text();
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          Emre MUTLU
                          wrote on last edited by Emre MUTLU
                          #13
                          double DB::selectData()
                          {
                              QSqlQuery query;
                              query.prepare("SELECT col1 FROM mytable");
                          
                              if(query.exec()){
                                 while(query.first()) {
                                  return query.value(0).toDouble();
                                }
                              }
                              else{
                                  qDebug()<<query.lastError();
                                  return -1;
                              }
                          }
                          

                          try this

                          M jsulmJ 2 Replies Last reply
                          0
                          • jsulmJ jsulm

                            @masa4 said in Sqlite connection weird thing:

                            But still no error messages

                            Of course not - you are not printing the error message...

                            qDebug() << query.lastError().text();
                            
                            M Offline
                            M Offline
                            masa4
                            wrote on last edited by
                            #14

                            @jsulm ow sorry didnt realize it. its:

                            "No query Unable to fetch row"
                            
                            1 Reply Last reply
                            0
                            • E Emre MUTLU
                              double DB::selectData()
                              {
                                  QSqlQuery query;
                                  query.prepare("SELECT col1 FROM mytable");
                              
                                  if(query.exec()){
                                     while(query.first()) {
                                      return query.value(0).toDouble();
                                    }
                                  }
                                  else{
                                      qDebug()<<query.lastError();
                                      return -1;
                                  }
                              }
                              

                              try this

                              M Offline
                              M Offline
                              masa4
                              wrote on last edited by
                              #15

                              @Emre-MUTLU said in Sqlite connection weird thing:

                              if(query.exec()){
                              while(query.first()) {
                              return query.value(0).toDouble();
                              }
                              }

                              nope. same error. But actual problem is this code works when i use absolute path. But not for relative path.

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                Emre MUTLU
                                wrote on last edited by
                                #16

                                did you check db is open when you trying to using query

                                1 Reply Last reply
                                0
                                • E Offline
                                  E Offline
                                  Emre MUTLU
                                  wrote on last edited by Emre MUTLU
                                  #17
                                  double DB::selectData()
                                  {
                                    if(db.open()) {
                                      QSqlQuery query;
                                      query.prepare("SELECT col1 FROM mytable");
                                  
                                      if(query.exec()){
                                         while(query.first()) {
                                          return query.value(0).toDouble();
                                        }
                                      } else{
                                          qDebug()<<query.lastError();
                                          return -1;
                                      }
                                  }else {
                                  qDebug()<<db.lastError();
                                  }
                                  }
                                  

                                  something like this

                                  1 Reply Last reply
                                  0
                                  • E Emre MUTLU
                                    double DB::selectData()
                                    {
                                        QSqlQuery query;
                                        query.prepare("SELECT col1 FROM mytable");
                                    
                                        if(query.exec()){
                                           while(query.first()) {
                                            return query.value(0).toDouble();
                                          }
                                        }
                                        else{
                                            qDebug()<<query.lastError();
                                            return -1;
                                        }
                                    }
                                    

                                    try this

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
                                    Try also:

                                    QSqlQuery query("SELECT col1 FROM mytable");
                                    

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    E M 3 Replies Last reply
                                    0
                                    • jsulmJ jsulm

                                      @Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
                                      Try also:

                                      QSqlQuery query("SELECT col1 FROM mytable");
                                      
                                      E Offline
                                      E Offline
                                      Emre MUTLU
                                      wrote on last edited by Emre MUTLU
                                      #19

                                      @jsulm

                                      double DB::selectData()
                                      {
                                        if(db.open()) {
                                          QSqlQuery query("SELECT col1 FROM mytable");
                                          if(query.exec()){
                                             while(query.first()) {
                                              return query.value(0).toDouble();
                                            }
                                          } else{
                                              qDebug()<<query.lastError();
                                              return -1;
                                          }
                                      }else {
                                      qDebug()<<db.lastError();
                                      }
                                      }
                                      
                                      jsulmJ 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
                                        Try also:

                                        QSqlQuery query("SELECT col1 FROM mytable");
                                        
                                        M Offline
                                        M Offline
                                        masa4
                                        wrote on last edited by
                                        #20

                                        @jsulm said in Sqlite connection weird thing:

                                        isopen returns true. query.prepare() returns false.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • E Emre MUTLU

                                          @jsulm

                                          double DB::selectData()
                                          {
                                            if(db.open()) {
                                              QSqlQuery query("SELECT col1 FROM mytable");
                                              if(query.exec()){
                                                 while(query.first()) {
                                                  return query.value(0).toDouble();
                                                }
                                              } else{
                                                  qDebug()<<query.lastError();
                                                  return -1;
                                              }
                                          }else {
                                          qDebug()<<db.lastError();
                                          }
                                          }
                                          
                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by jsulm
                                          #21

                                          @Emre-MUTLU said in Sqlite connection weird thing:

                                          double DB::selectData()
                                          {
                                          if(db.open()) {
                                          QSqlQuery query("SELECT col1 FROM mytable");
                                          if(query.exec()){
                                          while(query.first()) {
                                          return query.value(0).toDouble();
                                          }
                                          } else{
                                          qDebug()<<query.lastError();
                                          return -1;
                                          }
                                          }else {
                                          qDebug()<<db.lastError();
                                          }
                                          }

                                          You forgot to mention whether this version works...

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

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