TypeError: 'NoneType' object is not subscriptable
31,014
If, due to whatever reason (empty result set?) curfetchone()
returns None
, a [0]
access is of course forbidden (as the error message clearly says).
So better do that in two steps and do
row = curfetchone()
if row is not None:
names = row[0]
# proceed
else:
# act appropriately
Author by
Admin
Updated on December 31, 2020Comments
-
Admin over 3 years
The error:
names = curfetchone()[0]
TypeError: 'NoneType' object is not subscriptable.
I tried checking the indentation but still there's an error. I read that maybe the variable names returns 'None' if there is no record of the filename in the database.
I use the same variable 'names' in other 'def' and it works fine. I'm sure it has to do with the 'None' value being returned.
global filename global t try: con = sqlite3.connect('textdb.db') cur = con.cursor() filename = tkinter.simpledialog.askstring("Open file...", "Input filename to open:") if filename != None: cur.execute("SELECT file_name FROM file_info WHERE file_name = ?", (filename,)) names = cur.fetchone()[0] same = str(names) if filename == same: cur.execute("SELECT file_content FROM file_info WHERE file_name = ?", (filename,)) content = cur.fetchone()[0] t = str(content) text.delete(0.0, END) text.insert(0.0, t) else: result = tkinter.messagebox.askyesno(title="File doesn't exist", message="'"+(filename)+"' doesn't exist. Make a new file using '"+(filename)+"'?") if result == True: cur.execute("SELECT COUNT(*) FROM file_info") count_row = cur.fetchone() cntdata = count_row[0] incr = (cntdata + 1) t = str(text.get(0.0, END)) curtime = str(ctime()) cur.execute("INSERT OR IGNORE INTO file_info VALUES(?, ?, ?, ?, ?)", (incr, filename, t, curtime, curtime,)) con.commit() except sqlite3.Error: if con: con.rollback() finally: if con: con.close()