2

私は、アプリ開発、Android、および内部データベース全般の初心者です。文字列で満たされた単純なデータベースをコーディングし、デモンストレーションのために画面上のボタンを押すだけで文字列を表示する必要があります。デモンストレーションの最も重要な部分は、内部データベースです。Android アプリの作成方法に関する適切なチュートリアルが見つからないようです。誰でも助けることができますか?

4

1 に答える 1

1

データベースヘルパーを備えたデータベースアダプターが必要になります。アダプターはデータベース アクセスを処理し、ヘルパーはオープン、作成、およびアップグレードを処理します。Code Project の記事は次のとおりです。

コード プロジェクトの記事

データベース アダプタとヘルパーの使用例を次に示します。

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

以下は、私のプロジェクトの 1 つからのデータベース アダプターです。それは古いもので、今はより良いテクニックがありますが、うまくいきます。

public class 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 に答える