In my python application I run a query which returns me a product from my product table. When I get the result and assign it in a variable, it is stored in a list. But I want to create a product class and want to store query result in an object of my product class which matches with my database table.
But I dont want to do it in this way which is one by one getting values from list and setting object variables and so on.
I want to do this because my database table is large and list does not store variable's field names. It comes like following; so I cant use something like this; getProductName()
[5,"strawberry",011023234,10.5]
so I should remember in which position my product barcode code is stored when it is needed.
Retrieve data from database;
vt = sqlite3.connect("vt.sqlite")
im = vt.cursor()
im.execute("SELECT * FROM Products WHERE barcode='{}'".format(barcode))
veriler = im.fetchall()
Here is veriler = [5,"starberry",001].
My database table will be large. Then my veriler will be a long list.
When I want to get some attribute of retrieved data I have to access it like veriler[0], veriler[25] etc. I should remember indexes for each attribute.
Is it possible if I had product class which has all attributes in my database table and create a product object and when I assigned the database result to my product object I have set all attributes in product object and can access attributes of my product object using get methods.
Thanks in advance.
The pscycopg2 NamedTupleCursor provides objects for database results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With