1

I get object is not subscriptable on a non empty query result. When i print c.fetchone() it prints the correct result but when i check the type it says <class 'NoneType'>

import sqlite3

conn = sqlite3.connect("scraping.db")
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS Terminate (numar INT, arj INT )')
conn.commit()
c.execute('DELETE FROM Terminate')
conn.commit()
c.execute('INSERT OR IGNORE INTO Terminate ( numar, arj ) VALUES (1,0)')
conn.commit()
c.execute("SELECT * FROM Terminate") # ORDER BY numar DESC LIMIT 1")
print(c.fetchone(), type(c.fetchone()))

ins = c.fetchone()[0]
6
  • There are no more rows available (your table clearly has only one row). You can store the first result if you intend to reuse that: result = c.fetchone() Commented Jul 22, 2018 at 18:34
  • if you want to play more with the same line, you have to keep a reference like res = c.fetchone() then print(res, type(res)) will work as you expect Commented Jul 22, 2018 at 18:37
  • i still can not retrieve the first element of that result = c.fetchone() Commented Jul 22, 2018 at 18:40
  • ins = result[0] TypeError: 'NoneType' object is not subscriptable Commented Jul 22, 2018 at 18:42
  • you can only call c.fetchone() once Commented Jul 22, 2018 at 18:45

1 Answer 1

1

I didn't see @PRMoureu comment. She/he is completely right. It happens because you already have fetched both your values in print. You need re-execute SELECT or remove your print:

import sqlite3

conn = sqlite3.connect("scraping.db")
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS Terminate (numar INT, arj INT )')
conn.commit()
c.execute('DELETE FROM Terminate')
conn.commit()
c.execute('INSERT OR IGNORE INTO Terminate ( numar, arj ) VALUES (1,0)')
conn.commit()
c.execute("SELECT * FROM Terminate") # ORDER BY numar DESC LIMIT 1")


ins = c.fetchone()[0]
print ins, type(ins)

The output seems ok

1 <type 'int'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.