H
H
howuu2019-03-22 04:14:41
Python
howuu, 2019-03-22 04:14:41

How to create and populate a dict object with sqlite3 keys?

Now I have a list and a database that looks like this:
5c94353a1e8d4754009577.jpeg5c943541ecab3267273239.jpeg
It has 3 tables category, product, media
And I create a list like this:

def select_product_all(self):
        with self.connection:
            products = []
            for product in self.cursor.execute("SELECT  * FROM products_product,  products_product_media WHERE products_product.id = products_product_media.product_id").fetchall():
                products.append(product)
        self.connection.close()
        return products

Now, for example, to display the line
For an ordinary cactus, the image corresponds to media / 1.jpg

I need to call it like this
print ("Для",  products[0][1] , " соответствует картинка" ,  products[0][6])

Here, I want to make a dictionary and substitute all these values ​​​​in there, so that I could call it like this
print("Для",  products.name , " соответствует картинка" ,  products.media])

How can I redo the code so that all keys like price, category_id, etc. completed in the dictionary?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2019-03-22
@howuu

I feel that you have reached the point where you need an ORM, tk. dict is exactly what you don't need here. I think it's worth looking at SqlAlchemy as the most common one. Do not pay attention if the examples and articles are about, for example, mysql. Most likely your code will differ only in the database connection string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question