Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to only get second row data from Sqlite?

I am new to Python and I recently stuck in getting value from Sqlite. What I want is getting value only in second row of the table. This is the table data picture. And I have tried this but not working:

        con = sqlite3.connect(database=r'ims.db')
        cur = con.cursor()
        for l in range(1,8):
            cur.execute(f"Select occolor{l} from ordercart limit 2")
            row = cur.fetchall()
            print(row)

This will bring both first and second row value. But what I want are only the second row value. Anyone help with this pls?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

fetchall returns a reference to a list. Your query will return at most 2 rows. Therefore:

if (row := cur.fetchall()):
  print(row[-1])
else:
  print('Not found')

Doing it like this allows for the results being either empty or containing just one row

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading