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. Database location is not working if it is taken from external source
Forum Updated to NodeBB v4.3 + New Features

Database location is not working if it is taken from external source

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 314 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.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by
    #1

    I have a simple database in location "c:/data/a.db"
    It is opened and used in program using
    db.open("c:/data/a.db");

    What I want is to store this location in file and take location from there and execute the process

    QString databaseLocation = readTextFile(QString path);
    
    /* the path is also valid and readTextFile("./location.txt") is also returning correct value
    */
    QString readTextFile(QString path)
    {
        QFile file(path);
    
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&file);
            return  in.readAll();
        }
    
        return "";
    }
    // here databaseLocation = "c:/data/a.db"; which is same as of top
    db.open(databaseLocation);
    //this is  not working
    

    So the location given inside open is working and text given there is not working .
    What is wrong with this
    Please check this

    Thank You

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    JonBJ 1 Reply Last reply
    0
    • Thank YouT Thank You

      I have a simple database in location "c:/data/a.db"
      It is opened and used in program using
      db.open("c:/data/a.db");

      What I want is to store this location in file and take location from there and execute the process

      QString databaseLocation = readTextFile(QString path);
      
      /* the path is also valid and readTextFile("./location.txt") is also returning correct value
      */
      QString readTextFile(QString path)
      {
          QFile file(path);
      
          if(file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              QTextStream in(&file);
              return  in.readAll();
          }
      
          return "";
      }
      // here databaseLocation = "c:/data/a.db"; which is same as of top
      db.open(databaseLocation);
      //this is  not working
      

      So the location given inside open is working and text given there is not working .
      What is wrong with this
      Please check this

      Thank You

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #3

      @Thank-You
      This is not possible. If you need exactly the string "c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....

      Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to db.open(), which probably won't then work.

      Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect in.readAll() to read a line from a file; more like in.readLine():

      The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.

      [NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]

      You should look at storing any "setting" like this in QSettings class, not in a file of your own.

      Thank YouT 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Because the path is relative (to the current working directory). You either look for QCoreApplication::applicationDirPath() or better QStandardPaths::writeableLocation()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • Thank YouT Thank You

          I have a simple database in location "c:/data/a.db"
          It is opened and used in program using
          db.open("c:/data/a.db");

          What I want is to store this location in file and take location from there and execute the process

          QString databaseLocation = readTextFile(QString path);
          
          /* the path is also valid and readTextFile("./location.txt") is also returning correct value
          */
          QString readTextFile(QString path)
          {
              QFile file(path);
          
              if(file.open(QIODevice::ReadOnly | QIODevice::Text))
              {
                  QTextStream in(&file);
                  return  in.readAll();
              }
          
              return "";
          }
          // here databaseLocation = "c:/data/a.db"; which is same as of top
          db.open(databaseLocation);
          //this is  not working
          

          So the location given inside open is working and text given there is not working .
          What is wrong with this
          Please check this

          Thank You

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #3

          @Thank-You
          This is not possible. If you need exactly the string "c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....

          Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to db.open(), which probably won't then work.

          Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect in.readAll() to read a line from a file; more like in.readLine():

          The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.

          [NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]

          You should look at storing any "setting" like this in QSettings class, not in a file of your own.

          Thank YouT 1 Reply Last reply
          1
          • JonBJ JonB

            @Thank-You
            This is not possible. If you need exactly the string "c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....

            Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to db.open(), which probably won't then work.

            Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect in.readAll() to read a line from a file; more like in.readLine():

            The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.

            [NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]

            You should look at storing any "setting" like this in QSettings class, not in a file of your own.

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #4

            @JonB Yes I did some mistake
            Sorry for that
            I forgot to take location at the very first of porgram and some code gets executed so
            We should declare at very first of the program

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            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