-2

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?

bāsil
  • 5
  • 3

1 Answers1

1

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

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • 1
    This is something to do in SQL and not in Python code. It does not seem to matter a lot for 2 rows, but imaging you want to get just the 10,000th row. – Klaus D. Mar 11 '22 at 08:28
  • @KlausD. Thank you for your guidance. I am well aware of the implications of trying to use this technique for the Nth row. However, for once, I did read the question and this answers it in a reasonable way – DarkKnight Mar 11 '22 at 08:36