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.
  • 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
                      • M masa4

                        @jsulm said in Sqlite connection weird thing:

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

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

                        @masa4 said in Sqlite connection weird thing:

                        query.prepare() returns false.

                        Then print the error just after query.prepare()

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

                        M 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
                          #23

                          @jsulm Same error. And i think there is nothing wrong with my sql queries. Because they work if i set dbname with absolute path.

                          1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @masa4 said in Sqlite connection weird thing:

                            query.prepare() returns false.

                            Then print the error just after query.prepare()

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

                            @jsulm the error(i replaced mytable with my table name by the way):

                            "no such table: mytable Unable to execute statement"
                            
                            jsulmJ 1 Reply Last reply
                            0
                            • M masa4

                              @jsulm the error(i replaced mytable with my table name by the way):

                              "no such table: mytable Unable to execute statement"
                              
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #25

                              @masa4 said in Sqlite connection weird thing:

                              no such table: mytable Unable to execute statement

                              And does this table really exist? If a new SQLite database file is created there are no tables and you have to create them first.

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

                              M 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @masa4 said in Sqlite connection weird thing:

                                no such table: mytable Unable to execute statement

                                And does this table really exist? If a new SQLite database file is created there are no tables and you have to create them first.

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

                                @jsulm Yes of course this table exist. Also i can read from this table. But I have to provide full path of sqlite file to setDatabaseName method.

                                db.setDatabaseName("/home/projectfolder/mydbfile"); //Absolute path - Works without any problem
                                db.setDatabaseName("mydbfile"); //Relative path - Does not work
                                

                                I got this errors when i use relative path. But i want to use relative path, database file is inside the project folder.

                                jsulmJ 1 Reply Last reply
                                0
                                • M masa4

                                  @jsulm Yes of course this table exist. Also i can read from this table. But I have to provide full path of sqlite file to setDatabaseName method.

                                  db.setDatabaseName("/home/projectfolder/mydbfile"); //Absolute path - Works without any problem
                                  db.setDatabaseName("mydbfile"); //Relative path - Does not work
                                  

                                  I got this errors when i use relative path. But i want to use relative path, database file is inside the project folder.

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

                                  @masa4 said in Sqlite connection weird thing:

                                  db.setDatabaseName("mydbfile"); //Relative path - Does not work

                                  Are you aware that mydbfile is not the same as /home/projectfolder/mydbfile?
                                  The first one is inside current working directory, which is most probably not /home/projectfolder (so you're creating a new file).

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

                                  M 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @masa4 said in Sqlite connection weird thing:

                                    db.setDatabaseName("mydbfile"); //Relative path - Does not work

                                    Are you aware that mydbfile is not the same as /home/projectfolder/mydbfile?
                                    The first one is inside current working directory, which is most probably not /home/projectfolder (so you're creating a new file).

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

                                    @jsulm Ow, now i see the point. The db inside the code folder, but current working folder name is actually build-project-desktop-debug. Yeah now issue clarified.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • M masa4

                                      @jsulm Ow, now i see the point. The db inside the code folder, but current working folder name is actually build-project-desktop-debug. Yeah now issue clarified.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #29

                                      @masa4
                                      Be aware that the "code" folder simply does not exist at runtime (once you deploy), it is only a thing at design time when inside Qt Creator. And at runtime you do not even know what the "current directory" will be. That is why you need to use absolute rather than relative pathnames, and you might want to look instead at QStandardPaths::StandardLocation to pick and generate a path to a suitable location for your database files.

                                      1 Reply Last reply
                                      2

                                      • Login

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