· 7 years ago · Nov 18, 2018, 07:32 PM
1package com.example.nimicik.googlemapsgoogleplaces.DBs;
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 com.example.nimicik.googlemapsgoogleplaces.DataCity;
13
14import java.util.ArrayList;
15import java.util.List;
16
17public class DataBaseHelper_1 extends SQLiteOpenHelper
18{
19 public DataBaseHelper_1 dbHelper;
20 public static SQLiteDatabase SQLiteDatabase;
21 public SQLiteOpenHelper DBHelper;
22
23 public static final String DB_NAME = "cities.db";
24 public static final int DB_VERSION = 1;
25 public static final String TABLE_CITIES = "cities";
26 public static final String COLUMN_CITY = "city";
27 public static final String COLUMN_CITY_ID = "city_id";
28 public static final String [] ALL_COLUMNS = {COLUMN_CITY, COLUMN_CITY_ID};
29
30 public static final String SQL_CREATE =
31 "CREATE TABLE IF NOT EXISTS " + TABLE_CITIES + " ( " +
32 COLUMN_CITY_ID + " INTEGER PRIMARY KEY, " +
33 COLUMN_CITY + " TEXT ) ; ";
34
35 public static final String SQL_DROP = "DROP TABLE " + TABLE_CITIES;
36
37 public DataBaseHelper_1(@Nullable Context context)
38 {
39 super(context, DB_NAME, null, DB_VERSION);
40 }
41
42
43 @Override
44 public void onCreate(android.database.sqlite.SQLiteDatabase db)
45 {
46 db.execSQL(SQL_CREATE);
47 }
48
49 @Override
50 public void onUpgrade(android.database.sqlite.SQLiteDatabase db, int oldVersion, int newVersion)
51 {
52 db.execSQL(SQL_DROP);
53 onCreate(db);
54 }
55
56 //---opens the database---
57 public void open() throws SQLException
58 {
59
60 SQLiteDatabase = this.getWritableDatabase();
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 if (DBHelper != null)
71 dbHelper.close();
72 }
73
74// public boolean addCity(String dataCity) {
75// SQLiteDatabase db = this.getWritableDatabase();
76// ContentValues newValues = dataCity.toValues();
77//
78//
79// long result = db.insert(TABLE_CITIES, null, newValues);
80//
81// //if date as inserted incorrectly it will return -1
82// if (result == -1) {
83// return false;
84// } else {
85// return true;
86// }
87// }
88// public Cursor getListContents(){
89// SQLiteDatabase db = this.getWritableDatabase();
90// Cursor data = db.rawQuery("SELECT * FROM " + TABLE_CITIES, null);
91// return data;
92// }
93
94 public int deleteCity(DataCity dataCity)
95 {
96 //String id=String.valueOf(ID);
97 int numberOFEntriesDeleted = SQLiteDatabase.delete(TABLE_CITIES, COLUMN_CITY_ID + " =?",
98 new String[]{String.valueOf(dataCity.getCityId())});
99 return numberOFEntriesDeleted;
100 }
101
102 public List<DataCity> getAllCities()
103 {
104 List<DataCity> cities = new ArrayList<>();
105
106
107 Cursor cursor = SQLiteDatabase.query(TABLE_CITIES, ALL_COLUMNS,
108 null, null, null, null, null);
109
110 while (cursor.moveToNext()) {
111 DataCity c = new DataCity();
112 c.setCityId(cursor.getInt(cursor.getColumnIndex(COLUMN_CITY_ID)));
113 c.setCity(cursor.getString(cursor.getColumnIndex(COLUMN_CITY)));
114 cities.add(c);
115
116 }
117 cursor.close();
118
119 return cities;
120 }
121
122 public long getCitiesCount()
123 {
124 return DatabaseUtils.queryNumEntries(SQLiteDatabase, TABLE_CITIES);
125 }
126
127// // Checking if city exists
128// public boolean checkCity(DataCity dataCity)
129// {
130// SQLiteDatabase = this.getWritableDatabase();
131// Cursor cursor = SQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_CITIES + " WHERE " +
132// COLUMN_CITY + " =? ",
133// new String[]{dataCity.getCity()});
134// if (cursor.getCount() > 0) {
135// cursor.close();
136// return true;
137// } else {
138// cursor.close();
139// return false;
140// }
141// }
142
143 public boolean insertCity(DataCity dataCity)
144 {
145// ContentValues newValues = new ContentValues();
146// // Assign values for each row.
147// newValues.put(COLUMN_CITY, dataCity.getCity());
148// newValues.put(COLUMN_CITY_ID, dataCity.getCityId());
149
150 SQLiteDatabase = this.getWritableDatabase();
151 ContentValues newValues = dataCity.toValues();
152 Cursor cursor = SQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_CITIES + " WHERE " +
153 COLUMN_CITY + " =? ",
154 new String[]{dataCity.getCity()});
155 if (cursor.moveToNext()) {
156 cursor.close();
157 return false;
158 } else {
159 cursor.close();
160 // Insert the row into your table
161 long ins = SQLiteDatabase.insert(TABLE_CITIES, null, newValues);
162 if (ins == -1) return false;
163 else return true;
164
165 }
166
167 }
168}