Python Forum
Length of rows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Length of rows (/thread-38779.html)



Length of rows - Krissstian - Nov-23-2022

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


RE: Length of rows - ibreeden - Nov-25-2022

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.