H
H
howuu2018-12-01 13:37:03
Python
howuu, 2018-12-01 13:37:03

Loop for cursor.execute in sqlite?

There is a table of products and a table of images
Each product has an id cell, each image has an id_product cell to which it is attached
. A product may not have an image.
I'm trying

# Делаю цикл в котором достаем все из таблицы товаров 
for product in cursor.execute("SELECT rowid, * FROM products_product "):
  print("Название товара: ", product[2])
  print("id", product[0])
  print( "Ценна:" , product[3], "грн")
       #теперь ищем картинку 
  product_id = product[0]
  media = cursor.execute('SELECT * FROM products_product_media WHERE product_id=?', str(product_id))
  media = media.fetchone()
  print("Путь к картинке:" , media[1])

Everything is cool, but in the output only the first product and its picture, the cycle stops, I think maybe this is due to the fact that the course is moved or something like that, but I don’t know how to fix it, how to make the cycle work completely , because if you remove this piece
#теперь ищем картинку 
  product_id = product[0]
  media = cursor.execute('SELECT * FROM products_product_media WHERE product_id=?', str(product_id))
  media = media.fetchone()
  print("Путь к картинке:" , media[1])

Then the cycle gives everything as it should

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question