DatabaseManager

A Production-Ready SQLite Manager for Sketchware Pro

Designed for Sketchware Pro v7.0.0 and newer.

This document provides instructions for using DatabaseManager, an asynchronous, singleton-based SQLite helper class designed for robust performance in Android applications. It prevents UI freezes by executing all database operations on a background thread and returns results to the main thread via callbacks.


1. Initial Setup

Adding the Component

To start using DatabaseManager, you must add its component to every Activity or Fragment where you need it. It is recommended to use the same component name every time it is created within the same application.

DatabaseManager Component

Getting the Instance

DatabaseManager uses a singleton pattern to ensure a single, consistent connection point for each database. You should always get the instance in the onCreate event of each activity. Select the component, enter the database name, and its version (default is 1).

Sketchware blocks for getInstance

2. Schema Management

Creating a Table

To create a new table, specify a table name and a definition for columns and their SQL data types. The operation uses CREATE TABLE IF NOT EXISTS to prevent errors on repeated calls.

Sketchware blocks for createTable

You can also define foreign key constraints here, such as ON DELETE CASCADE.

Example of foreign key constraint when creating a table.

3. CRUD Operations

Standard methods for data manipulation: Create, Read, Update, and Delete.

Inserting Data

To add a new row to a table, provide the table name and the column-value pairs to insert. The callback will return the auto-incremented ID of the new row in the get insert id number variable.

Sketchware blocks for insertData

Reading Data (Multiple Records)

To retrieve multiple records, specify the table and optional selection criteria (WHERE clause). The result is returned as an ArrayList of HashMap objects (known as a ListMap in Sketchware). This list is ready to be used anywhere in your activity or fragment.

Sketchware blocks for readData to ListMap

Reading a Single Record

For retrieving a single row, this method is optimized to return only the first matching record as a single HashMap (known as a Map variable in Sketchware).

Sketchware blocks for readSingleRecord

Updating Data

To modify existing records, specify the table, the new data, and a WHERE clause. The callback returns the number of rows affected in the rowsAffected number variable.

Sketchware blocks for updateData

Deleting Data

To remove records, provide the table name and a WHERE clause. The callback returns the number of rows deleted.

Sketchware blocks for deleteData

4. Utility Methods

Helper methods for common checks and queries.

Existence Checks

These methods are designed for quick checks to see if a table, column, or specific value exists. A boolean (true/false) is returned in the callback.

Sketchware blocks for common checks

Get Row Count

This method efficiently retrieves the total number of rows in a specified table.

Sketchware blocks for getRowCount

Execute Raw Query

For complex queries not covered by standard methods (e.g., JOIN), you can execute a raw SQL query. This method returns a ListMap, similar to readData. Blocks are available with and without arguments.

Sketchware blocks for rawQuery

Insert or Update

This convenience method checks for a record's existence. If it exists, it's updated; otherwise, a new record is inserted. Useful for data synchronization.

Sketchware blocks for insertOrUpdate

5. Management Operations

Clear or Delete a Table

Delete Table completely removes the table from the database. Clear Table removes all records from the table, returning the number of affected rows in the rowsAffected variable.

Blocks for table management

Close the Database

This block closes the database connection globally. Only use it when you are certain no other part of your app will need database access. If your app frequently accesses the database, it's better to let the Android system manage the connection lifecycle.

Sketchware block for closing the database

Callback Handling: All asynchronous methods utilize callbacks to return results. It is crucial to implement the success events for handling outcomes and the failure event for error management.

Created by CarlSD

Contact:

🇨🇺

Feel free to modify and share, but please credit the original creator. For suggestions or bug reports, please contact me via email.