Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cursors and loops
#1
Hello all

Very new Python user here, so be gentle.

If there is a better way to do what I am trying to do (self evident from the code), please let me know.

My problem is that the program starts to go through the loop once. If I uncomment out the mycursor.close, it fails on the mycursor.close line. If I leave it commented then it fails on the second iteration of the loopo at the assignment of mycursor = mydb.cursor().

for dfct in defects:
	if dfct == 0:
		break
	sql = "SELECT * FROM table_def_int_data WHERE defect_id='" + str(dfct) + "'"	
	mycursor = mydb.cursor()
	mycursor.execute(sql)
	myresult = mycursor.fetchone()
	print(myresult)
#	mycursor.close()
Please let me know what I am doing incorrect here. (Probably many things ha ha)

TIA
Reply
#2
Never mind. Trial and error got me there eventually. :)

Still, if anyone has any useful tips about my coding that would be nice.

Here is the working version:
mycursor = mydb.cursor()
for dfct in defects:
	if dfct == 0:
		break
	# first get the data_id of the latest revision
	sql = "SELECT * FROM table_def_int_data WHERE defect_id='" + str(dfct) + "' AND revision = (SELECT MAX(revision) from table_def_int_data where defect_id='" + str(dfct) + "')"
	mycursor.execute(sql)
	myresult = mycursor.fetchone()
	sql = "SELECT * FROM table_def_int_data WHERE defect_id='" + str(dfct) + "' AND data_id = " + str(myresult[0])
	mycursor.execute(sql)
	myresult = mycursor.fetchone()
	print(myresult)
mycursor.close()
Reply


Forum Jump:

User Panel Messages

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