Why this sqlite python 3x code is not compatible with python 27 -


i´m new on python , developing app python 2.7, , got nice code python 3.x doesn´t work in version. i´ve been trying adapt it´s beauty couldn´t see issue causing compatibility problem. message is: attributeerror: database instance has no attribute '_db' in line 9.please me solve issue.

#!/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 {} (t1, i1) values (?, ?)'.format(self._table), (row['t1'], row['i1']))         self._db.commit()      def retrieve(self, key):         cursor = self._db.execute('select * {} t1 = ?'.format(self._table), (key,))         return dict(cursor.fetchone())      def update(self, row):         self._db.execute(             'update {} set i1 = ? t1 = ?'.format(self._table),             (row['i1'], row['t1']))         self._db.commit()      def delete(self, key):         self._db.execute('delete {} t1 = ?'.format(self._table), (key,))         self._db.commit()      def disp_rows(self):         cursor = self._db.execute('select * {} order t1'.format(self._table))         row in cursor:             print('  {}: {}'.format(row['t1'], row['i1']))      def __iter__(self):         cursor = self._db.execute('select * {} order t1'.format(self._table))         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))     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))     row in db: print(row)      print('delete rows')     db.delete('one')     db.delete('three')     row in db: print(row)  if __name__ == "__main__": main() 

on python 2.x need declare class new-style class inheriting object, because uses properties. properties supported new-style classes.

class database(object): 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -