-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHelper.java
164 lines (130 loc) · 5.14 KB
/
DatabaseHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.scoresheet.discgolf;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
/**
* Created by Joe Post on 4/12/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DiscGolfCourseTable";
// Table Names
private static final String COURSE_TABLE = "DiscGolfCourses";
// Common column names
private static final String KEY_ID = "id";
private static final String KEY_COURSE_NAME = "course_name";
private static final String KEY_COURSE_ID = "course_id";
// Table Create Statements
// Course table create statement, needs to be private, only one of these tables
private static final String CREATE_COURSE_TABLE = "CREATE TABLE "
+ COURSE_TABLE + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_COURSE_NAME + " VARCHAR(64),"
+ KEY_COURSE_ID + " INTEGER" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_COURSE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + COURSE_TABLE);
// create new tables
onCreate(db);
}
/*
* get single course information
*/
public ArrayList<String> getAllDoneCoursesNames(ArrayList<Integer> ids) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = null;
ArrayList<String> names = new ArrayList<>();
for(int i = 0; i < ids.size(); i++){
String selectQuery = "SELECT * FROM " + COURSE_TABLE + " WHERE "
+ KEY_COURSE_ID + " = ?";
String[] id_query = {Integer.toString(ids.get(i))};
c = db.rawQuery(selectQuery, id_query);
if(c != null && c.getCount() > 0) {
c.moveToFirst();
names.add(i, c.getString(c.getColumnIndex(KEY_COURSE_NAME)));
}
}
return names;
}
/*
* getting all courses
* */
public ArrayList<String> getAllCoursesNames() {
ArrayList<String> DGCs = new ArrayList<String>();
//just search for all names from the database
String selectQuery = "SELECT * FROM " + COURSE_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
int loop = 0;
// looping through all rows and adding to list
if(c != null && c.getCount() > 0) {
if (c.moveToFirst()) {
do {
//add each name to the
DGCs.add(loop, c.getString(c.getColumnIndex(KEY_COURSE_NAME)));
loop++;
} while (c.moveToNext());
}
c.close();
}
return DGCs;
}
//now we need to make a code snippet that will let us add entries to the
//course table.
public void addNewCourseName(String course_name) {
//use this method to add a row to the current SingleRoundScoringTable
ContentValues values = new ContentValues(2);
String selectQuery = "SELECT * FROM " + COURSE_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
Integer course_id = 0;
if(c == null || c.getCount() == 0){
course_id = 0;
} else {
c.moveToLast();
course_id = c.getInt(c.getColumnIndex(KEY_COURSE_ID)) + 1;
}
c.close();
//insert into table
values.put(KEY_COURSE_NAME, course_name);
values.put(KEY_COURSE_ID, course_id);
db.insert(COURSE_TABLE, KEY_COURSE_NAME, values);
}
public String getCourseName(Integer id) {
ArrayList<String> DGCs = new ArrayList<String>();
//just search for all names from the database
String selectQuery = "SELECT * FROM " + COURSE_TABLE + " where course_id = ?";
SQLiteDatabase db = this.getReadableDatabase();
String[] ID = {String.valueOf(id)};
Cursor c = db.rawQuery(selectQuery, ID);
String name = null;
int loop = 0;
// looping through all rows and adding to list
if(c != null && c.getCount() > 0) {
if (c.moveToFirst()) {
do {
//add each name to the
name = c.getString(c.getColumnIndex(KEY_COURSE_NAME));
} while (c.moveToNext());
}
c.close();
}
return name;
}
}