Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.projekat;
- 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;
- import com.example.projekat.Weather;
- public class dbWeather extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "weather3.db";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_NAME = "weather";
- private static final String COLUMN_CITY = "city";
- private static final String COLUMN_DATE = "date";
- private static final String COLUMN_TEMPERATURE = "temperature";
- private static final String COLUMN_PRESSURE = "pressure";
- private static final String COLUMN_HUMIDITY = "humidity";
- private static final String COLUMN_SUNRISE = "sunrise";
- private static final String COLUMN_SUNSET = "sunset";
- private static final String COLUMN_WIND_SPEED = "wind_speed";
- private static final String COLUMN_WIND_DIR = "wind_direction";
- public dbWeather(Context context){
- super(context, DATABASE_NAME,null,DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- final String SQL_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
- COLUMN_CITY + " TEXT," +
- COLUMN_DATE + " TEXT ," +
- COLUMN_TEMPERATURE + " TEXT," +
- COLUMN_PRESSURE + " TEXT ," +
- COLUMN_HUMIDITY + " TEXT ," +
- COLUMN_SUNRISE + " TEXT ," +
- COLUMN_SUNSET + " TEXT ," +
- COLUMN_WIND_SPEED + " TEXT ," +
- COLUMN_WIND_DIR + " TEXT " +
- ");";
- db.execSQL(SQL_TABLE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(TABLE_NAME);
- onCreate(db);
- }
- public void insert(Weather forecast) {
- SQLiteDatabase dbWrite = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(COLUMN_CITY, forecast.getGrad());
- contentValues.put(COLUMN_DATE, forecast.getDatum());
- contentValues.put(COLUMN_TEMPERATURE, forecast.getTempretura());
- contentValues.put(COLUMN_PRESSURE, forecast.getPritisak());
- contentValues.put(COLUMN_HUMIDITY, forecast.getVlaznost_vazduha());
- contentValues.put(COLUMN_SUNRISE, forecast.getIzlazak_sunca());
- contentValues.put(COLUMN_SUNSET, forecast.getZalazak_sunca());
- contentValues.put(COLUMN_WIND_SPEED, forecast.getBrzina_vetra());
- contentValues.put(COLUMN_WIND_DIR, forecast.getSmer_vetra());
- dbWrite.insert(TABLE_NAME, null, contentValues);
- close();
- }
- public boolean remove(String city)
- {
- SQLiteDatabase db = this.getWritableDatabase();
- if(db.delete(TABLE_NAME,COLUMN_CITY +"=?",new String[]{city})==-1){
- db.close();
- return false;
- }
- else{
- db.close();
- return true;
- }
- }
- public Weather getItem(String city){
- SQLiteDatabase db = getReadableDatabase();
- Cursor cursor=db.query(TABLE_NAME,null,COLUMN_CITY+"=?",new String[]{city},null,null,null,null);
- if(cursor.getCount() <= 0)
- return null;
- cursor.moveToLast();
- Weather weather = createWeatherItem(cursor);
- cursor.close();
- db.close();
- return weather;
- }
- public Weather createWeatherItem(Cursor cursor){
- String city = cursor.getString(cursor.getColumnIndex(COLUMN_CITY));
- String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE));
- String temperature = cursor.getString(cursor.getColumnIndex(COLUMN_TEMPERATURE));
- String humidity = cursor.getString(cursor.getColumnIndex(COLUMN_HUMIDITY));
- String pressure = cursor.getString(cursor.getColumnIndex(COLUMN_PRESSURE));
- String sunrise = cursor.getString(cursor.getColumnIndex(COLUMN_SUNRISE));
- String sunset = cursor.getString(cursor.getColumnIndex(COLUMN_SUNSET));
- String wind_speed = cursor.getString(cursor.getColumnIndex(COLUMN_WIND_SPEED));
- String wind_direction = cursor.getString(cursor.getColumnIndex(COLUMN_WIND_DIR));
- return new Weather(city,date,temperature,humidity,pressure,wind_speed,sunrise,sunset,wind_direction);
- }
- public Weather getItemByWeekDay(String city, String weekday, int x) {
- SQLiteDatabase db = getReadableDatabase();
- Cursor cursor = null;
- if (x == 0)
- cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" ;", null, null);
- if (x == 1)
- cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " >= 10 ;", null, null);
- if (x == 2)
- cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " < 10 ;", null, null);
- if (x == 3)
- cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_HUMIDITY + " > 60 ;", null, null);
- assert cursor != null;
- if (cursor.getCount() <= 0)
- return null;
- cursor.moveToLast();
- Weather forecast = createWeatherItem(cursor);
- cursor.close();
- db.close();
- return forecast;
- }
- public Weather[] getItems(String item) {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = null;
- //if (number == 0)
- cursor = db.query(TABLE_NAME, null, COLUMN_CITY + "=?", new String[]{item}, null, null, COLUMN_TEMPERATURE, null);
- //else if (number == 1)
- // cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\" and " + COLUMN_TEMPERATURE + " = " +
- // "(SELECT MIN(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\");", null, null);
- //cursor = db.query(TABLE_NAME, null, "city=?", new String[]{item}, null, null, "temperature ASC");
- //cursor.moveToFirst();
- //String datum = cursor.getString(cursor.getColumnIndex("date"));
- //String grad = cursor.getString(cursor.getColumnIndex("city"));
- //else if (number == 2)
- // cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\" and " + COLUMN_TEMPERATURE + " = " +
- // "(SELECT MAX(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\");", null, null);
- assert cursor != null;
- if (cursor.getCount() <= 0)
- return null;
- Weather[] forecasts = new Weather[cursor.getCount()];
- int i = 0;
- for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) {
- forecasts[i++] = createWeatherItem(cursor);
- }
- db.close();
- return forecasts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement