2

一般来说,我是应用程序开发、Android 和内部数据库的新手。我需要编写一个包含字符串的简单数据库,并在屏幕上按下按钮来显示字符串以进行演示。演示中最重要的部分是内部数据库。我似乎找不到关于如何为 Android 应用编写教程的好教程。任何人都可以帮忙吗?

4

1 回答 1

1

您将需要一个带有数据库助手的数据库适配器。适配器将处理您的数据访问,助手将处理打开、创建和升级。这是代码项目的一篇文章:

代码项目文章

下面是一个使用数据库适配器和助手的例子:

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 {

// 全局变量

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);
    }
}

}

于 2012-09-25T03:28:34.937 回答