How to create multiple tables in sqlite android studio

To create multiple tables in SQLite using Android Studio, you need to perform the following steps:

  1. Create a SQLiteOpenHelper subclass to handle the database operations.
  2. Define the table schemas with the necessary columns and data types.
  3. Implement the necessary methods to create the tables within the SQLiteOpenHelper subclass.

Step 1: Create SQLiteOpenHelper subclass

Create a new Java class that extends the SQLiteOpenHelper class. This class will handle the creation and management of the database.


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "my_database.db";
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Create the necessary tables in the database
        createTable1(db);
        createTable2(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop the existing tables if needed and recreate them
        db.execSQL("DROP TABLE IF EXISTS table1");
        db.execSQL("DROP TABLE IF EXISTS table2");
        onCreate(db);
    }
    
    // Method to create table1
    private void createTable1(SQLiteDatabase db) {
        db.execSQL(
            "CREATE TABLE table1 ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "column1 TEXT,"
            + "column2 INTEGER)"
        );
    }
    
    // Method to create table2
    private void createTable2(SQLiteDatabase db) {
        db.execSQL(
            "CREATE TABLE table2 ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "column3 REAL,"
            + "column4 TEXT)"
        );
    }
}
  

Step 2: Define table schemas

In the SQLiteOpenHelper subclass, define the necessary table schemas by using the CREATE TABLE SQL statement. Specify the table name, column names, and their respective data types.

Step 3: Implement necessary methods

Within the SQLiteOpenHelper subclass, implement the necessary methods, such as onCreate() and onUpgrade(), to create and manage the tables.

In the onCreate() method, call the methods to create the tables. In this example, createTable1() and createTable2() are called to create the respective tables.

In the onUpgrade() method, you can handle upgrades to the database version. Drop the existing tables if needed and recreate them using the CREATE TABLE SQL statements.

Make sure to handle any other necessary modifications to the database within the onUpgrade() method, such as adding or altering tables.

Example

Here is an example of using the DatabaseHelper class to create and manage tables in SQLite:


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MainActivity extends AppCompatActivity {
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize the DatabaseHelper
        databaseHelper = new DatabaseHelper(this);

        // Get a writable database instance
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
    }

    @Override
    protected void onDestroy() {
        // Close the database connection
        databaseHelper.close();

        super.onDestroy();
    }
}
  

In this example, the DatabaseHelper class is initialized within the onCreate() method of the MainActivity class. This creates or opens the database file and obtains a writable instance of the database through the getWritableDatabase() method.

It’s important to close the database connection when it’s no longer needed, which is typically done in the onDestroy() method of the activity.

Leave a comment