Database in Android studio

I am a Windows Mobile developer. Recently, I began to use Android studio to develop Android applications

I need to create a database and store values in it and retrieve updated values on the screen, so I need help:

>Create a database. > How do I display the values in the database on the screen?

resolvent:

To create a database, you need to extend sqliteopenhelper and need a constructor with context. Suppose you name this class dboperator. The table creation process looks like this,

public class DbOperator extends sqliteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "DB_NAME";
    protected static final String FIRST_TABLE_NAME = "FIRST_TABLE";
    protected static final String SECOND_TABLE_NAME = "SECOND_TABLE";

    public static final String CREATE_FIRST_TABLE = "create table if not exists "
            + FIRST_TABLE_NAME
            + " ( _id integer primary key autoincrement, COL1  TEXT NOT NULL, COL2 TEXT NOT NULL,COL3 TEXT, COL4 int, COL5 TEXT,"
            + "COL6 TEXT,COL7 REAL, COL8 INTEGER,COL9 TEXT not null);";

    public static final String CREATE_SECOND_TABLE = "create table if not exists "
            + SECOND_TABLE_NAME+.........

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

    @Override
    public void onCreate(sqliteDatabase db) {
        db.execsql(CREATE_SFIRST_TABLE);
        db.execsql(CREATE_SECOND_TABLE);
        //db.close();
    }

    @Override
    public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {
        //THIS WILL BE EXECUTED WHEN YOU UPDATED VERSION OF DATABASE_VERSION
        //YOUR DROP AND CREATE QUERIES
    }
}

Now your data operation class (add, delete, update) looks like this,

public class FirstTableDML extends DbOperator {

    public FirstTableDML(Context context) {
        super(context);
    }

    private static final String COL_ID = "_id";
    private static final String COL1 = "COL1";
    private static final String COL2 = "COL2";
    ........
    .......

    public void deleteFirstTableDataList(List<FirstTableData> firstTableDataList) {
        for (FirstTableData data : firstTableDataList)
            deleteFirstTableDetailData(data);
    }
    public void deleteFirstTableDetailData(FirstTableData item) {
        sqliteDatabase db = this.getWritableDatabase();
        db.delete(FIRST_TABLE_NAME, item.getId() + "=" + COL_ID, null);
        db.close();
    }
    /**this method retrieves all the records from table and returns them as list of  
     FirstTableData types. Now you use this list to display detail on your screen as per your 
    requirements.
    */
    public List< FirstTableData > getFirstTableDataList() {
        List< FirstTableData > firstTableDataList = new ArrayList< FirstTableData >();
        String refQuery = "Select * From " + FIRST_TABLE_NAME;
        sqliteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(refQuery, null);
        try {
            if (cursor.moveToFirst()) {
                do {
                    FirstTableData itemData = new FirstTableData();

                    itemData.setId(cursor.getInt(0));

                    itemData.setCol1(cursor.getString(1));
                    itemData.setCol2(cursor.getInt(2));
                    .....
                    .....

                    firstTableDataList.add(itemData);
                } while (cursor.moveToNext());
            }
        } finally {

            db.close();
        }

        Collections.sort(itemDataList);
        return itemDataList;
    }

    public int  addFirstTableData(FirstTableData data) {
        sqliteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COL1, data.getCol1());
        values.put(COL2, data.getCol2());
        .....
        .....
        long x=db.insert(FIRST_TABLE_NAME, null, values);
        db.close();
        return (int)x;
    }

    public void updateItemDetailData(FirstTableData data) {
        sqliteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COL1, data.getCol1());
        values.put(COL2, data.getCol2());
        values.put(COL3, data.getCol3());
        .....
        .....


        db.update(FIRST_TABLE_NAME, values, COL_ID + "=" + data.getId(),    null);
        db.close();
    }

}

P. S: * data class is a POJO data class representing the corresponding table. Since you say you are not new, I do not provide any help comments, because most method names are self explanatory. I hope it can help you get started

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>