DatabaseManager For Android

repository·master·Indexed 43 days ago

https://github.com/sanathp/databasemanager_for_android

A library that provides an in-app UI for managing SQLite databases within Android applications. It allows developers to view, edit, and query tables, perform row and table operations, and execute custom SQL statements, functioning as a lightweight database manager similar to MySQL Workbench or Oracle SQL Developer.

Tokens
1.3K
Snippets
3
Records
4
Agent score
6%

What's inside DatabaseManager For Android

  1. Features of DatabaseManager_For_Android

    master

    The library provides a UI within your app to perform the following SQLite operations:

    • View Data: See all table data in a tabular format.
    • Row Operations: Insert, Update, and Delete rows.
    • Table Operations: Delete or Drop tables.
    • Custom Queries: Write and execute custom SQL statements (e.g., CREATE, JOIN, etc.) and view the results directly.
    • Live Testing: Change data in tables and immediately observe how your application responds.
  2. Setup DatabaseManager_For_Android

    master

    To integrate the database manager into your Android application, follow these four steps:

    1. Add the source file: Clone or download the repository and add AndroidDatabaseManager.java to your project.

      • Crucial: Open AndroidDatabaseManager.java and locate lines 54 and 73. Replace yourSqlliteopenhelper with the actual class name of your custom SQLiteOpenHelper class (the class you use to create and manage your app's database tables).
    2. Inject helper functions: Copy the entire content of the helperFunction.txt file provided in the repository and paste it into your custom SQLiteOpenHelper class.

      • If you encounter a compile error regarding MatrixCursor, add import android.database.MatrixCursor; to the top of your SQLiteOpenHelper class file.
    3. Register the Activity: Add the AndroidDatabaseManager activity to your AndroidManifest.xml. Ensure you use the correct package name where AndroidDatabaseManager.java is located.

    4. Launch the Manager: Start the AndroidDatabaseManager activity from within your app (e.g., via a button click or menu item).

    <!-- Step 3: AndroidManifest.xml -->
    <activity android:name="yourpackagename.AndroidDatabaseManager" 
              android:theme="@style/Theme.AppCompat.Light"/>
  3. Launch the AndroidDatabaseManager Activity

    master

    Once configured, you can launch the database management interface using an Intent. You can trigger this from a TextView, a Button, or an Action Bar item. Use the following pattern to start the activity:

    Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class);
    startActivity(dbmanager);
    // Example: Launching via a Button OnClickListener
    Button button = (Button) findViewById(R.id.yourbuttonid);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class);
            startActivity(dbmanager);
        }
    });
  4. Retrieve data using getData(String Query)

    master

    The getData(String Query) method executes a raw SQL query and returns an ArrayList<Cursor> containing up to two cursors. This pattern allows you to handle both the query results and potential error messages within a single return object.

    Return Structure: An ArrayList<Cursor> with a size of 2:

    1. Index 0 (Result Cursor): Contains the actual results of the SQL query if successful. If the query returns no results or fails, this index may be null.
    2. Index 1 (Status/Error Cursor): A MatrixCursor containing a single column named "message".
      • On Success: The row contains the string "Success".
      • On Failure: The row contains the error message string (e.g., the SQLException or general Exception message).

    Usage Pattern:

    1. Call getData with your SQL string.
    2. Check the cursor at index 1 to determine if the operation succeeded.
    3. If successful, process the cursor at index 0.
    // Example usage of getData
    ArrayList<Cursor> results = databaseHelper.getData("SELECT * FROM my_table");
    
    Cursor statusCursor = results.get(1);
    if (statusCursor != null && statusCursor.moveToFirst()) {
        String status = statusCursor.getString(0);
        if (status.equals("Success")) {
            // Operation was successful, process results
            Cursor dataCursor = results.get(0);
            if (dataCursor != null) {
                while (dataCursor.moveToNext()) {
                    // Process your data here
                }
            }
        } else {
            // Operation failed, status contains the error message
            String errorMessage = status;
            Log.e("DB_ERROR", errorMessage);
        }
    }