Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to read date from database and display in QDateEdit

How to read date from database and display in QDateEdit

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.3k 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.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    I want to read this date format (2000-02-14) from database and display in QDateEdit but it does not work.I used .toPyDate() to insert the date into database and the datatype used for this date is TEXT. I have spent days trying to figure it out but I cannot get the solution. Please any help . Below is the code I tried, thanks in advance.

    search_id = self.ui.lineEdit_132.text()
    
    cur.execute('''SELECT * FROM details WHERE staff_no=?''', ([search_id]))  
    data = cur.fetchall()
               
    firstname = self.ui.lineEdit.setText(data[0][1])
    dob = self.ui.dateEdit.setDate(QDate.fromString(data[0][2]))
    
    jsulmJ 1 Reply Last reply
    0
    • L LT-K101

      @jsulm the image below shows how the date looks like in the database, the date format is yyyy-M-d.

      Screenshot (5).png

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

      @LT-K101 said in How to read date from database and display in QDateEdit:

      the date format is yyyy-M-d

      Then pass this format to fromString method:

      dob = self.ui.dateEdit.setDate(QDate.fromString(data[0][2], "yyyy-M-d"))
      

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

      L 1 Reply Last reply
      1
      • L LT-K101

        I want to read this date format (2000-02-14) from database and display in QDateEdit but it does not work.I used .toPyDate() to insert the date into database and the datatype used for this date is TEXT. I have spent days trying to figure it out but I cannot get the solution. Please any help . Below is the code I tried, thanks in advance.

        search_id = self.ui.lineEdit_132.text()
        
        cur.execute('''SELECT * FROM details WHERE staff_no=?''', ([search_id]))  
        data = cur.fetchall()
                   
        firstname = self.ui.lineEdit.setText(data[0][1])
        dob = self.ui.dateEdit.setDate(QDate.fromString(data[0][2]))
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @LT-K101 said in How to read date from database and display in QDateEdit:

        to insert the date into database and the datatype used for this date is TEXT

        How exactly does the date string look like in your DB?
        You can use https://doc.qt.io/qt-6/qdate.html#fromString-4 to convert a string to a QDate, but you need to know the format of the date string.

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

        L 1 Reply Last reply
        0
        • jsulmJ jsulm

          @LT-K101 said in How to read date from database and display in QDateEdit:

          to insert the date into database and the datatype used for this date is TEXT

          How exactly does the date string look like in your DB?
          You can use https://doc.qt.io/qt-6/qdate.html#fromString-4 to convert a string to a QDate, but you need to know the format of the date string.

          L Offline
          L Offline
          LT-K101
          wrote on last edited by
          #3

          @jsulm the image below shows how the date looks like in the database, the date format is yyyy-M-d.

          Screenshot (5).png

          jsulmJ 1 Reply Last reply
          0
          • L LT-K101

            @jsulm the image below shows how the date looks like in the database, the date format is yyyy-M-d.

            Screenshot (5).png

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

            @LT-K101 said in How to read date from database and display in QDateEdit:

            the date format is yyyy-M-d

            Then pass this format to fromString method:

            dob = self.ui.dateEdit.setDate(QDate.fromString(data[0][2], "yyyy-M-d"))
            

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

            L 1 Reply Last reply
            1
            • jsulmJ jsulm

              @LT-K101 said in How to read date from database and display in QDateEdit:

              the date format is yyyy-M-d

              Then pass this format to fromString method:

              dob = self.ui.dateEdit.setDate(QDate.fromString(data[0][2], "yyyy-M-d"))
              
              L Offline
              L Offline
              LT-K101
              wrote on last edited by
              #5

              @jsulm Thanks a lot it works perfectly!

              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