· 7 years ago · Nov 13, 2018, 06:12 PM
1package com.example.nimicik.mohammed_ahmed_saleh_at2;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.DatabaseUtils;
7import android.database.SQLException;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.support.annotation.Nullable;
11
12import java.util.ArrayList;
13import java.util.List;
14
15public class DataBaseHelper extends SQLiteOpenHelper
16{
17
18
19 public static final String DB_NAME = "cities.db";
20 public static final int DB_VERSION = 1;
21 public static final String TABLE_CITIES = "cities";
22 public static final String COLUMN_CITY = "city";
23 public static final String COLUMN_CITYID = "cityId";
24 public static final String [] ALL_COLUMNS = {COLUMN_CITY, COLUMN_CITYID};
25 public DataBaseHelper dbHelper;
26 public static SQLiteDatabase SQLiteDatabase;
27
28 public static final String SQL_CREATE =
29 " CREATE TABLE IF NOT EXISTS " + TABLE_CITIES + " (" +
30 COLUMN_CITYID + "INTEGER PRIMARY KEY, " +
31 COLUMN_CITY + " TEXT); ";
32
33 public static final String SQL_DROP = "DROP TABLE " + TABLE_CITIES;
34
35
36 public DataBaseHelper(@Nullable Context context)
37 {
38 super(context, DB_NAME, null, DB_VERSION);
39 }
40
41 @Override
42 public void onCreate(SQLiteDatabase db)
43 {
44 db.execSQL(SQL_CREATE);
45
46 }
47
48 @Override
49 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
50 {
51 db.execSQL(SQL_DROP);
52 onCreate(db);
53 }
54
55 //---opens the database---
56 public DataBaseHelper open() throws SQLException
57 {
58
59 SQLiteDatabase = this.getWritableDatabase();
60 return this;
61 }
62
63 //---closes the database---
64 public void close()
65 {
66 if (SQLiteDatabase != null)
67 SQLiteDatabase.close();
68 if (dbHelper != null)
69 dbHelper.close();
70 }
71
72 public List<DataCity> getAllCities()
73 {
74 List<DataCity> cities = new ArrayList<>();
75
76 Cursor cursor = SQLiteDatabase.query(TABLE_CITIES, ALL_COLUMNS,
77 null, null, null, null, null);
78
79 while (cursor.moveToNext()) {
80 DataCity c = new DataCity();
81 c.setCityId(cursor.getInt(cursor.getColumnIndex(COLUMN_CITYID)));
82 c.setCity(cursor.getString(cursor.getColumnIndex(COLUMN_CITY)));
83 cities.add(c);
84 }
85
86 cursor.close();
87 return cities;
88 }
89
90 public long getUsersCount()
91 {
92 return DatabaseUtils.queryNumEntries(SQLiteDatabase, TABLE_CITIES);
93 }
94
95 // Checking if email exists
96 public boolean checkCity(DataCity dataCity)
97 {
98 SQLiteDatabase = this.getWritableDatabase();
99 Cursor cursor = SQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_CITIES + " WHERE " +
100 COLUMN_CITY + " =? ",
101 new String[]{dataCity.getCity()});
102 if (cursor.getCount() > 0) return false;
103 else return true;
104 }
105
106
107 public DataCity insertCity(DataCity dataCity)
108 {
109 ContentValues newValues = new ContentValues(2);
110 // Assign values for each row.
111 newValues.put("cityId", COLUMN_CITYID);
112 newValues.put("city", COLUMN_CITY);
113
114 // Insert the row into your table
115 long ins = SQLiteDatabase.insert(TABLE_CITIES, null, newValues);
116 ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
117 if (ins == -1)
118 return null;
119 else return dataCity;
120
121 }
122
123 public int deleteCity(DataCity dataCity)
124 {
125 //String id=String.valueOf(ID);
126 int numberOFEntriesDeleted = SQLiteDatabase.delete(TABLE_CITIES, COLUMN_CITYID + " =?",
127 new String[]{String.valueOf(dataCity.getCityId())});
128 return numberOFEntriesDeleted;
129 }
130}