Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.databaseapp;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import androidx.annotation.Nullable;
- public class Database extends SQLiteOpenHelper {
- public static final String DATABASE_NAME = "student.db";
- public static final String TABLE_NAME = "student_table";
- public static final String COL_1 = "STUDENT_ID";
- public static final String COL_2 = "NAME";
- public static final String COL_3 = "SURNAME";
- public static final String COL_4 = "MARKS";
- public Database(@Nullable Context context) {
- super(context, DATABASE_NAME, null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + "INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2 + " TEXT, " + COL_3 + " TEXT, " + COL_4 + "INTEGER )");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- public void Update (String name, String surname, int marks) {
- SQLiteDatabase db = this.getWritableDatabase();
- /* db.execSQL("INSERT INTO " + TABLE_NAME + " ( " + COL_1 + ", " + COL_2 + ", " + COL_3 + ", " + COL_4 + ")" +
- "VALUES ( " + name + ", " + surname + ", " + marks + ")");*/
- ContentValues contentValues = new ContentValues();
- contentValues.put(COL_2, name);
- contentValues.put(COL_3, surname);
- contentValues.put(COL_4, marks);
- db.insert(TABLE_NAME, null, contentValues);
- db.close();
- }
- public void Delete (int ID) {
- SQLiteDatabase db = this.getWritableDatabase();
- db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE ID = " + ID);
- db.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement