I have this definition in my python script
def read_from_db():
c.execute("SELECT title FROM book WHERE title LIKE 'Huckle%' ")
for row in c.fetchall():
print row
title = row
I wanted to save the value of the title into another variable. but it seems that it doesn't work like that?
I think you might just want to do title = row[0]. Row contains all column values of the particular row in the result set. Even if you select a single column you still need to index to get that value.
Related
import gspread
list_of_dicts = worksheet.get_all_records()
for d in list_of_dicts:
d['Name'] == "Dash"
In excel sheet, I have Name, Age, address. I want to be able to fetch the whole row for a particular value. The only way I could figure from documentation is this. But I do not know how to fetch the row number
values_list = worksheet.row_values(1)
How do I find the index?
From the documentation it looks like you can query for a string and you get back the cell of the value. Then you can get the row of the cell and fetch the whole row.
So for your example you can query for the name you want
cell = worksheet.find("Dash")
And then ask for the whole row where the name is Dash
values_list = worksheet.row_values(cell.row)
I have a table called draws. All I want to do is get the column 'user_id' as an array, in order from the 'id' column (top to bottom).
The ID column just numbers each row.
I've written this:
user_id = Draw.query.order_by(desc('id')).all()
This query gets every column though right? I just want the information in the 'user_id' column
user_id = Draw.query.with_entities(Draw.user_id).order_by(desc(Draw.id)).all()
With entities explanation from the docs.
Alternatively you can just do a list comprehension on the query you already have -- something like:
user_ids = [d.user_id for d in user_id]
Try this!
from sqlalchemy import desc
db.session.query(Draw.user_id).order_by(desc(Draw.id)).all()
I have multiple
I want to get rows of Name.
I know how to get by index using dataframe but I want to get using row name as index might change.
like
(row=="Name" ) or (row== "name")
output be like :
Thanks in advance!
If you want the name column name = df['Name']
I'm starting to learn python and I'm trying to do an exercise where I have to save in a "rows" variable some stock data coming from a SQL query, like this:
rows = db.execute("SELECT * FROM user_quote WHERE user_id=:userid", userid=session["user_id"])
This will return 4 columns (id, user_id, symbol, name)
Then, for every row the query returns I'll get the last known price of that stock from an API, and I want to add that information to another column in my rows variable. Is there a way to do this? Should I use another approach?
Thanks for your time!
I'm not sure what type the rows variable is, but you can just add an additional column in the SELECT:
rows = db.execute("SELECT *, 0 NewCol FROM user_quote WHERE user_id=:userid", userid=session["user_id"])
Assuming rows is mutable, this will provide a placeholder for the new value.
Convert the rows tuple to a list, then you can use append() to add the price.
rows = list(rows)
rows.append(price)
I have searched the docs and SO and could not find anything to resolve my issue. I am trying to call a select from my sqlite database and add it to a dictionary with the columns as keys. When I do this it returns a row for each column/key. It is has 14 columns and if I only have 4 rows it repeats for each one. This was the first attempt
columns = [desc[0] for desc in cursor.description]
results = []
for row in r:
Summary = {}
items = zip(columns, row)
for (items, values) in items:
Summary[items] = row
results.append(Summary)
Then I also tried the row_factory as given in the docs. That didn't work. My end goal is to be able to print out to a text file verticly by using
for x in results:
print x[name]
print x[email]
etc
Any help is appreciated
You are creating your dictionary incorrectly. Use:
for row in r:
summary = dict(zip(columns, row))
results.append(summary)
instead.
Your code sets the whole row sequence as the value for each key in Summary, instead of the individual column value, then appending that same dictionary to the results list for each column key..