python - sqlite 属性 execute 是只读的

标签 python exception sqlite

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

当我尝试插入数据库时​​。我得到以下 AttributeError

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

我似乎找不到关于此错误的任何信息。有谁知道这个异常是什么意思?

我正在使用带有 virtualenv 的 python 2.7。

以下是我尝试执行的代码,假设日期是一个字符串。

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

谢谢

最佳答案

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

应该是

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

不是

    cur.execute = ('insert ...

关于python - sqlite 属性 execute 是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8054582/

相关文章:

python - 如何模拟 `s3fs.FileSystem().glob` ?

exception - BlackBerry - 从位图创建图形对象时出现异常

c++ - 在捕获到异常时将有用的数据转储到控制台

python - 使用启用了 WAL 的 sqlite3 数据库-Python

entity-framework - 数据库首先使用system.data.sqlite 1.0.93创建 Entity Framework 6.1.1模型

join - 在sql条件下使用表而不将表的内容合并到结果中

python - 获取带有 tkinter.filedialog.asksaveasfilename 的文件名以附加到其中

python - Django Rest框架全局分页参数不适用于ModelViewSet

Python初学者: Extract a specific each row from CSV file and write it to different CSV files

c# - 为什么内置异常消息往往没有具体细节? (例如字典中的键)