Android insert new table and data sqlite

Here is an example of how to insert a new table and data into a SQLite database in Android:

Step 1: Create a SQLite database and table

First, you need to create a SQLite database and a table in your Android app. You can do this by creating a SQLiteOpenHelper class that extends SQLiteOpenHelper.

Here is an example:

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

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE my_table (_id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Handle database upgrades
    }
}

In this example, we create a MyDatabaseHelper class that extends SQLiteOpenHelper. We define the database name and version, and override the onCreate method to create the table.

Step 2: Insert data into the table

To insert data into the table, you can use the insert method of the SQLiteDatabase class.

Here is an example:

public class MyActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new MyDatabaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // Insert data into the table
        ContentValues values = new ContentValues();
        values.put("name", "John");
        values.put("age", 30);
        db.insert("my_table", null, values);

        // Close the database
        db.close();
    }
}

In this example, we create a MyActivity class that extends AppCompatActivity. We create an instance of the MyDatabaseHelper class and get a writable database using the getWritableDatabase method.

We then create a ContentValues object to hold the data we want to insert, and use the insert method to insert the data into the table.

Step 3: Query the table

To query the table, you can use the query method of the SQLiteDatabase class.

Here is an example:

public class MyActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new MyDatabaseHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        // Query the table
        Cursor cursor = db.query("my_table", new String[] {"_id", "name", "age"}, null, null, null, null, null);

        // Loop through the results
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            Log.d("MyActivity", "ID: " + id + ", Name: " + name + ", Age: " + age);
        }

        // Close the cursor and database
        cursor.close();
        db.close();
    }
}

In this example, we create a MyActivity class that extends AppCompatActivity. We create an instance of the MyDatabaseHelper class and get a readable database using the getReadableDatabase method.

We then use the query method to query the table, and loop through the results using a Cursor object.

That's it! This is a basic example of how to insert data into a SQLite database in Android. You can modify the code to fit your specific needs.