TypeError: NoneType object is not subscriptable [duplicate]

2024/11/16 19:48:38

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()
Answer

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
https://en.xdnf.cn/q/71634.html

Related Q&A

Where can I find numpy.where() source code? [duplicate]

This question already has answers here:How do I use numpy.where()? What should I pass, and what does the result mean? [closed](2 answers)Closed 4 years ago.I have already found the source for the num…

NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python

I am trying to connect to the Mountain Lion notification center via python. Ive installed pyobjc and am following the instructions here and here. Also see: Working with Mountain Lions Notification Cent…

Flask app hangs while processing the request

I have a simple flask app, single page, upload html and then do some processing on it on the POST; at POST request; i am using beautifulsoup, pandas and usually it takes 5-10 sec to complete the task. …

How do I make a server listen on multiple ports

I would like to listen on 100 different TCP port with the same server. Heres what Im currently doing:-import socket import selectdef main():server_socket = socket.socket(socket.AF_INET, socket.SOCK_STR…

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

Im developing a REST API which takes POST requests from some really brain-dead software which cant PATCH or anything else. The POSTs are to update Model objects which already exist in the database.Spec…

How can i login in instagram with python requests?

Hello i am trying to login instagram with python requests library but when i try, instagram turns me "bad requests". İs anyone know how can i solve this problem?i searched to find a solve f…

How to abstract away command code in custom django commands

Im writing custom django commands under my apps management/commands directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. How…

Python one-liner (converting perl to pyp)

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.perl -l -a -F, -p -eif ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }This takes i…

Getting symbols with Lark parsing

Im trying to parse a little pseudo-code Im writing and having some trouble getting values for symbols. It parses successfully, but it wont return a value the same as it would with "regular" …

Why cant I connect to my localhost django dev server?

Im creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000…