Python class to manage a table in SQLite Module name:    database.py

Code:

import sqlite3

class Database(object):
    """sqlite3 database class that holds testers jobs"""
    DB_LOCATION = "/root/Documents/testerJobSearch/tester_db.sqlite"

    def __init__(self):
        """Initialize db class variables"""
        self.connection = sqlite3.connect(Database.DB_LOCATION)
        self.cur = self.connection.cursor()

    def close(self):
        """close sqlite3 connection"""
        self.connection.close()

    def execute(self, new_data):
        """execute a row of data to current cursor"""
        self.cur.execute(new_data)

    def executemany(self, many_new_data):
        """add many new data to database in one go"""
        self.create_table()
        self.cur.executemany('REPLACE INTO jobs VALUES(?, ?, ?, ?)', many_new_data)

    def create_table(self):
        """create a database table if it does not exist already"""
        self.cur.execute('''CREATE TABLE IF NOT EXISTS jobs(title text, \
                                                            job_id integer PRIMARY KEY, 
                                                            company text,
                                                            age integer)''')

    def commit(self):
        """commit changes to database"""
        self.connection.commit()


I have a few questions here:


I named my python module as database.py and my class as Database, which is perhaps not the best way, as they are too generic and somewhat less informative and they are almost identical. Is there a better way to name them?
I exposed the database connection and database cur as instance variables. Is that a good practice?
I exposed the database connection and database cur as instance variables. Is that a good practice?


Having a cursor defined as an instance variable is a common practice. The problem though here is that you don't have a way to close the cursor and the connection itself in case of an error. In situations like this, having "context manager" capabilities on your class would be quite handy:

def __enter__(self):
    return self

def __exit__(self, ext_type, exc_value, traceback):
    self.cursor.close()
    if isinstance(exc_value, Exception):
        self.connection.rollback()
    else:
        self.connection.commit()
    self.connection.close()


Then, the Database class could be used with with:

with Database() as db:
    db.create_table()