python typeerror subscriptable_python - TypeError: 'NoneType' object is not subscriptable

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值