This question already has an answer here:
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.rsor = conn.cursor() cursor.execute ("SELECT * FROM pin WHERE active=1") while (1): row = cursor.fetchone() st = str(row[2]) pin = str(row[1]) order = str(st)+str(pin) if row == None: b
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()
python
object
nonetype
|
this question
edited Dec 31 '14 at 8:39
glglgl 54.4k 6 72 126 asked Dec 31 '14 at 8:33
Abby Cordovilla 1 1 1
marked as duplicate by glglglpython Users with the python badge can single-handedly close python questions as duplicates and reopen them as needed. Jan 1 '15 at 19:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
1 Answers
1
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
|
this answer answered Dec 31 '14 at 8:38
glglgl 54.4k 6 72 126 thank you so much!!!! your advice helped me a lot! thank you again! :D –
Abby Cordovilla Dec 31 '14 at 8:49 1 @AbbyCordovilla Accepting an answer is much more thanks than using four
! signs :-P –
glglgl Dec 31 '14 at 9:33 @AbbyCordovilla (Accepting works by pressing the outlined check mark on the left of the question.) –
glglgl Jan 1 '15 at 19:31
|
Not the answer you're looking for? Browse other questions tagged python object nonetype or ask your own question.
ran into the error "TypeError: 'NoneType' object is not subscriptable. I have never before seen this error, so I have no idea what it means. import mathprint("The format you should consider:")print str("value 1a")+str(" + ")+str("value 2