Android Studio中的数据库 [英] Database in android studio

查看:522
本文介绍了Android Studio中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Windows Phone开发人员,最近刚开始使用android studio开发android应用.

I'm a windows phone developer and newly I started developing android apps using android studio.

我需要创建一个数据库并在其中存储值并在屏幕上检索更新后的值,因此我需要以下方面的帮助:

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

  1. 创建数据库.
  2. 如何在屏幕上显示数据库中的值?

推荐答案

要创建数据库,您需要扩展SQLiteOpenHelper并需要带有Context的构造函数. 假设您将此类命名为DBOperator.表格创建过程看起来像这样,

to create database , you need to extend SQLiteOpenHelper and need a constructor that takes Context. lets say you name this class DBOperator. The table creation process will look something 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 manipulation class ( add, delete , update ) will look something 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:*数据类是表示对应表的POJO数据类. 既然您说您对这些并不完全陌生,我没有提供任何帮助者注释,因为大多数方法名称都是自解释性的. 希望它能帮助您入门.

P.S : *Data class are POJO data class representing the corresponding table. Since you said you are not totally new to these, I have not provided any helper comments as most of the method names are self explanatory. Hope it helps you to get started.

这篇关于Android Studio中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆