SQLite
 sql >> Teknologi Basis Data >  >> RDS >> SQLite

Pengembangan Database MultiTable SQLite Android

Buat kelas Database Singleton:

public class Database extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "yourDatabaseName";

    //Declare a String for each Table name
    public static final String TABLE_NAME1 = "tableName1";
    public static final String TABLE_NAME2 = "tableName2";

    //Declare a SQL string for create each table
    private static final String CREATE_TABLE1 =
            "CREATE TABLE if not exists " + TABLE_NAME1 ..............";

    private static final String CREATE_TABLE2 =
            "CREATE TABLE if not exists " + TABLE_NAME2 .................";

    private static Database Singleton = null;
    private Context context;
    private static SQLiteDatabase Db;

    private Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        this.context = context;
    }

    public static synchronized Database getInstance(Context c){
        if(Singleton == null) {
            Singleton = new Database(c.getApplicationContext());
            Db = Singleton.getWritableDatabase();
        }
        return Singleton;
    }

    public SQLiteDatabase getDatabase() {

        return Db;
    }

     public synchronized void close() {

         if (Singleton != null && Db.isOpen()) Db.close();
     }


    @Override
    protected void finalize() throws Throwable  {

        try{
            close();
        }
        finally{
            super.finalize();
        }

    @Override
    public void onCreate(SQLiteDatabase db) {

        //Create Tables
        db.execSQL(CREATE_TABLE1);
        db.execSQL(CREATE_TABLE2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}  

Buat kelas dasar untuk Tabel. Kelas ini memiliki metode umum untuk menangani tabel

abstract class DatabaseTable {

    private SQLiteDatabase Db;
    private String TableName;

    public DatabaseTable(SQLiteDatabase db,String tableName) {

        this.TableName = tableName;
        this.Db = db;

    }

    public Cursor fetchAll() {

        Cursor cursor = Db.query(TableName, null, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    public Cursor fetchAllOrderBy(String OrderField) {

        Cursor cursor = Db.query(TableName, null, null, null, null, null, OrderField);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    public Cursor fetchById(long id) {

        Cursor cursor = Db.query(TableName, null, "_id = " + id, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

    public boolean deleteById(long id) {

        return Db.delete(TableName,"_id = " + id, null) > 0;
    }

    public boolean deleteAll() {

        return Db.delete(TableName,null, null) > 0;
    }

    public int getNumberRows() {

        return (int) DatabaseUtils.queryNumEntries(Db, TableName);
    }

}

Sekarang Anda harus menentukan kelas untuk setiap tabel yang ingin Anda tangani:

public class TbTable1 extends DatabaseTable{

    //Declare string for each field name
    public static final String FIELD_ID = "_id";
    public static final String FIELD_FIELDNAME1 = "fieldName1";
    public static final String FIELD_FIELDNAME2 = "fieldName2";
    ..........
    ..........

    private SQLiteDatabase Db;

    //Get the table name from Database class
    private static final String TableName = Database.TABLE_NAME1;


    public TbTable1(SQLiteDatabase db) {

        super(db,TableName);
        this.Db = db;
    }

    // Below define all the methods you need to access this table

    public long insertRecord(String value1, String value2) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(FIELD_FIELDNAME2, value1);
        initialValues.put(FIELD_FIELDNAME2, value2);
        return Db.insert(TableName, null, initialValues);
    }

    ..............
    ..............
}  

Saat Anda ingin melakukan sesuatu dengan tabel:

SQLiteDatabase Db = Database.getInstance(context).getDatabase();  
TbTable1 table1 = new TbTable1(Db);  
table1.insertRecord("Value1","Value2");
Cursor cursor = table1.fetchAll();

// do something with data

cursor.close();

Semoga membantu!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Memulai Pencarian Teks Lengkap SQLite

  2. Android:NullPointerException Tidak dapat memuat database ke tampilan daftar dalam sebuah fragmen

  3. Fungsi JSON sekarang Diaktifkan secara Default di SQLite

  4. Hitung Jumlah Detik Sejak Tanggal/Waktu Tertentu di SQLite

  5. Cara meminta ulang secara otomatis dengan LoaderManager