0

I am getting an bug in my code. This is caused by

From the ide

DEBUG:cs50:SELECT id FROM stock_info where id = 108
length of rows {} 1 
DEBUG:cs50:SELECT * FROM stock_info where id = 108
length of columns {} 1



stock_info 

id Symbol      Name          Shares        Price          TOTAL
1  AAPL        Apple          2            $185.22        $370.44
1  GOOG        Google         3            235.00         $705       
2  AAPL         Apple         1            185.22         $185.22

I want to select columns

2     AAPL         Apple         2                 185.22        $185.22

IOW rows len(5) How do I do this

I tried googling first.

Now

1  AAPL          Apple         2             $185.22        $370.44
1  GOOG          Google        3             $235.00           $705 

It is pretty easy to select 1 col but how do I select 2 columns where the id is = the same number?

Bought.html

{% extends "layout.html" %}

{% block title %}
    bought
{% endblock title %}

{% block main %}





<table>
  <tr>
    <th> SYMBOL </th>
    <th> NAME </th>
    <th> SHARES </th>
    <th> PRICE </th>
    <th> TOTAL </th>
  </tr>

{% for y in range(db_stock_info_num_columns_len) %}
  {% for x in range(db_stock_info_num_rows_len) %}


  <tr> {{[y]}}
    <th> {{database_stock_info[x][y]}} </th>
  </tr>
{% endfor %}
  {% endfor %}

  </tr>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> {{ cash_after_purchase }} </th>
  <tr>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> {{ current_cash[0]["cash"] }} </th>
  </tr>

</table>

{% endblock %}<!–– https://www.geeksforgeeks.org/python-using-for-loop-in-flask/  -->



{% extends "layout.html" %}

{% block title %}
    bought
{% endblock title %}

{% block main %}





<table>
  <tr>
    <th> SYMBOL </th>
    <th> NAME </th>
    <th> SHARES </th>
    <th> PRICE </th>
    <th> TOTAL </th>
  </tr>

{% for y in range(db_stock_info_num_columns_len) %}
  {% for x in range(db_stock_info_num_rows_len) %}


  <tr> {{[y]}}
    <th> {{database_stock_info[x][y]}} </th>
  </tr>
{% endfor %}
  {% endfor %}

  </tr>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> </th>
    <th> {{ cash_after_purchase }} </th>
  <tr>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> </th>
      <th> {{ current_cash[0]["cash"] }} </th>
  </tr>

</table>

{% endblock %}

Output from bought.html

SYMBOL  NAME    SHARES  PRICE   TOTAL

                                9444.34

                                9814.78

Thanks for the help.

1
{% for y in range(db_stock_info_num_columns_len) %}
  {% for x in range(db_stock_info_num_rows_len) %}


  <tr> {{[y]}}
    <th> {{database_stock_info[x][y]}} </th>
  </tr>
{% endfor %}
  {% endfor %}

looks strange. You want <tr> </tr> once per row, not per element, so it should look more like

{% for x in range(db_stock_info_num_rows_len) %}
<tr>
  {% for y in range(db_stock_info_num_columns_len) %}
    <td> {{database_stock_info[x][y]}} </td>
  {% endfor %}
</tr>
{% endfor %}

Also, you seem to have non-matching <tr>/</tr> below that loop.

BTW, I'm pretty confused, are your money-related fields in the table not of some number type?

edit: From your comment, I somewhat guess you might have some list of dicts or equivalent, and would be searching for something like this instead:

{% for row in database_stock_info %}
<tr>
  {% for key, value in row.items() %}
    <td> {{ value }} </td>
  {% endfor %}
</tr>
{% endfor %}

I would not use an inner loop unless you want all the fields be handled the same.

  • ya I probably need to fix the cash output. How do I select all 5 columns SYMBOL NAME SHARES PRICE TOTAL? I could try SELECT * FROM stock_info where id = session["user_id"]. But only get a length 1. I need a length of 5. How do I get a length of 5 by using the len function? @Blauelf – grggthwrth Jun 9 '19 at 1:08
  • Do you have 5 records in that table for this user id? From your code, I assumed you had a 2D list. Do you mean you have a list of dicts (or equivalent) instead? – Blauelf Jun 9 '19 at 15:39
  • I realize I do not need id when trying to get length. But this is just shorter to type here. well when I test select * from stock_info where id = 100 inside php liteadmn otherwise known as finance.db I get id stock_name stock_symbol shares stock_price stock_total 100 Apple Inc. AAPL 1 185.22 185.22 But when I go len(rows) rows being the db.execute select statement I get 1. I want to get all six columns. I realize I only need 5 but as stated it is shorter to type here. How do I get 6 = len(rows) using the database select statement? @Blauelf – grggthwrth Jun 10 '19 at 20:13
  • 1
    rows in your case is likely the value returned by a db.execute("SELECT .... That one is a kind of list (you can apply len to it to get the number of records returned). Each element would be a kind of dict, containing field name/value pairs. You can access those by name, or use methods like .items() to convert the dict to a list of key-value pairs. – Blauelf Jun 11 '19 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .