Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dep.unipiinformaticsapp;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DatabaseClass extends SQLiteOpenHelper {
- public static final String DATABASE_NAME = "unipi.db";
- public static final int DATABASE_VERSION = 1;
- public static final String TABLE_NAME = "credentials";
- public static final String COL_1 = "username";
- public static final String COL_2 = "password";
- public SQLiteDatabase db;
- private final Context context;
- public DatabaseClass(Context context) {
- // context: use to open or create the database
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- this.context = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db = this.getWritableDatabase();
- db.execSQL("CREATE TABLE " + TABLE_NAME +" (USERNAME TEXT PRIMARY KEY,PASSWORD TEXT)");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w("DB", "Upgrading from version " + oldVersion + " to " + newVersion);
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- public DatabaseClass open() throws SQLException {
- Log.w("DB", "Database opened");
- db = this.getWritableDatabase();
- return this;
- }
- public void close() {
- Log.w("DB", "Database closed");
- db.close();
- }
- public SQLiteDatabase getDatabaseInstance() {
- return db;
- }
- public void insertEntry(String userName, String password) {
- ContentValues newValues = new ContentValues();
- newValues.put("USERNAME", userName);
- newValues.put("PASSWORD", password);
- Log.w("DB", newValues + " inserted");
- db.insert("unipi", null, newValues);
- }
- public int deleteEntry(String username) {
- Log.w("DB", username + " deleted");
- return db.delete("unipi", "USERNAME=?", new String[] {username});
- }
- public String getSinlgeEntry(String username) {
- Cursor cursor = db.query("unipi", null, " USERNAME=?", new String[] {username}, null, null, null);
- if (cursor.getCount() < 1) {
- cursor.close();
- return "NOT EXIST";
- }
- cursor.moveToFirst();
- String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
- cursor.close();
- return password;
- }
- public void updateEntry(String username, String password) {
- Log.w("DB", "Entry Updated");
- ContentValues updatedValues = new ContentValues();
- updatedValues.put("USERNAME", username);
- updatedValues.put("PASSWORD", password);
- db.update("LOGIN", updatedValues, "USERNAME=?", new String[] { username });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement