How to read date from database and display in QDateEdit
-
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]))
-
@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"))
-
@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. -
@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"))