publicstaticlongnormalizeDate(long startDate){ Time time = new Time(); time.set(startDate); int julianDay = Time.getJulianDay(startDate, time.gmtoff); return time.setJulianDay(julianDay); }
privatestaticfinalint DATABASE_VERSION = 2; // 데이터 베이스의 버전을 의미합니다 staticfinal String DATABASE_NAME = "weather.db"; // 데이터 베이스 파일의 이름을 정합니다 publicWeatherDbHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); // 생성자를 이용해서 데이터 베이스 이름, 버전을 정하게 됩니다 }
WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " + WeatherEntry.COLUMN_DATE + " INTEGER NOT NULL, " + WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " + WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," +
WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " + WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " +
WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " + WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " + WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " + WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " +
// public void testCreateDb() throws Throwable { // final HashSet<String> tableNameHashSet = new HashSet<String>(); // tableNameHashSet.add(WeatherContract.LocationEntry.TABLE_NAME); // tableNameHashSet.add(WeatherContract.WeatherEntry.TABLE_NAME); // // mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME); // SQLiteDatabase db = new WeatherDbHelper( // this.mContext).getWritableDatabase(); // assertEquals(true, db.isOpen()); // // Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); // // assertTrue("Error: This means that the database has not been created correctly", // c.moveToFirst()); // // do { // tableNameHashSet.remove(c.getString(0)); // } while( c.moveToNext() ); // // assertTrue("Error: Your database was created without both the location entry and weather entry tables", // tableNameHashSet.isEmpty()); // // c = db.rawQuery("PRAGMA table_info(" + WeatherContract.LocationEntry.TABLE_NAME + ")", // null); // // assertTrue("Error: This means that we were unable to query the database for table information.", // c.moveToFirst()); // // final HashSet<String> locationColumnHashSet = new HashSet<String>(); // locationColumnHashSet.add(WeatherContract.LocationEntry._ID); // locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_CITY_NAME); // locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LAT); // locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LONG); // locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING); // // int columnNameIndex = c.getColumnIndex("name"); // do { // String columnName = c.getString(columnNameIndex); // locationColumnHashSet.remove(columnName); // } while(c.moveToNext()); // // assertTrue("Error: The database doesn't contain all of the required location entry columns", // locationColumnHashSet.isEmpty()); // db.close(); // }
publicclassTestPracticeextendsAndroidTestCase{ /* This gets run before every test. */ @Override protectedvoidsetUp()throws Exception { super.setUp(); }
publicvoidtestThatDemonstratesAssertions()throws Throwable { int a = 5; int b = 3; int c = 5; int d = 10;
assertEquals("X should be equal", a, c); assertTrue("Y should be true", d > a); assertFalse("Z should be false", a == b);
데이터베이스를 사용하다 보면 업그레이드할 일이 생깁니다. Column이 변경(추가/삭제)이 되는 경우도 있고 테이블이 추가되어서 관계를 맺는 경우도 있습니다
이런 상황을 대비 해 뒀습니다
1 2 3 4 5 6 7 8 9
privatestaticfinalint DATABASE_VERSION = 2; // 데이터 베이스의 버전을 의미합니다 staticfinal String DATABASE_NAME = "weather.db";
// 데이터 베이스 파일의 이름을 정합니다 publicWeatherDbHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); // 생성자를 이용해서 데이터 베이스 이름, 버전을 정하게 됩니다 }
만약 버전이 변경되어서 DATABASE_VERSION 값이 3이 되면
1 2 3 4 5
publicvoidonUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion){ sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME); sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); onCreate(sqLiteDatabase); }
onUpgrade() 메서드가 실행됩니다 우리는 현재 버전이 변경되면 테이블이 DROP합니다 다른 형태로 변경하고 싶다면 SQLite를 참고하시기 바랍니다
다음과 같은 구조에서 Data Contract, DB Helper, SQLiteDatabase 을 구축하였습니다
데이터베이스를 읽기/쓰기/검사 하기 위해서 여러 가지를 사용합니다
1
SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase();
long locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);
assertTrue("Error : No Insert Data ", locationRowId != -1);
Cursor cursor = db.query(WeatherContract.LocationEntry.TABLE_NAME, // Table to Query null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null, // columns to group by null, // columns to filter by row groups null// sort order );
assertTrue("Error : No Records retuned from location query", cursor.moveToFirst());
WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase();
long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, weatherValues); assertTrue(weatherRowId != -1);
Cursor weatherCursor = db.query( WeatherContract.WeatherEntry.TABLE_NAME, // Table to Query null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null, // columns to group by null, // columns to filter by row groups null// sort order );
assertTrue("Error: No Records returned from location query", weatherCursor.moveToFirst());
TestUtilities.validateCurrentRecord("testInsertReadDb weatherEntry failed to validate", weatherCursor, weatherValues);
assertFalse("Error: More than one record returned from weather query", weatherCursor.moveToNext());
long locationRowId; locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);
assertTrue(locationRowId != -1);
Cursor cursor = db.query( WeatherContract.LocationEntry.TABLE_NAME, // Table to Query null, // all columns null, // Columns for the "where" clause null, // Values for the "where" clause null, // columns to group by null, // columns to filter by row groups null// sort order );
assertTrue( "Error: No Records returned from location query", cursor.moveToFirst() );