W
W
willdes2018-08-17 16:17:47
Python
willdes, 2018-08-17 16:17:47

Why is SQL query not executed repeatedly in while?

Wrote a script that takes values ​​from the database and assigns them to the state of the gpio rasbperry pi. But I ran into a problem that the request is not re-executed with each passage of the cycle
.

while True:
  print("OUT1")
  cursors = db.cursor()
  w = cursors.execute("SELECT numb FROM gpio WHERE type='out' and value=1")
  cursors.execute("SELECT numb FROM gpio WHERE type='out' and value=1")
  dataa =  cursors.fetchall()
  for d in range(w):
    rowc = dataa[d]
    idlegs = rowc[0]
    GPIO.output(idlegs, 1)
    print("set OUT", idlegs, "= 1")
  print("OUT0")
  time.sleep(1)	
  cursora = db.cursor()
  j = cursora.execute("SELECT numb FROM gpio WHERE type='out' and value=0")
  cursora.execute("SELECT numb FROM gpio WHERE type='out' and value=0")
  datal =  cursora.fetchall()
  print(j)
  print(datal)
  for l in range(j):
    rowj = datal[l]
    idlegj = rowj[0]
    GPIO.output(idlegj, 0)
    print("set OUT", idlegj, "= 0")
  time.sleep(2)
  print("wait")

Console output:
wait
OUT1
set OUT 3 = 1
set OUT 5 = 1
set OUT 11 = 1
set OUT 12 = 1
set OUT 13 = 1
set OUT 15 = 1
set OUT 16 = 1
set OUT 18 = 1
set OUT 19 = 1
set OUT 21 = 1
set OUT 22 = 1
set OUT 23 = 1
OUT0
0
()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
CrazyElf, 2018-08-17
@willdes

And if you comment out everything in the first half of the cycle, then the second half is executed?
I have a suspicion that you are changing, but the changes are not committed and are visible only in the client where you are changing.
So you don’t understand why the selects are repeated and the cycles are strangely organized, probably not copied somewhere.
But this should not affect the logic of the loop execution.

A
alexhott, 2018-08-17
@alexhott

try deleting objects after processing

W
willdes, 2018-08-17
@willdes

Now this conclusion

OUT1
set OUT 3 = 1
set OUT 5 = 1
set OUT 11 = 1
set OUT 12 = 1
set OUT 13 = 1
set OUT 15 = 1
set OUT 16 = 1
set OUT 18 = 1
set OUT 19 = 1
set OUT 21 = 1
set OUT 22 = 1
set OUT 23 = 1
OUT0
wait

script fixed
while True:
  print("OUT1")
  cursor = db.cursor()
  w = cursor.execute("SELECT numb FROM gpio WHERE type='out' and value=1")
  cursor.execute("SELECT numb FROM gpio WHERE type='out' and value=1")
  data =  cursor.fetchall()
  for d in range(w):
    row = data[d]
    idleg = row[0]
    GPIO.output(idleg, 1)
    print("set OUT", idleg, "= 1")
    del row
    del idleg
  del data
  del cursor
  del w
  print("OUT0")
  cursor = db.cursor()
  w = cursor.execute("SELECT numb FROM gpio WHERE type='out' and value=0")
  cursor.execute("SELECT numb FROM gpio WHERE type='out' and value=0")
  data =  cursor.fetchall()
  for d in range(w):
    row = data[d]
    idleg = row[0]
    GPIO.output(idleg, 0)
    print("set OUT", idleg, "= 0")
    del row
    del idleg
  del data
  del cursor
  del w
  time.sleep(2)
  print("wait")

in the database set the field value value=0 to 3 and 15 numb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question