Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package com.example.databaseapp;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7.  
  8. import androidx.annotation.Nullable;
  9.  
  10. public class Database extends SQLiteOpenHelper {
  11.  
  12.  
  13.  
  14. public static final String DATABASE_NAME = "student.db";
  15. public static final String TABLE_NAME = "student_table";
  16. public static final String COL_1 = "STUDENT_ID";
  17. public static final String COL_2 = "NAME";
  18. public static final String COL_3 = "SURNAME";
  19. public static final String COL_4 = "MARKS";
  20.  
  21. public Database(@Nullable Context context) {
  22. super(context, DATABASE_NAME, null, 1);
  23.  
  24. }
  25.  
  26. @Override
  27. public void onCreate(SQLiteDatabase db) {
  28. db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + "INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2 + " TEXT, " + COL_3 + " TEXT, " + COL_4 + "INTEGER )");
  29.  
  30. }
  31.  
  32. @Override
  33. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  35. onCreate(db);
  36. }
  37.  
  38. public void Update (String name, String surname, int marks) {
  39.  
  40. SQLiteDatabase db = this.getWritableDatabase();
  41.  
  42. /* db.execSQL("INSERT INTO " + TABLE_NAME + " ( " + COL_1 + ", " + COL_2 + ", " + COL_3 + ", " + COL_4 + ")" +
  43. "VALUES ( " + name + ", " + surname + ", " + marks + ")");*/
  44.  
  45.  
  46. ContentValues contentValues = new ContentValues();
  47. contentValues.put(COL_2, name);
  48. contentValues.put(COL_3, surname);
  49. contentValues.put(COL_4, marks);
  50. db.insert(TABLE_NAME, null, contentValues);
  51. db.close();
  52. }
  53. public void Delete (int ID) {
  54.  
  55. SQLiteDatabase db = this.getWritableDatabase();
  56. db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE ID = " + ID);
  57. db.close();
  58. }
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement