sqlite属性execute是只读的 [英] sqlite attribute execute is read-only

查看:247
本文介绍了sqlite属性execute是只读的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlite创建并连接到sqlite db foo.db

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

当我尝试在数据库中进行插入时。我得到以下AttributeError

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?

我正在将python 2.7与virtualenv配合使用。

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()

谢谢

推荐答案

您正在尝试修改游标的属性。您要调用游标的方法。

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

应该是

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

不是

    cur.execute = ('insert ...

这篇关于sqlite属性execute是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆