Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite attribute execute is read-only

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 = '[email protected]'
        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

like image 500
sasker Avatar asked Nov 08 '11 17:11

sasker


2 Answers

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 ...
like image 111
rmmh Avatar answered Oct 23 '22 04:10

rmmh


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.

like image 25
jonathan.hepp Avatar answered Oct 23 '22 05:10

jonathan.hepp