Python Forum
[PyQt] to display cotent of database between two dates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] to display cotent of database between two dates
#21
I rectify the indentation and the code : I had written the function (select_by_dates) in the old version, I apologize for the error I just corrected it. and yet the problem still remains without displaying the content of the database between the two dates of lineedit.

The first step is to create a sqlite3 database and a table with the following function:

#function to create sqlite3 database and table :    
def create_database_and_table_correction_loc35R(self):
 		
	monitor_before_correction=self.lineEdit_monitor_before_correction.text()          
	monitor_after_correction=self.lineEdit_monitor_after_correction.text()   
        	
	date_correction =str(time.strftime("%d-%m-%Y"))
	time_correction = str(time.strftime('%H:%M:%S'))    
                            		
	script_path_correction = os.path.dirname(os.path.realpath(__file__))
	new_path_correction=script_path_correction + '\\data_base_ils35R' 
		
	os.makedirs(new_path_correction, exist_ok=True)   

	connexion_correction = sqlite3.connect(new_path_correction+'\\correction.db')
	curseur_correction = connexion_correction.cursor()
	curseur_correction.execute('''CREATE TABLE IF NOT EXISTS correction_loc35R
           (reading_date_correction text, reading_time_correction text,reading_monitor_before_correction text, reading_monitor_after_correction text)  ''') 
              
	if (self.lineEdit_monitor_before_correction.text()=="") :
		self.textEdit_display_correction.setText('<span style="color:#FF0000; font-weight: bold;font-size:15px;">invalid data entry for monitor data before correction.</span>')     
	elif (self.lineEdit_monitor_after_correction.text()==""):
		self.textEdit_display_correction.setText('<span style="color:#FF0000; font-weight: bold;font-size:15px;">invalid data entry for monitor data after correction.</span>')     
       	
	else:
		curseur_correction.execute('''INSERT INTO correction_loc35R VALUES (?,?,?,?) ''',
            (date_correction,time_correction, monitor_before_correction, monitor_after_correction))
            
	connexion_correction.commit()
	self.textEdit_display_correction.setText('<span style=" color:#00aa00;font-size:15px;font-weight: bold;">information saved with succes in database</span>')                                   
	curseur_correction.close()
	connexion_correction.close()
The second step is to create two functions which will set up the start date and the end date for the selection of the dates entered by the user :

#function for star day :
def function_for_start_date(self):
	try:
		start_day = int(self.lineEdit_introduce_start_day.text())
		start_month = int(self.lineEdit_introduce_start_month.text())
		start_year = int(self.lineEdit_introduce_start_year.text())
		start_date = dt.date(start_year, start_month, start_day)
	except ValueError:
		self.textEdit_dispaly_info.setText('<span style="color:#FF0000; font-weight: bold;font-size:15px;">one of the tree champs is invalid</span>') 
	else:
		self.lineEdit_introduce_start_date.setText("{:%Y/%m/%d}".format(start_date))
		self.textEdit_dispaly_info.setText('<span style="color:green; font-weight: bold;font-size:15px;">date created with ("year/month/day") form.</span>') 

#function for end day :
def function_for_end_date(self):
	try:
		end_day = int(self.lineEdit_introduce_end_day.text())
		end_month = int(self.lineEdit_introduce_end_month.text())
		end_year = int(self.lineEdit_introduce_end_year.text())
		end_date = dt.date(end_year, end_month, end_day)
	except ValueError:
		self.textEdit_dispaly_info.setText('<span style="color:#FF0000; font-weight: bold;font-size:15px;">one of the tree champs is invalid</span>') 
	else:
		self.lineEdit_introduce_end_date.setText("{:%Y/%m/%d}".format(end_date))
		self.textEdit_dispaly_info.setText('<span style="color:green; font-weight: bold;font-size:15px;">date created with ("year/month/day") form.</span>') 
The last step is to create a function to select the information from the database between the two dates previously entered by the user:

# function to select recorded data in database between two dates :
def select_by_dates (self):
                
    start_date =self.lineEdit_introduce_start_date.text() # retrieve from function_for_start_date function         
    end_date = self.lineEdit_introduce_end_date.text()  # retrieve from function_for_end_date function 
              
    try :
          
        conn = sqlite3.connect ('correction.db')
        curseur=conn.cursor()
        strSQL = ''
        strSQL += "SELECT * "
        strSQL += "FROM correction_loc35R "
        strSQL += "WHERE date_correction "
        strSQL += "BETWEEN " + start_date.strftime('%Y-%m-%d')
        strSQL += "AND " + end_date.strftime('%Y-%m-%d')
        c=curseur.execute(strSQL)
                        
        liste_select_by_dates_correction=c.fetchall()
        nbr_rows=len(liste_select_by_dates_correction)
                    
        if nbr_rows != 0 :
              
            # set row count
            self.tableWidget_correction_by_dates.setRowCount(nbr_rows)
                      
            for nb in range(nbr_rows):
                for nombre in range(10):
                self.tableWidget_correction_by_dates.setItem(nb,nombre, QTableWidgetItem(liste_select_by_dates_correction[nb][nombre]))
                  
            self.tableWidget_correction_by_dates.resizeRowsToContents()
            self.tableWidget_correction_by_dates.resizeColumnsToContents()        
                          
        conn.commit()
        curseur.close()
        conn.close()
        else :
            print("OK")
  
	except :
        print("error")
		
Reply


Messages In This Thread
RE: to display cotent of database between two dates - by atlass218 - Jan-10-2020, 10:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Display and update the label text which display the serial value jenkins43 5 9,089 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 3,895 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020