Home > front end >  show only one data in python query
show only one data in python query

Time:06-30

I write to see how I can get only one data to show by a print when I make a query in python, when I do the query it should only give me a number but I cannot show or access it.

def run_query(self, query, parameters = ()):
     with sqlite3.connect(self.db_name) as conn:
        cursor = conn.cursor()
        result = cursor.execute(query, parameters)
        conn.commit()
     return result
    
def get_horarios(self):
    
    query = 'SELECT hora FROM horarios where horario=1'

    db_rows = self.run_query(query)
    
    print(db_rows)

enter image description here

CodePudding user response:

To show the first row in the results of the query:

print(db_rows.fetchone())

To show all of the results of the query:

print(db_rows.fetchall())

or

for row in db_rows.fetchall():
    print(row)

CodePudding user response:

The query always return a list. To access the first item, you can do:

print(db_rows[0])
  • Related