Search code examples
pythonsqlitereportlab

Unable to read SQL data into a ReportLab rows


I'm working on a reportlab project here where data is read from an SQlite3 table. It is not able to read and convert it to the correct format for the system to read and post. Initially I called in the data from a csv file which worked. I am notable to present this in the correct format for perfomance.

str_rankings = (self.choose_class_combo.currentText() + ' ' + self.choose_term_combo.currentText() + ' ' + self.choose_year_combo.currentText() + ' ' + " STREAM RANKINGS.pdf")
pdf2=SimpleDocTemplate(str_rankings)
frame1 = Frame(30,20,900,600)
land = PageTemplate(id='l',pagesize=[950,650],frames=[frame1])
heading = self.choose_class_combo.currentText()+' '+self.choose_term_combo.currentText()+' '+self.choose_year_combo.currentText()+' OVERALL PERFOMANCE AND GRADE BREAKDOWN'
descrip = ParagraphStyle('description',fontName='Courier-Bold',fontSize=18,leading=30, alignment=TA_CENTER)
head_text = Paragraph(heading,descrip)
pdf.addPageTemplates([land])
pdf2.addPageTemplates(PageTemplate(id='l',pagesize=[950,650],frames=[frame1]))
flow_obj=[]
flow_obj.append(head_text)
#creating the connection
conn = sqlite3.connect('mydb.db')
cur = conn.cursor()
str_summary = pd.read_sql("select * from '" + str(overall_summary) + "'", conn)
with open(str_summary) as f1:
    csvdata=csv.reader(f1,delimiter=",")
    tdata=[]
    for data in csvdata:
        rowdata=[]
        BLANK=data[0]
        A1 =data[1]
        A2=data[2]
        B3=data[3]
        B4=data[4]
        B5=data[5]
        C6=data[6]
        C7=data[7]

It throws this error

Traceback (most recent call last):
  File "D:\Python\PyQt5\Backup\Result Management System(RMS)\REMARE 2.py", line 2084, in get_results
    with open(str_summary) as f1:
TypeError: expected str, bytes or os.PathLike object, not DataFrame

Solution

  • You can't try to open a Pandas Dataframe or try to read it using csv.reader(). If you would like to dump your results to a CSV file, use str_summary.to_csv() (to_csv documentation). Otherwise, you can iterate over rows of the DataFrame using iterrows()

    An example is as follows:

    for index, row in str_summary.iterrows():
        BLANK = row['column_name']
        A1 = row['another_column_name']
        ...