@@ -0,0 +1,3597 @@
+# Copyright 2014 Rico Antonio Felix%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# You may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A# http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0Aimport sqlite3%0A%0A%0Aclass DatabaseConnection:%0A    %22%22%22This class is used an as application specific model of a database connection.%0A%0A    Can be used to perform all database CRUD actions which allows you to do the following:%0A    -%3E Create new records in the connected database%0A    -%3E Retrieve specified records from the connected database%0A    -%3E Update specified records in the connected database%0A    -%3E Delete specified records in the connected database%0A%0A    To perform these operations correctly using this module, class constants are provided to determine how an operation%0A    should be performed.%0A%0A    Class constants are:%0A    CREATE%0A    READ%0A    UPDATE%0A    DELETE%0A%0A    Example of usage:%0A    connection = DatabaseConnection(%22expenses.db%22)%0A    connection.sql_statement(connection.CREATE, %22CREATE TABLE expenses (month TEXT, year INT, expense FLOAT)%22)%0A    connection.sql_statement(connection.UPDATE, %22INSERT INTO expenses (month, year, expense) VALUES ('Jan', 07, 200.99)%22)%0A    data_set = connection.sql_statement(connection.READ, %22SELECT * FROM expenses%22)%0A    connection.sql_statement(connection.DELETE, %22DELETE * FROM expenses%22)%0A    connection.sql_statement(connection.DELETE, %22DROP TABLE IF EXIST expenses%22)%0A    %22%22%22%0A%0A    def __init__(self, database_file):%0A        %22%22%22Initializes a database-connection object to interact with the specified local database.%0A%0A        Checks that the argument passed in is of type 'str' else an 'Exception' is raised.%0A%0A        :param database_file: The local database file to connect to%0A        :return: An initialized object with a connection to the specified database%0A        %22%22%22%0A        if type(database_file) != str:%0A            raise Exception(%0A                %22Invalid argument: database_file of type %7B%7D should be: %3Cclass 'str'%3E%22.format(type(database_file)))%0A        self.__database__ = sqlite3.connect(database_file)%0A        self.CREATE = 0%0A        self.READ = 1%0A        self.UPDATE = 2%0A        self.DELETE = 3%0A%0A    def sql_statement(self, operation, sql):%0A        %22%22%22Performs CRUD operations on the connected database.%0A%0A        Checks that the argument 'operation' passed in is valid else an 'Exception' is raised.%0A        Checks that the argument 'sql' passed in is of type 'str' else an 'Exception' is raised.%0A%0A        :param operation: Operation to perform on the connected database%0A        :param sql: SQL statement to transmit to the underlying database engine%0A        :return: If a READ operation is to be performed, the result set is returned%0A        %22%22%22%0A        if type(sql) != str:%0A            raise Exception(%0A                %22Invalid argument: sql of type %7B%7D should be: %3Cclass 'str'%3E%22.format(type(sql)))%0A        if operation == 0 or operation == 2 or operation == 3:%0A            self.__database__.execute(sql)%0A        elif operation == 1:%0A            return self.__database__.execute(sql)%0A        else:%0A            raise Exception(%22Invalid SQL operation code%22)%0A%0A    def close(self):%0A        %22%22%22Closes the connection to the database.%22%22%22%0A        self.__database__.close()%0A

Create DatabaseConnection.py