Официальный сайт студ.городка НГТУ
Программирование и БД » [python] TypeError: 'NoneType' object is unsubscriptable 

#1  08.04.10 15:12

[python] TypeError: 'NoneType' object is unsubscriptable

простой скрипт

Код: python:

import MySQLdb 
import time
 
db = MySQLdb.Connect(host="localhost", user="username", passwd="password", db="dbname")
cursor = db.cursor()
 
row=cursor.execute("SELECT * from online;") 
 
rw=cursor.fetchone()
 
now=int(time.mktime(time.localtime()));
 
 
for i in range(row):
 
	if( now-rw[2] > 300 ): 
		sql="UPDATE online SET idle=1 WHERE ident='%s'" % (rw[1],)
		cursor.execute(sql);
		db.commit();
 
	rw=cursor.fetchone()
 
db.close();
 


выдаёт

Traceback (most recent call last):
  File "online.py", line 17, in <module>
    if( now-rw[2] > 300 ):
TypeError: 'NoneType' object is unsubscriptable

если убрать в запросе кавычки ...WHERE ident=%s выдаёт ошибку

Traceback (most recent call last):
  File "online.py", line 19, in <module>
    cursor.execute(sql);
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'FuckU' in 'where clause'")

как правильно злоипучие кавычки поставить ?

Исправлено iDrum (08.04.10 15:13)

Offline

#2  08.04.10 15:22

Re: [python] TypeError: 'NoneType' object is unsubscriptable

если сделать так что питон сам экранировал переменную

Код: python:

cursor.execute("""UPDATE online SET idle=1 WHERE ident=%s""", (rw[1], ))

та же самая ошибка

Offline

#3  08.04.10 15:23

Re: [python] TypeError: 'NoneType' object is unsubscriptable

""" это же комментарий, не?

Offline

#4  08.04.10 15:33

Re: [python] TypeError: 'NoneType' object is unsubscriptable

если вот так вот

Код: python:

cursor.execute("""UPDATE online SET idle=1 WHERE ident='%s'""", (rw[1], ))


то ошибка

Traceback (most recent call last):
  File "online.py", line 19, in <module>
    cursor.execute("""UPDATE online SET idle=1 WHERE ident='%s'""", (rw[1], ))
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FuckU''' at line 1")

Offline

#5  08.04.10 16:11

Re: [python] TypeError: 'NoneType' object is unsubscriptable

всё правильно сделал

Код: pythhon:

import MySQLdb, time
 
db = MySQLdb.Connect(host="localhost", user="username", passwd="password", db="dbname")
cursor = db.cursor()
cursor.execute("SET NAMES 'cp1251'")
cursor.execute("SELECT * from online;")
 
now=int(time.mktime(time.localtime()));
 
for rw in cursor.fetchall():
	if( (now-rw[2]) > 300 ):
		cursor.execute("UPDATE online SET idle=1 WHERE ident = %s", (rw[1], ))
 
db.close();


так и не осилил чо было выше. и там и тут везде одинаковый тупл

Offline

#6  08.04.10 16:12

Re: [python] TypeError: 'NoneType' object is unsubscriptable

такой вариант лучше:

Код: python:

cursor.execute ( "UPDATE online SET idle=1 WHERE ident=%s", rw[1] )

а ошибка, вот же она:

1054, "Unknown column 'FuckU'

Исправлено LLlypka (08.04.10 16:12)

Offline

#7  08.04.10 16:14

Re: [python] TypeError: 'NoneType' object is unsubscriptable

LLlypka написал(а):

а ошибка, вот же она:

это ошибка бывает когда в where имя_поля=имя_переменной, а не имя_поля='имя_переменной', переменая строковая

Исправлено iDrum (08.04.10 16:14)

Offline

Программирование и БД » [python] TypeError: 'NoneType' object is unsubscriptable 

ФутЕр:)

© Hostel Web Group, 2002-2025.   Сообщить об ошибке

Сгенерировано за 0.050 сек.
Выполнено 14 запросов.