Back to project page Do-not-get-annoyed.
The source code is released under:
Apache License
If you think the Android project Do-not-get-annoyed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mn100013d.pmu.data; //w w w . j av a 2s. com import java.util.ArrayList; import mn100013d.pmu.data.GameTableContract.DataEntry; import mn100013d.pmu.models.Result; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class GameDataDbHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "GameData02.db"; private static final String TEXT_TYPE = " TEXT"; private static final String INT_TYPE = " INT"; private static final String COMMA_SEP = ","; private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + DataEntry.TABLE_NAME + " (" + DataEntry._ID + " INTEGER PRIMARY KEY," + DataEntry.COLUMN_NAME_PLAYER_ID + TEXT_TYPE + COMMA_SEP + DataEntry.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP + DataEntry.COLUMN_NAME_EATEN + INT_TYPE + COMMA_SEP + DataEntry.COLUMN_NAME_BEEN_EATEN + INT_TYPE + " )"; private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + DataEntry.TABLE_NAME; public GameDataDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); db.execSQL(SQL_DELETE_ENTRIES); onCreate(db); } public void addNewEntry(String name, int eaten, int beenEaten) { SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DataEntry.COLUMN_NAME_NAME, name); values.put(DataEntry.COLUMN_NAME_EATEN, eaten); values.put(DataEntry.COLUMN_NAME_BEEN_EATEN, beenEaten); db.close(); db = getWritableDatabase(); db.insert(DataEntry.TABLE_NAME, null, values); db.close(); } public ArrayList<Result> getAllEntries() { String[] projection = { DataEntry.COLUMN_NAME_NAME, DataEntry.COLUMN_NAME_EATEN, DataEntry.COLUMN_NAME_BEEN_EATEN }; SQLiteDatabase db = getWritableDatabase(); Cursor cursor = db.query(DataEntry.TABLE_NAME, projection, null, null, null, null, null); cursor.moveToFirst(); ArrayList<Result> results = new ArrayList<Result>(); while (!cursor.isAfterLast()) { results.add(new Result(cursor.getString(0), cursor.getString(1), cursor.getString(2))); cursor.moveToNext(); } cursor.close(); return results; } }