I have a database(mysql) table named foo, and its primary key is bigint type, and auto incre开发者_运维技巧ment, I write the following codes to insert a record and get the record's primary key
q = db.insert('foo', name='bob', age=2, _test=True)
print q
this record has been insert into the table, but I got the following error:
type 'exceptions.TypeError' : 'long' object is unsubscriptable
Can you help me what should I do ?
You get the TypeError: '<type>' object is not subscriptable
error when you try to use the __getitem__
interface to an object that doesn't support it. That normally looks like this container[...]
.
Take a look at the full traceback. Somewhere, you should see an object being accessed that way. Since you haven't shown the full traceback or the rest of your code, I can't tell you where.
I suspect the problem is in the print q It is not mentionned in the doc of web.py
>>> db = DB(None, {})
>>> q = db.insert('foo', name='bob', age=2, created=SQLLiteral('NOW()'), _test=True)
>>> q
<sql: "INSERT INTO foo (age, name, created) VALUES (2, 'bob', NOW())">
>>> q.query()
'INSERT INTO foo (age, name, created) VALUES (%s, %s, NOW())'
>>> q.values()
[2, 'bob']
精彩评论