I have a SQLite database where the table userProducts currently has 2 rows in it, 2|1 and 2|2. My expected output for my SQL query is 1 and 2, however im only getting 1. When i run the exact query in my database it returns exactly what i expect, however in my flask function it doesnt. Ive recently had to re-do my database handling due to switching libraries so im very new to this way of getting data. Its not the cleanest code since im actively debugging it
@app.route("/", methods=["GET", "POST"])
@login_required
def index():
# Get all productID's from current user
userProductsRaw = db.execute("SELECT productID FROM userProducts WHERE userID = ?",
str(session["user_id"]))
userProducts = userProductsRaw.fetchall()
if len(userProducts) > 0:
userProducts = userProducts[0]
products= []
# For every product in Users list update the values and send them to front-end
if len(userProducts) > 0:
for userProduct in userProducts:
productID = str(userProduct)
# Create list of updated products
print("test: " + productID)
productRaw = db.execute("SELECT * FROM products WHERE id = ?", (productID,))
product = productRaw.fetchone()
products.append(product)
print(products)
# Send updated products to index.html
return render_template("index.html", products=products)
I believe your problem is because of the following snippet of code
if len(userProducts) > 0:
userProducts = userProducts[0]
You could remove that and try.
If there’s a specific reason for that piece of code and you’re still not able to figure it out, you could try adding ORM to your application which will make you CRUD easier.
Here are a few reference for you
https://pypi.org/project/ORM-SQLite/
https://docs.sqlalchemy.org/en/14/orm/tutorial.html