Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zrobiłem sobie taką zajebistą klasę:
- class DataBaseAdditional extends SQLiteOpenHelper {
- private static final String TABLE_NAME = "tableAdditional";
- 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)";
- private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
- public DataBaseAdditional(Context context) {
- super(context, TABLE_NAME, null, 1);
- }
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(SQL_CREATE_ENTRIES);
- }
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(SQL_DELETE_ENTRIES);
- onCreate(db);
- }
- public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- onUpgrade(db, oldVersion, newVersion);
- }
- }
- A potem zobacz, tak sobie wpisywałem
- DataBaseAdditional dbHelper = new DataBaseAdditional(getContext());
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("cities", json.getString("name").toUpperCase(Locale.US) + ", " + json.getJSONObject("sys").getString("country"));
- values.put("updateF", df.format(new Date(json.getLong("dt") * 1000)));
- values.put("temperature", main.getDouble("temp"));
- (...)
- long newRowId = db.insert("tableAdditional", null, values);
- if(newRowId == -1)
- {}
- else
- Toast.makeText(getActivity(), "Zapisano do bazy!", Toast.LENGTH_SHORT).show();
- A tu odczyt
- DataBaseAdditional dbHelper = new DataBaseAdditional(getContext());
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String querys = "SELECT * FROM tableAdditional";
- Cursor cursor = db.rawQuery(querys, null);
- if(cursor.moveToLast())
- {
- cityField.setText(cursor.getString(1)); //każdy numerek to kolejne pole
- updatedField.setText(cursor.getString(2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement