我使用 pyqt gui 应用程序使用以下函数从我的数据库中提取历史记录def historique_jr_date(self): database = QSqlDatabase("QPSQL") database.setHostName("localhost") database.setDatabaseName("database") database.setUserName("postgres") database.setPassword("password") database.open() model_hjd = QSqlTableModel(db=database) model_hjd.setTable('transactions') model_hjd.setEditStrategy(QSqlTableModel.OnManualSubmit) date = str(self.dateEdit_2.text()) selected_day = self.dateEdit_2.date() last_day = selected_day.addDays(-1) last_day_str = last_day.toString(self.dateEdit_2.displayFormat()) print(last_day_str) self.tableView_3.setModel(model_hjd) self.tableView_3.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) #idx = model_ft.fieldIndex("max(id)") model_hjd.setSort(7, Qt.DescendingOrder) model_hjd.select() filter_ft = "(date_d, time_d) > ('%s'::date, '16:00:00'::time) AND (date_d, time_d) < ('%s'::date, '16:00:00'::time) " % (selected_day, last_day) model_hjd.setFilter(filter_ft) self.connection = psycopg2.connect(user="postgres", password="password", host="localhost", database="database") self.cur = self.connection.cursor() self.cur.execute( '''SELECT SUM(montant) AS total FROM transactions WHERE (date_d, time_d) > ('%s'::date, '16:00:00'::time) AND (date_d, time_d) < ('%s'::date, '16:00:00'::time)''',(selected_day, last_day)) result = self.cur.fetchall() for i in result: o = i[0] oo = str(o) self.lineEdit_19.setText(oo)
1 回答
倚天杖
TA贡献1828条经验 获得超3个赞
selected_day
和的类型last_day
是。QDate
将它们转换为字符串,例如:
(selected_day.toString(Qt.ISODate), last_day.toString(Qt.ISODate))
添加回答
举报
0/150
提交
取消