Issue
Through a cursor.fetchall()[0]
i get a row from the database in which it is sought if the last trend on the stock exchange (already entered in the database) is positive or negative. Through OR
one is taken only one: or only negative
or only positive
, depending on what is the last one (so the first in temporal order). Positive or negative only selected via a combobox. So far everything is ok.
cursor.execute("SELECT american_company, asian_company FROM financial_values WHERE positive =? OR negative=? LIMIT 1", [combo_value_positive, combo_value_negative])
Last_value = cursor.fetchall()[0]
With a condition (if
), I would like to make sure that if the last value is positive, then something is printed. On the contrary if the last value is negative, then another is printed. So I would like to seek the name of a company based on positive or negative within Last Value = Cursor.fetchall()[0]
. Positive or negative are searched precisely by Last_Value.
if Last_value == positive: #HERE, i want to use positive that of the database of Last value
print("value positive")
else: #negative
print("value negative")
I wrote ==
but obviously it's not good. I get the error, because there is no item defined with this name, but I would like to use the database directly of Last_value = cursor.fetchall()[0]
NameError: name 'positive' is not defined
How can i?
UPDATE
As you can see in Last_value I used OR, so positive or negative. Only the latest (I mean the most recent) american_company and asian_company are taken, based on whether the latter is positive or negative. With if Last_value, I wanted to be able to choose "if the last american_company and asian_company were collected thanks to positive or thanks to negative (only one of negative or positive)
Solution
Python can only use the data you load from the database. If you want to use the value of the positive and negative columns, you have to add them to your select query.
cursor.execute("""
SELECT american_company, asian_company, positive, negative
FROM financial_values
WHERE positive =? OR negative=?
LIMIT 1
""", [combo_value_positive, combo_value_negative])
# Use fetchone() to get just one row.
row = cursor.fetchone()
fetchone
returns a tuple of the results. Positive and negative are the 3rd and 4th items in the select list. Tuples start at 0 so row[2]
is the value for positive
and row[3]
is the value for negative
.
if row[2]
print("its positive")
if row[3]
print("it's negative")
Answered By - Schwern
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.