Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. Zrobiłem sobie taką zajebistą klasę:
  2.  
  3. class DataBaseAdditional extends SQLiteOpenHelper {
  4. private static final String TABLE_NAME = "tableAdditional";
  5. private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, cities TEXT, updateF TEXT, temperature TEXT, weatherI TEXT, windSpeed TEXT, windDeg TEXT, humidity TEXT, visible TEXT)";
  6. private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
  7.  
  8.  
  9. public DataBaseAdditional(Context context) {
  10. super(context, TABLE_NAME, null, 1);
  11. }
  12. public void onCreate(SQLiteDatabase db) {
  13. db.execSQL(SQL_CREATE_ENTRIES);
  14. }
  15. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  16. db.execSQL(SQL_DELETE_ENTRIES);
  17. onCreate(db);
  18. }
  19. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  20. onUpgrade(db, oldVersion, newVersion);
  21. }
  22. }
  23.  
  24.  
  25.  
  26.  
  27. A potem zobacz, tak sobie wpisywałem
  28.  
  29. DataBaseAdditional dbHelper = new DataBaseAdditional(getContext());
  30. SQLiteDatabase db = dbHelper.getWritableDatabase();
  31. ContentValues values = new ContentValues();
  32.  
  33. values.put("cities", json.getString("name").toUpperCase(Locale.US) + ", " + json.getJSONObject("sys").getString("country"));
  34. values.put("updateF", df.format(new Date(json.getLong("dt") * 1000)));
  35. values.put("temperature", main.getDouble("temp"));
  36. (...)
  37.  
  38. long newRowId = db.insert("tableAdditional", null, values);
  39.  
  40. if(newRowId == -1)
  41. {}
  42. else
  43. Toast.makeText(getActivity(), "Zapisano do bazy!", Toast.LENGTH_SHORT).show();
  44.  
  45.  
  46.  
  47.  
  48.  
  49. A tu odczyt
  50.  
  51. DataBaseAdditional dbHelper = new DataBaseAdditional(getContext());
  52. SQLiteDatabase db = dbHelper.getWritableDatabase();
  53. String querys = "SELECT * FROM tableAdditional";
  54. Cursor cursor = db.rawQuery(querys, null);
  55.  
  56. if(cursor.moveToLast())
  57. {
  58. cityField.setText(cursor.getString(1)); //każdy numerek to kolejne pole
  59. updatedField.setText(cursor.getString(2));
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement