Python Forum

Full Version: Length of rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

How can I determine number of a value occur in a column from an sql db?

con = pymysql.connect(host='localhost', user='root', password='*', database='prices')
            cur = con.cursor()
            cur.execute('select Var78 from prices ')
            rows = cur.fetchall()

            if rows != 0:
               self.totalrecord.set(len(rows))
               table.delete(*table.get_children())

            for row in rows:
                if row[78] == 'A':
                    table.insert("", END, values=row, tags='Albastru')
                    a = len(rows)
                    print(a)
It result:
7
7
7
7
where 7 is the total number of rows and it occur 4 times, but I need number 4 as result.
Thanks
That is obvious. "Rows" is not changed so it will count the the same number of rows every time.
If you want to count the rows with "A" in position 78, you should count them. For example:
rows_with_a = 0
for row in rows:
    if row[78] == 'A':
        table.insert("", END, values=row, tags='Albastru')
        rows_with_a += 1
print(rows_with_a)
Obviously you show us a small part of your program and we cannot see what the "table" object is.