sqlite attribute execute is read-only

时间秒杀一切 提交于 2020-02-15 08:32:05

问题


I am using sqlite to create and connect to a sqlite db foo.db

When I try to do an insert into the DB. I get the following AttributeError

AttributeError: 'sqlite3.Cursor' object attribute 'execute' is read-only

I can't seem to find any information on this error. Does anyone have any idea what this exception means?

I am using python 2.7 with virtualenv.

The following is the code I am trying to execute assume date is a string.

        username = 'user'
        pwdhash = some_hash_function()
        email = 'user@foo.com'
        date = '11/07/2011'

        g.db = sqlite3.connect('foo.db')
        cur = g.db.cursor()            
        cur.execute = ('insert into user_reg (username,pwdhash,email,initial_date)\
                        values (?,?,?,?)',
                        [username,
                         pwdhash,
                         email,
                         date])
        g.db.commit()
        g.db.close()

Thanks


回答1:


You're trying to modify an attribute of the cursor. You want to call a method of the cursor.

It should be

    cur.execute('insert into user_reg (username,pwdhash,email,initial_date)\
                    values (?,?,?,?)',
                    [username,
                     pwdhash,
                     email,
                     date])

Not

    cur.execute = ('insert ...



回答2:


Seems to be a simple syntax error. You are trying to set a value to the command execute while you have just to call it: remove the '=' and it should be fine.



来源:https://stackoverflow.com/questions/8054582/sqlite-attribute-execute-is-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!