Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tugassqlfirebase;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import androidx.annotation.Nullable;
- public class DatabaseHelper extends SQLiteOpenHelper {
- Context context;
- static final String DATABASE_NAME = "User.db";
- static final int DATABASE_VERSION = 1;
- static final String TABLE_NAME = "user";
- static final String COLUMN_ID = "_id";
- static final String COLUMN_NAME = "fullname";
- static final String COLUMN_USERNAME = "username";
- static final String COLUMN_AGE = "age";
- static final String COLUMN_STATUS = "status";
- DatabaseHelper(@Nullable Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- this.context = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- String query = " CREATE TABLE " + TABLE_NAME +
- " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
- COLUMN_NAME + " TEXT, " +
- COLUMN_USERNAME + " TEXT, " +
- COLUMN_AGE + " TEXT, " +
- COLUMN_STATUS + " TEXT); ";
- db.execSQL(query);
- String insert = " INSERT INTO " + TABLE_NAME + " VALUES ( 1, 'Herza Avicena', 'Apicendol', '21', 'Available' )";
- db.execSQL(insert);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int i, int i1) {
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- void updateData (String id, String fullname, String username, String age, String status) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put(COLUMN_NAME, fullname);
- cv.put(COLUMN_USERNAME, username);
- cv.put(COLUMN_AGE, age);
- cv.put(COLUMN_STATUS, status);
- db.update("user", cv, "_id = ?", new String[]{"1"});
- }
- Cursor readData () {
- SQLiteDatabase DB = this.getWritableDatabase();
- Cursor cursor = DB.rawQuery("SELECT * FROM user", null);
- return cursor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement