试图找出Android内部数据库的基础知识

时间:2012-09-25 02:54:46

标签: android database internal

我是App Development,Android和内部数据库的新手。我需要编写一个充满字符串的简单数据库,并在屏幕上按下按钮显示字符串以进行演示。演示中最重要的部分是内部数据库。我似乎无法找到一个关于如何为Android应用程序编写一个的好教程。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您将需要一个带数据库助手的数据库适配器。适配器将处理您的数据访问,帮助程序将处理打开,创建和升级。以下是Code Project的文章:

Code Project article

以下是使用数据库适配器和帮助程序的示例:

public void loadList() {
    String title = "";
    arrayAdapter.clear();
    DaysDatabaseAdapter dbAdapter = new DaysDatabaseAdapter(getApplicationContext());
    dbAdapter.open();
    Cursor c = dbAdapter.getAllRows();
    if (c.moveToFirst()) {
        do {
            title = c.getString(DaysDatabaseAdapter.POS_TITLE);
            arrayAdapter.add(title);
        } while (c.moveToNext());
    }
    c.close();
    dbAdapter.close();
}

以下是我的某个项目的数据库适配器。这是旧的,我现在有更好的技术,但它有效。

公共类DaysDatabaseAdapter {

// Globals

public static final String TAG = "DaysDBAdapter";

public static final String COLUMN_ROWID = "_id";
public static final String COLUMN_INUSE = "inuse";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_YEAR = "year";
public static final String COLUMN_MONTH = "month";
public static final String COLUMN_DAY = "day";
public static final String[] COLUMN_NAMES = {
    COLUMN_ROWID, COLUMN_INUSE, COLUMN_TITLE, COLUMN_YEAR, COLUMN_MONTH, COLUMN_DAY
};
public static final String[] COLUMN_NAMES_NO_ROWID = {
    COLUMN_INUSE, COLUMN_TITLE, COLUMN_YEAR, COLUMN_MONTH, COLUMN_DAY
};

public static final int POS_ROWID = 0;
public static final int POS_INUSE = 1;
public static final int POS_TITLE = 2;
public static final int POS_YEAR = 3;
public static final int POS_MONTH = 4;
public static final int POS_DAY = 5;

private static final String DATABASE_NAME = "daystildayssince";
private static final String DATABASE_TABLE = "days";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE
        + " (" 
        + COLUMN_ROWID + " integer primary key autoincrement, "
        + COLUMN_INUSE + " integer not null, "
        + COLUMN_TITLE + " text not null, "
        + COLUMN_YEAR + " integer not null, "
        + COLUMN_MONTH + " integer not null, "
        + COLUMN_DAY + " integer not null"
        + ");";

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

// ******************* Functions ***************************

// Constructor

public DaysDatabaseAdapter(Context ctx) {
    MyLog.d(TAG, "Adapter Constructor entered");
    DBHelper = new DatabaseHelper(ctx);
}

// Open the database

public DaysDatabaseAdapter open() throws SQLException {
    MyLog.d(TAG, "open database");
    db = DBHelper.getWritableDatabase();
    return this;
}

// Close the database

public void close() {
    MyLog.d(TAG, "close database");
    DBHelper.close();
}

// Insert row

public long insertRow(int inuse, String title, int year, int month, int day) {
    MyLog.d(TAG, "insertRow");
    initialValues.put(COLUMN_INUSE, inuse);
    initialValues.put(COLUMN_TITLE, title);
    initialValues.put(COLUMN_YEAR, year);
    initialValues.put(COLUMN_MONTH, month);
    initialValues.put(COLUMN_DAY, day);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete row using COLUMN_ROWID

public boolean deleteRow(long rowid) {
    MyLog.d(TAG, "delete row " + Long.toString(rowid));
    return db.delete(DATABASE_TABLE, COLUMN_ROWID + " = " + Long.toString(rowid), null) > 0;
}

// Get all rows

public Cursor getAllRows() {
    MyLog.d(TAG, "getAllRows");
    return db.query(DATABASE_TABLE, COLUMN_NAMES, null, null, null, null, null);
}

// Get specific row using COLUMN_ROWID (primary key)

public Cursor getRowByKey(long rowid) throws SQLException {
    MyLog.d(TAG, "getRowByKey");
    Cursor mCursor = db.query(true, DATABASE_TABLE, COLUMN_NAMES, COLUMN_ROWID + " = " + Long.toString(rowid), null,
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// Get all rows that match COLUMN_INUSE (the Widget AppWidgetId used for the match.)

public Cursor getInuseMatches(int appWidgetId) throws SQLException {
    MyLog.d(TAG, "getInuseMatches");
    Cursor mCursor = db.query(DATABASE_TABLE,
            COLUMN_NAMES, COLUMN_INUSE + " = " + Integer.toString(appWidgetId),
            null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// Get row(s) that match title

public Cursor getTitleMatches(String title) throws SQLException {
    MyLog.d(TAG, "TitleMatches");
    String where[] = {title};
    Cursor mCursor = db.query(DATABASE_TABLE,
            COLUMN_NAMES, COLUMN_TITLE + " = ?",
            where, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// Turn off COLUMN_INUSE in matching rows

public boolean setUnused(int inuse) {
    MyLog.d(TAG, "Adapter setUnused");
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_INUSE, 0);
    return db.update(DATABASE_TABLE, contentValues, COLUMN_INUSE + " = " + Integer.toString(inuse), null) > 0;
}

// Turn off COLUMN_INUSE in all rows

public boolean setAllUnused() {
    MyLog.d(TAG, "Adapter setAllUnused");
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_INUSE, 0);
    return db.update(DATABASE_TABLE, contentValues, null, null) > 0;
}

// Set COLUMN_INUSE to Widget id using COLUMN_ROWID (primary key)

public boolean setInuse(long rowid, int inuse) {
    MyLog.d(TAG, "setInuse (" + Long.toString(rowid) + ", " + Integer.toString(inuse) + ")");
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_INUSE, inuse);
    return db.update(DATABASE_TABLE, contentValues, COLUMN_ROWID + " = " + Long.toString(rowid), null) > 0;
}

// ***************** inline classes ***********************

// Database Helper Class

private static class DatabaseHelper extends SQLiteOpenHelper {

    public static final String TAG = "DaysDBHelper";

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        MyLog.d(TAG, "Constructor");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        MyLog.d(TAG, "Helper onCreate entered");
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        MyLog.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

}