python - 为什么这个 sqlite python 3x 代码与 python 27 不兼容

标签 python python-2.7 python-3.x

我是 python 新手,正在使用 python 2.7 开发一个应用程序,并得到了这个很好的 python 3.x 代码,但在我的版本中不起作用。我一直在尝试对其进行调整以使其美观,但没有发现导致兼容性问题的问题。我收到的消息是:AttributeError:数据库实例在第 9 行没有属性“_db”。请帮助我解决此问题。

#!/usr/bin/python3

import sqlite3

class database:
    def __init__(self, **kwargs):
        self.filename = kwargs.get('filename')
        self.table = kwargs.get('table', 'test')

    def sql_do(self, sql, *params):
        self._db.execute(sql, params)
        self._db.commit()

    def insert(self, row):
        self._db.execute('insert into {} (t1, i1) values (?, ?)'.format(self._table), (row['t1'], row['i1']))
        self._db.commit()

    def retrieve(self, key):
        cursor = self._db.execute('select * from {} where t1 = ?'.format(self._table), (key,))
        return dict(cursor.fetchone())

    def update(self, row):
        self._db.execute(
            'update {} set i1 = ? where t1 = ?'.format(self._table),
            (row['i1'], row['t1']))
        self._db.commit()

    def delete(self, key):
        self._db.execute('delete from {} where t1 = ?'.format(self._table), (key,))
        self._db.commit()

    def disp_rows(self):
        cursor = self._db.execute('select * from {} order by t1'.format(self._table))
        for row in cursor:
            print('  {}: {}'.format(row['t1'], row['i1']))

    def __iter__(self):
        cursor = self._db.execute('select * from {} order by t1'.format(self._table))
        for row in cursor:
            yield dict(row)

    @property
    def filename(self): return self._filename

    @filename.setter
    def filename(self, fn):
        self._filename = fn
        self._db = sqlite3.connect(fn)
        self._db.row_factory = sqlite3.Row

    @filename.deleter
    def filename(self): self.close()

    @property
    def table(self): return self._table
    @table.setter
    def table(self, t): self._table = t
    @table.deleter
    def table(self): self._table = 'test'

    def close(self):
            self._db.close()
            del self._filename

def main():
    db = database(filename = 'test.db', table = 'test')

    print('Create table test')
    #db.sql_do('drop table if exists test')
    db.sql_do('create table test ( t1 text, i1 int )')

    print('Create rows')
    db.insert(dict(t1 = 'one', i1 = 1))
    db.insert(dict(t1 = 'two', i1 = 2))
    db.insert(dict(t1 = 'three', i1 = 3))
    db.insert(dict(t1 = 'four', i1 = 4))
    for row in db: print(row)

    print('Retrieve rows')
    print(db.retrieve('one'), db.retrieve('two'))

    print('Update rows')
    db.update(dict(t1 = 'one', i1 = 101))
    db.update(dict(t1 = 'three', i1 = 103))
    for row in db: print(row)

    print('Delete rows')
    db.delete('one')
    db.delete('three')
    for row in db: print(row)

if __name__ == "__main__": main()

最佳答案

在Python 2.x上,您需要通过继承object来将该类声明为新式类,因为它使用属性。属性仅受新型类支持。

class database(object):

关于python - 为什么这个 sqlite python 3x 代码与 python 27 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962902/

相关文章:

python - 如何定位字幕?

python - 如何仅下载数据 Python 的前 x 个字节

python - 检查 dict 中的键返回 false 即使键存在

mysql - 使用 Flask 中的全选查询根据默认列列表排列 JSON 输出

python - Django Web 服务器在启动守护进程后挂起

python - 在 CSV 中嵌入 JSON 的正确格式

python - 将列表中的元素打印为连接字符串

python - 是否有第 3 方 Python 3 库的列表?

python - 如何在 Python 中 reshape networkx 图?

python - 在 Python 中使用 __new__