Advertisement
Guest User

Untitled

a guest
May 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. package com.example.projekat;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.util.Log;
  9.  
  10. import com.example.projekat.Weather;
  11.  
  12.  
  13. public class dbWeather extends SQLiteOpenHelper {
  14.  
  15. private static final String DATABASE_NAME = "weather3.db";
  16. private static final int DATABASE_VERSION = 1;
  17.  
  18. private static final String TABLE_NAME = "weather";
  19.  
  20. private static final String COLUMN_CITY = "city";
  21. private static final String COLUMN_DATE = "date";
  22.  
  23. private static final String COLUMN_TEMPERATURE = "temperature";
  24. private static final String COLUMN_PRESSURE = "pressure";
  25. private static final String COLUMN_HUMIDITY = "humidity";
  26. private static final String COLUMN_SUNRISE = "sunrise";
  27. private static final String COLUMN_SUNSET = "sunset";
  28. private static final String COLUMN_WIND_SPEED = "wind_speed";
  29. private static final String COLUMN_WIND_DIR = "wind_direction";
  30.  
  31.  
  32. public dbWeather(Context context){
  33. super(context, DATABASE_NAME,null,DATABASE_VERSION);
  34. }
  35. @Override
  36. public void onCreate(SQLiteDatabase db) {
  37. final String SQL_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
  38. COLUMN_CITY + " TEXT," +
  39. COLUMN_DATE + " TEXT ," +
  40. COLUMN_TEMPERATURE + " TEXT," +
  41. COLUMN_PRESSURE + " TEXT ," +
  42. COLUMN_HUMIDITY + " TEXT ," +
  43. COLUMN_SUNRISE + " TEXT ," +
  44. COLUMN_SUNSET + " TEXT ," +
  45. COLUMN_WIND_SPEED + " TEXT ," +
  46. COLUMN_WIND_DIR + " TEXT " +
  47. ");";
  48. db.execSQL(SQL_TABLE);
  49. }
  50.  
  51. @Override
  52. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  53. db.execSQL(TABLE_NAME);
  54. onCreate(db);
  55. }
  56.  
  57. public void insert(Weather forecast) {
  58. SQLiteDatabase dbWrite = this.getWritableDatabase();
  59. ContentValues contentValues = new ContentValues();
  60.  
  61.  
  62. contentValues.put(COLUMN_CITY, forecast.getGrad());
  63. contentValues.put(COLUMN_DATE, forecast.getDatum());
  64.  
  65. contentValues.put(COLUMN_TEMPERATURE, forecast.getTempretura());
  66. contentValues.put(COLUMN_PRESSURE, forecast.getPritisak());
  67. contentValues.put(COLUMN_HUMIDITY, forecast.getVlaznost_vazduha());
  68. contentValues.put(COLUMN_SUNRISE, forecast.getIzlazak_sunca());
  69. contentValues.put(COLUMN_SUNSET, forecast.getZalazak_sunca());
  70. contentValues.put(COLUMN_WIND_SPEED, forecast.getBrzina_vetra());
  71. contentValues.put(COLUMN_WIND_DIR, forecast.getSmer_vetra());
  72.  
  73. dbWrite.insert(TABLE_NAME, null, contentValues);
  74. close();
  75. }
  76.  
  77. public boolean remove(String city)
  78. {
  79. SQLiteDatabase db = this.getWritableDatabase();
  80.  
  81. if(db.delete(TABLE_NAME,COLUMN_CITY +"=?",new String[]{city})==-1){
  82. db.close();
  83. return false;
  84. }
  85. else{
  86. db.close();
  87. return true;
  88. }
  89. }
  90.  
  91. public Weather getItem(String city){
  92. SQLiteDatabase db = getReadableDatabase();
  93. Cursor cursor=db.query(TABLE_NAME,null,COLUMN_CITY+"=?",new String[]{city},null,null,null,null);
  94.  
  95. if(cursor.getCount() <= 0)
  96. return null;
  97.  
  98. cursor.moveToLast();
  99. Weather weather = createWeatherItem(cursor);
  100.  
  101. cursor.close();
  102. db.close();
  103.  
  104. return weather;
  105.  
  106. }
  107.  
  108. public Weather createWeatherItem(Cursor cursor){
  109. String city = cursor.getString(cursor.getColumnIndex(COLUMN_CITY));
  110. String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE));
  111.  
  112. String temperature = cursor.getString(cursor.getColumnIndex(COLUMN_TEMPERATURE));
  113. String humidity = cursor.getString(cursor.getColumnIndex(COLUMN_HUMIDITY));
  114. String pressure = cursor.getString(cursor.getColumnIndex(COLUMN_PRESSURE));
  115. String sunrise = cursor.getString(cursor.getColumnIndex(COLUMN_SUNRISE));
  116. String sunset = cursor.getString(cursor.getColumnIndex(COLUMN_SUNSET));
  117. String wind_speed = cursor.getString(cursor.getColumnIndex(COLUMN_WIND_SPEED));
  118. String wind_direction = cursor.getString(cursor.getColumnIndex(COLUMN_WIND_DIR));
  119.  
  120. return new Weather(city,date,temperature,humidity,pressure,wind_speed,sunrise,sunset,wind_direction);
  121. }
  122.  
  123. public Weather getItemByWeekDay(String city, String weekday, int x) {
  124. SQLiteDatabase db = getReadableDatabase();
  125. Cursor cursor = null;
  126.  
  127. if (x == 0)
  128. cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" ;", null, null);
  129. if (x == 1)
  130. cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " >= 10 ;", null, null);
  131. if (x == 2)
  132. cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_TEMPERATURE + " < 10 ;", null, null);
  133. if (x == 3)
  134. cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + city + "\" AND " + COLUMN_DATE + " = \"" + weekday + "\" AND " + COLUMN_HUMIDITY + " > 60 ;", null, null);
  135. assert cursor != null;
  136. if (cursor.getCount() <= 0)
  137. return null;
  138.  
  139. cursor.moveToLast();
  140. Weather forecast = createWeatherItem(cursor);
  141.  
  142. cursor.close();
  143. db.close();
  144.  
  145. return forecast;
  146. }
  147.  
  148. public Weather[] getItems(String item) {
  149. SQLiteDatabase db = this.getReadableDatabase();
  150.  
  151. Cursor cursor = null;
  152.  
  153. //if (number == 0)
  154. cursor = db.query(TABLE_NAME, null, COLUMN_CITY + "=?", new String[]{item}, null, null, COLUMN_TEMPERATURE, null);
  155. //else if (number == 1)
  156. // cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\" and " + COLUMN_TEMPERATURE + " = " +
  157. // "(SELECT MIN(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\");", null, null);
  158. //cursor = db.query(TABLE_NAME, null, "city=?", new String[]{item}, null, null, "temperature ASC");
  159. //cursor.moveToFirst();
  160. //String datum = cursor.getString(cursor.getColumnIndex("date"));
  161. //String grad = cursor.getString(cursor.getColumnIndex("city"));
  162.  
  163. //else if (number == 2)
  164. // cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\" and " + COLUMN_TEMPERATURE + " = " +
  165. // "(SELECT MAX(" + COLUMN_TEMPERATURE + ") FROM " + TABLE_NAME + " WHERE " + COLUMN_CITY + " = \"" + item + "\");", null, null);
  166.  
  167. assert cursor != null;
  168. if (cursor.getCount() <= 0)
  169. return null;
  170.  
  171. Weather[] forecasts = new Weather[cursor.getCount()];
  172.  
  173. int i = 0;
  174. for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) {
  175. forecasts[i++] = createWeatherItem(cursor);
  176. }
  177.  
  178. db.close();
  179.  
  180. return forecasts;
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement