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

How to read date from database and display in QDateEdit

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.0k 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 5 Jul 2022, 12:29 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]))
    
    J 1 Reply Last reply 5 Jul 2022, 12:39
    0
    • L LT-K101
      5 Jul 2022, 12:49

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

      Screenshot (5).png

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Jul 2022, 12:52 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 5 Jul 2022, 13:01
      1
      • L LT-K101
        5 Jul 2022, 12:29

        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]))
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 5 Jul 2022, 12:39 last edited by jsulm 7 May 2022, 12:40
        #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 5 Jul 2022, 12:49
        0
        • J jsulm
          5 Jul 2022, 12:39

          @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 5 Jul 2022, 12:49 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

          J 1 Reply Last reply 5 Jul 2022, 12:52
          0
          • L LT-K101
            5 Jul 2022, 12:49

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

            Screenshot (5).png

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 5 Jul 2022, 12:52 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 5 Jul 2022, 13:01
            1
            • J jsulm
              5 Jul 2022, 12:52

              @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 5 Jul 2022, 13:01 last edited by
              #5

              @jsulm Thanks a lot it works perfectly!

              1 Reply Last reply
              0

              1/5

              5 Jul 2022, 12:29

              • Login

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