title: How to extend sqlite3 connection object with own functions?
body: <p>I have a project written in Python 2.7 where the main program needs frequent access to a sqlite3 db for writing logs, measurement results, getting settings,...</p>
  
  <p>At the moment I have a db module with functions such as add_log(), get_setting(), and each function in there basically looks like:</p>
  
  <pre><code>def add_log(logtext):
      try:
          db = sqlite3.connect(database_location)
      except sqlite3.DatabaseError as e:
          db.close()  # try to gracefully close the db
          return("ERROR (ADD_LOG): While opening db: {}".format(e))
      try:
          with db:  # using context manager to automatically commit or roll back changes.
              # when using the context manager, the execute function of the db should be used instead of the cursor
              db.execute("insert into logs(level, source, log) values (?, ?, ?)", (level, source, logtext))
      except sqlite3.DatabaseError as e:
          return("ERROR (ADD_LOG): While adding log to db: {}".format(e))
      return "OK"
  </code></pre>
  
  <p>(some additional code and comments removed). </p>
  
  <p>It seems I should write a class extends the base sqlite connection object function so that the connection is created only once (at the beginning of the main program), and then this object contains the functionality such as </p>
  
  <pre><code>class Db(sqlite3.Connection):
      def __init__(self, db_location = database_location):
          try:
              self = sqlite3.connect(db_location)
              return self
          except sqlite3.DatabaseError as e:
              self.close()  # try to gracefully close the db
  
      def add_log(self, logtext):
          self.execute("insert into logs(level, source, log) values (?, ?, ?)", (level, source, logtext))
  </code></pre>
  
  <p>It seems this <em>should</em> be fairly straightforward but, I can't seem to get it working.</p>
  
  <p>It seems there is some useful advise here:
  <a href="https://stackoverflow.com/questions/23683886/python-how-to-successfully-inherit-sqlite3-cursor-and-add-my-customized-method">Python: How to successfully inherit Sqlite3.Cursor and add my customized method</a> but I can't seem to understand how to use a similar construct for my purpose.</p>