Advertisement
Guest User

DatabaseHelper.java

a guest
Nov 3rd, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.16 KB | None | 0 0
  1. import android.annotation.SuppressLint;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.DatabaseUtils;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.util.Log;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11.  
  12. public class DatabaseHelper extends SQLiteOpenHelper {
  13.  
  14. private static final String database_name = "College.db";
  15. public static final String majors_table_name = "Majors";
  16. public static final String students_table_name = "Students";
  17.  
  18. public DatabaseHelper(Context c) {
  19. super(c, database_name, null, 18);
  20. }
  21.  
  22. @Override
  23. public void onCreate(SQLiteDatabase db) {
  24. db.execSQL("CREATE TABLE " + majors_table_name + " (majorID integer primary key autoincrement not null, majorName varchar(50), majorPrefix varchar(50));");
  25. db.execSQL("CREATE TABLE " + students_table_name + " (username varchar(50) primary key not null, fname varchar(50), lname varchar(50), email varchar(50), age integer, GPA float, major varchar(50));");
  26. }
  27.  
  28. @Override
  29. public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  30. db.execSQL("DROP TABLE IF EXISTS " + majors_table_name + ";");
  31. db.execSQL("DROP TABLE IF EXISTS " + students_table_name + ";");
  32. onCreate(db);
  33. }
  34.  
  35. public String getMajors_table_name() {
  36. return majors_table_name;
  37. }
  38.  
  39. public String getStudents_table_name() {
  40. return students_table_name;
  41. }
  42.  
  43. public void initAllTables() {
  44. initMajors();
  45. initStudents();
  46. }
  47.  
  48. private void initMajors() {
  49. if (countRecordsFromTable(majors_table_name) == 0) {
  50. SQLiteDatabase db = this.getWritableDatabase();
  51.  
  52. //Add dummy data
  53. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('App Development', 'CIS')");
  54. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('Psychology', 'PSYCH')");
  55. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('Chemistry', 'CHEM')");
  56. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('Biology', 'BIOL')");
  57. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('Graphic Design', 'ART')");
  58. db.execSQL("INSERT INTO " + majors_table_name + "(majorName, majorPrefix) VALUES ('English', 'ENGL')");
  59.  
  60. //close the database
  61. db.close();
  62. }
  63. }
  64.  
  65. public void initStudents() {
  66. if (countRecordsFromTable(students_table_name) == 0) {
  67. SQLiteDatabase db = this.getWritableDatabase();
  68.  
  69. //Add dummy data
  70. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('CdeSist', 'Cecil', 'deSist', '[email protected]', 23, 2.8, 'App Development');");
  71. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('STired', 'Sally', 'Tored', '[email protected]', 25, 3.0, 'Psychology');");
  72. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('QAsque', 'Quentin', 'Asque', '[email protected]', 27, 1.3, 'Psychology');");
  73. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('FdeSist', 'Fanny', 'deSist', '[email protected]', 26, 3.1, 'App Development');");
  74. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('STop', 'Samuel', 'Top', '[email protected]', 22, 4.0, 'App Development');");
  75. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('YMom', 'Yvonne', 'Mom', '[email protected]', 29, 3.7, 'Chemistry');");
  76. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('BOlogy', 'Beth', 'Ology', '[email protected]', 29, 3.7, 'Biology');");
  77. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('GPhic', 'Ginny', 'Phic', '[email protected]', 24, 1.5, 'Graphic Design');");
  78. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('EGlish', 'Ebony', 'Glish', '[email protected]', 28, 2.0, 'English');");
  79.  
  80. //close database
  81. db.close();
  82. }
  83. }
  84.  
  85. public int countRecordsFromTable(String tableName) {
  86. //get readable database
  87. SQLiteDatabase db = this.getReadableDatabase();
  88.  
  89. //Count num entries
  90. int numRows = (int) DatabaseUtils.queryNumEntries(db, tableName);
  91.  
  92. //close db
  93. db.close();
  94.  
  95. return numRows;
  96. }
  97.  
  98. //Check if a given username is already in the students table
  99. public boolean usernameExists(String newUsername) {
  100. //Step 1: get readable database (DONE)
  101. SQLiteDatabase db = this.getReadableDatabase();
  102. //Step 2: create SQL statement to execute (DONE)
  103. String checkUsername = "SELECT count(username) FROM " + students_table_name + " WHERE username = '" + newUsername + "';";
  104. //Step 3: run the query (DONE)
  105. Cursor cursor = db.rawQuery(checkUsername, null);
  106. //Step 4: move cursor to first (DONE)
  107. cursor.moveToFirst();
  108. //Step 5: get count (DONE)
  109. int count = cursor.getInt(0);
  110. //Step 6: close database (DONE)
  111. db.close();
  112. if (count != 0) {
  113. return true;
  114. }
  115. else {
  116. return false;
  117. }
  118. }
  119.  
  120. //Add new student to student table
  121. //Code below DIDN'T WORK
  122. public void addNewStudent(String newU, String newF, String newL, String newE, Integer newA, Float newGPA, String newM) {
  123. Student newStudent = new Student();
  124.  
  125. SQLiteDatabase db = this.getWritableDatabase();
  126.  
  127. newStudent.setuName(newU);
  128. newStudent.setfName(newF);
  129. newStudent.setlName(newL);
  130. newStudent.seteMail(newE);
  131. newStudent.setAge(newA);
  132. newStudent.setGPA(newGPA);
  133. newStudent.setMajor(newM);
  134.  
  135. /*db.execSQL("INSERT INTO " + students_table_name + "VALUES (newU, newF, newL, newE, newA, newGPA, newM) + "'';"); DIDN'T WORK */
  136.  
  137. /*db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES (newU, newF, newL, newE, newA, newGPA, newM);"); DIDN'T WORK */
  138.  
  139. db.execSQL("INSERT INTO " + students_table_name + " (username, fname, lname, email, age, GPA, major) VALUES ('" + newStudent.getuName() + "','" + newStudent.getfName() + "','" + newStudent.getlName() + "','" + newStudent.geteMail() + "','" + newStudent.getAge() + "','" + newStudent.getGPA() + "','" + newStudent.getMajor() + "');");
  140.  
  141. db.close();
  142. }
  143.  
  144. //Check if a given major name is already in the students table
  145. public boolean majorExists(String newMajor) {
  146. //Step 1: get readable database (DONE)
  147. SQLiteDatabase db = this.getReadableDatabase();
  148. //Step 2: create SQL statement to execute (DONE)
  149. String checkMajorName = "SELECT count(majorName) FROM " + majors_table_name + " WHERE majorName = '" + newMajor + "';";
  150. //Step 3: run the query (DONE)
  151. Cursor cursor = db.rawQuery(checkMajorName, null);
  152. //Step 4: move cursor to first (DONE)
  153. cursor.moveToFirst();
  154. //Step 5: get count (DONE)
  155. int count = cursor.getInt(0);
  156. //Step 6: close database (DONE)
  157. db.close();
  158. if (count != 0) {
  159. return true;
  160. }
  161. else {
  162. return false;
  163. }
  164. }
  165.  
  166. //add new major to major table
  167. public void addNewMajor(String majorN, String majorP) {
  168. Major newMajor = new Major();
  169. int numMajorRecords = this.countRecordsFromTable(this.getMajors_table_name());
  170. int newNumMajors = numMajorRecords + 1;
  171. SQLiteDatabase db = this.getWritableDatabase();
  172.  
  173. newMajor.setId(newNumMajors);
  174. newMajor.setmName(majorN);
  175. newMajor.setmPrefix(majorP);
  176.  
  177. db.execSQL("INSERT INTO " + majors_table_name + " (majorName, majorPrefix) VALUES ('" + newMajor.getmName() + "','" + newMajor.getmPrefix() + "');");
  178.  
  179. db.close();
  180.  
  181. }
  182.  
  183. @SuppressLint("Range")
  184. //attempt at filtering by criteria. DIDN'T WORK
  185. public ArrayList<String> findStudentGivenCritera(String u, String f, String l, String m, Float gpaLower, Float gpaUpper) {
  186. Log.d("passed data ", u + " " + f + " " + l + " " + m + " " + gpaLower.toString() + " " + gpaUpper.toString());
  187. ArrayList<String> listStudents = new ArrayList<String>();
  188. ArrayList<String> columnNames = new ArrayList<>(Arrays.asList("username", "fname", "lname", "major", "age", "GPA"));
  189. String selectStatement = "Select * from " + students_table_name + " Where ";
  190. if (u.isEmpty()) {
  191. selectStatement += "username is not null ";
  192. }
  193. else {
  194. selectStatement += "username = '" + u + "' ";
  195. }
  196. selectStatement += "and ";
  197. if (f.isEmpty()) {
  198. selectStatement += "fname is not null ";
  199. }
  200. else {
  201. selectStatement += "fname = '" + f + "' ";
  202. }
  203. selectStatement += "and ";
  204. if (l.isEmpty()) {
  205. selectStatement += "lname is not null ";
  206. }
  207. else {
  208. selectStatement += "lname = '" + l + "' ";
  209. }
  210. selectStatement += "and ";
  211. if (m.isEmpty()) {
  212. selectStatement += "major is not null ";
  213. }
  214. else {
  215. selectStatement += "major = '" + m + "' ";
  216. }
  217. if (gpaLower != null) {
  218. selectStatement += "and GPA > '" + gpaLower + "' ";
  219. }
  220. else {
  221. selectStatement += "and GPA is not null ";
  222. }
  223. if (gpaUpper != null) {
  224. selectStatement += "and GPA < '" + gpaUpper + "' ";
  225. }
  226. else {
  227. selectStatement += "and GPA is not null ";
  228. }
  229. selectStatement += ";";
  230.  
  231. SQLiteDatabase db = this.getReadableDatabase();
  232.  
  233. Cursor cursor = db.rawQuery(selectStatement, null);
  234. String uname, fname, lname, email;
  235. Integer age;
  236. Float gpa;
  237. String major;
  238.  
  239. if (cursor.moveToFirst()) {
  240. do {
  241. uname = cursor.getString(cursor.getColumnIndex("username"));
  242. fname = cursor.getString(cursor.getColumnIndex("fname"));
  243. lname = cursor.getString(cursor.getColumnIndex("lname"));
  244. email = cursor.getString(cursor.getColumnIndex("email"));
  245. age = cursor.getInt(cursor.getColumnIndex("age"));
  246. gpa = cursor.getFloat(cursor.getColumnIndex("GPA"));
  247. major = cursor.getString(cursor.getColumnIndex("major"));
  248.  
  249. String info = uname + " " + fname + " " + lname + " " + email + " " + age + " " + gpa + " " + major;
  250. Student searchedStudent = new Student(uname, fname, lname, email, age, gpa, major);
  251.  
  252. listStudents.add(info);
  253. }
  254. while (cursor.moveToNext());
  255. }
  256. db.close();
  257. return listStudents;
  258. }
  259.  
  260. //NOTE TO SELF this function didn't work
  261. @SuppressLint("Range")
  262. public ArrayList<Student> filterStudents(String u, String f, String l, String m, Float gpaLower, Float gpaUpper) {
  263. Log.d("passed data ", u + " " + f + " " + l + " " + m + " " + gpaLower.toString() + " " + gpaUpper.toString());
  264. Student fStudent = null;
  265. ArrayList<Student> listFoundStudents = new ArrayList<Student>();
  266. String selectStatement = "Select * from " + students_table_name + " Where ";
  267. if (u.isEmpty()) {
  268. selectStatement += "username is not null ";
  269. }
  270. else {
  271. selectStatement += "username = '" + u + "' ";
  272. }
  273. selectStatement += "and ";
  274. if (f.isEmpty()) {
  275. selectStatement += "fname is not null ";
  276. }
  277. else {
  278. selectStatement += "fname = '" + f + "' ";
  279. }
  280. selectStatement += "and ";
  281. if (l.isEmpty()) {
  282. selectStatement += "lname is not null ";
  283. }
  284. else {
  285. selectStatement += "lname = '" + l + "' ";
  286. }
  287. selectStatement += "and ";
  288. if (m.isEmpty()) {
  289. selectStatement += "major is not null ";
  290. }
  291. else {
  292. selectStatement += "major = '" + m + "' ";
  293. }
  294. if (gpaLower != null) {
  295. selectStatement += "and GPA > '" + gpaLower + "' ";
  296. }
  297. else {
  298. selectStatement += "and GPA is not null ";
  299. }
  300. if (gpaUpper != null) {
  301. selectStatement += "and GPA < '" + gpaUpper + "' ";
  302. }
  303. else {
  304. selectStatement += "and GPA is not null ";
  305. }
  306. selectStatement += ";";
  307.  
  308. SQLiteDatabase db = this.getReadableDatabase();
  309.  
  310. Cursor cursor = db.rawQuery(selectStatement, null);
  311. String uname, fname, lname, email;
  312. Integer age;
  313. Float gpa;
  314. String major;
  315.  
  316. if (cursor.moveToFirst()) {
  317. do {
  318. uname = cursor.getString(cursor.getColumnIndex("username"));
  319. fname = cursor.getString(cursor.getColumnIndex("fname"));
  320. lname = cursor.getString(cursor.getColumnIndex("lname"));
  321. email = cursor.getString(cursor.getColumnIndex("email"));
  322. age = cursor.getInt(cursor.getColumnIndex("age"));
  323. gpa = cursor.getFloat(cursor.getColumnIndex("GPA"));
  324. major = cursor.getString(cursor.getColumnIndex("major"));
  325. Student searchedStudent = new Student(uname, fname, lname, email, age, gpa, major);
  326. listFoundStudents.add(searchedStudent);
  327. }
  328. while (cursor.moveToNext());
  329. }
  330. db.close();
  331. return listFoundStudents;
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement