android sqlite database

SQLite is a lightweight relational database management system that is widely used in Android applications. Android provides built-in support for SQLite through the SQLiteDatabase class, which allows developers to create, read, update, and delete records in a SQLite database.

Here's how to use the SQLiteDatabase class to work with a SQLite database in an Android application:

  1. Create a SQLite database: To create a SQLite database, create a subclass of SQLiteOpenHelper and override the onCreate() and onUpgrade() methods to create and update the database schema.

  2. Open the database: To open the database, call the getWritableDatabase() or getReadableDatabase() method on the SQLiteOpenHelper object. The getWritableDatabase() method returns a SQLiteDatabase object that allows read and write access to the database, while the getReadableDatabase() method returns a SQLiteDatabase object that allows read-only access to the database.

  3. Create a table: To create a table in the database, use the execSQL() method of the SQLiteDatabase object to execute a SQL CREATE TABLE statement.

  4. Insert records: To insert a record into a table, use the insert() method of the SQLiteDatabase object and pass in the name of the table, a ContentValues object containing the column values for the new record, and optionally a conflict resolution strategy.

  5. Query records: To query records from a table, use the query() method of the SQLiteDatabase object and pass in the name of the table, an array of columns to return, selection criteria, selection arguments, group by clause, having clause, order by clause, and optionally a limit.

  6. Update records: To update a record in a table, use the update() method of the SQLiteDatabase object and pass in the name of the table, a ContentValues object containing the new column values, selection criteria, and selection arguments.

  7. Delete records: To delete a record from a table, use the delete() method of the SQLiteDatabase object and pass in the name of the table, selection criteria, and selection arguments.

Here's an example of how to use the SQLiteDatabase class to create a SQLite database and insert a record into a table:

public class MyDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "mytable";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_NAME = "name";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

// Open the database
MyDatabaseHelper dbHelper = new MyDatabaseHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();

// Insert a record into the table
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, "John Doe");
long id = db.insert(TABLE_NAME, null, values);
Source‮figi.www:‬tidea.com

Note that the Android SQLite database requires the android.permission.WRITE_EXTERNAL_STORAGE permission in the app's manifest if the database is stored on external storage.