0

I want to have a better solution than mine, for getting the first row of an SQLite database using Python.

import sqlite3

con = sqlite3.connect('emails.db')
cur = con.cursor()

cur.execute('SELECT * FROM my_table LIMIT 1')
print(cur.fetchall())

con.close()

outputs the correct row, but I think SELECT * iterates through my entire table, which is inefficient. I tried COUNT(*) but couldn't succeed.

azro
  • 53,056
  • 7
  • 34
  • 70
maus9
  • 1
  • You should learn a bit more about SQL if you think `count(*)` could have helped you – azro Mar 27 '22 at 14:24
  • Use `fetchone` and remove LIMIT – azro Mar 27 '22 at 14:24
  • Just to note... `SELECT` doesn't refer to rows - it refers to columns... so your approach is fine as the `LIMIT 1` will just take the first row and not bother looking any further. – Jon Clements Mar 27 '22 at 14:24

0 Answers0