Python Forum
Problem in formulating append logic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in formulating append logic
#2
(Jun-06-2022, 02:29 PM)shantanu97 Wrote:
         cursor.execute("SELECT  * FROM  [dbo].[Centroids]")
         while 1:      # Endless loop; same as while True:.
             row=cursor.fetchone()
             if not row:
                 break
             project = row[0]
             Latitude = row[3]
             Longitude = row[2]
         cursor.close()    # Now the loop is finished. Project, Latitude, Longitude contain the values of the last row.
         connection.close()
         return (project,Latitude,Longitude,)

There are several ways to solve this.
  • Use fetchall() instead of fetchone(). In this case the "while 1" is not needed at all.
  • Use "yield" instead of "return". In this case the function becomes an generator.
  • Use a list and append the results in the loop to this list.
In all cases you have to change the place where sql() is called to handle the results as an iterator.
Reply


Messages In This Thread
Problem in formulating append logic - by shantanu97 - Jun-06-2022, 02:29 PM
RE: Problem in formulating append logic - by ibreeden - Jun-07-2022, 08:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,641 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Problem with append list in loop michaelko03 0 1,707 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  Problem with and if() logic nikos 2 2,048 Feb-11-2019, 10:14 PM
Last Post: nikos
  Number logic problem m1xzg 13 10,767 Oct-23-2016, 09:36 PM
Last Post: m1xzg
  PyPDF2, merge.append(...) problem peterkl 1 8,106 Oct-23-2016, 09:50 AM
Last Post: peterkl

Forum Jump:

User Panel Messages

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