User:Kichz/sandbox

package com.example.test50.it17115780.Database;

import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns;

import java.util.ArrayList; import java.util.List;

public class DBHandler extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "Database.db";

public DBHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }   public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES1); db.execSQL(SQL_CREATE_ENTRIES2); db.execSQL(SQL_CREATE_ENTRIES3); }   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // This database is only a cache for online data, so its upgrade policy is       // to simply to discard the data and start over db.execSQL(SQL_DELETE_ENTRIES1); db.execSQL(SQL_DELETE_ENTRIES2); db.execSQL(SQL_DELETE_ENTRIES3); onCreate(db); }   public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); }

private static final String SQL_CREATE_ENTRIES1 = "CREATE TABLE " + DatabaseMaster.Users.TABLE_NAME + " (" +                   DatabaseMaster.Users._ID + " INTEGER PRIMARY KEY," +                    DatabaseMaster.Users.COLUMN_USERNAME + " TEXT," +                    DatabaseMaster.Users.COLUMN_PASSWORD + " TEXT,"+                    DatabaseMaster.Users.COLUMN_USERTYPE + " TEXT)";;

private static final String SQL_DELETE_ENTRIES1 = "DROP TABLE IF EXISTS " + DatabaseMaster.Users.TABLE_NAME;

private static final String SQL_CREATE_ENTRIES2 = "CREATE TABLE " + DatabaseMaster.Movie.TABLE_NAME + " (" +                   DatabaseMaster.Movie._ID + " INTEGER PRIMARY KEY," +                    DatabaseMaster.Movie.COLUMN_NAME + " TEXT," +                    DatabaseMaster.Movie.COLUMN_YEAR + " TEXT)";;

private static final String SQL_DELETE_ENTRIES2 = "DROP TABLE IF EXISTS " + DatabaseMaster.Users.TABLE_NAME;

private static final String SQL_CREATE_ENTRIES3 = "CREATE TABLE " + DatabaseMaster.Comments.TABLE_NAME + " (" +                   DatabaseMaster.Comments._ID + " INTEGER PRIMARY KEY," +                    DatabaseMaster.Comments.COLUMN_NAME + " TEXT," +                    DatabaseMaster.Comments.COLUMN_RATE + " TEXT,"+                    DatabaseMaster.Comments.COLUMN_COMMENTS + " TEXT)";;

private static final String SQL_DELETE_ENTRIES3 = "DROP TABLE IF EXISTS " + DatabaseMaster.Users.TABLE_NAME;

public long regiterUser(String username, String password){

// Gets the data repository in write mode SQLiteDatabase db = getWritableDatabase;

// Create a new map of values, where column names are the keys ContentValues values = new ContentValues; values.put(DatabaseMaster.Users.COLUMN_USERNAME, username); values.put(DatabaseMaster.Users.COLUMN_PASSWORD, password);

// Insert the new row, returning the primary key value of the new row long newRowId = db.insert(DatabaseMaster.Users.TABLE_NAME, null, values); return newRowId; }

public boolean loginUser(String username, String password){

SQLiteDatabase db = getWritableDatabase; ContentValues values = new ContentValues; values.put(DatabaseMaster.Users.COLUMN_USERNAME, username); values.put(DatabaseMaster.Users.COLUMN_PASSWORD, password);

String selection1 = DatabaseMaster.Users.COLUMN_USERNAME + " LIKE ?"; String[] selectionArgs1 = { username };

String selection2 = DatabaseMaster.Users.COLUMN_USERNAME + " LIKE ?"; String[] selectionArgs2 = { password };

if(username == "admin" && password == selection2 ){ return true; }       else if(username == selection1 && password == selection2) { return true; }       else{ return false; }   }

public List viewUsers(String username){

SQLiteDatabase db = getReadableDatabase;

// Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { BaseColumns._ID, DatabaseMaster.Users.COLUMN_USERNAME, DatabaseMaster.Users.COLUMN_PASSWORD, DatabaseMaster.Users.COLUMN_USERTYPE };

// Filter results WHERE "title" = 'My Title' String selection = DatabaseMaster.Users.COLUMN_USERNAME + " LIKE ?"; String[] selectionArgs = { username };

// How you want the results sorted in the resulting Cursor String sortOrder = DatabaseMaster.Users.COLUMN_USERNAME + " ASC";

Cursor cursor = db.query(               DatabaseMaster.Users.TABLE_NAME,   // The table to query                projection,             // The array of columns to return (pass null to get all)                selection,              // The columns for the WHERE clause                selectionArgs,          // The values for the WHERE clause                null,                   // don't group the rows                null,                   // don't filter by row groups                sortOrder               // The sort order        );

List userInfo = new ArrayList<>; while(cursor.moveToNext) { String user = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Users.COLUMN_USERNAME)); String pass = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Users.COLUMN_PASSWORD)); String type = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Users.COLUMN_USERTYPE)); userInfo.add(user); userInfo.add(pass); userInfo.add(type); }       cursor.close; return userInfo; }

public boolean addMovie(String name, int year){

// Gets the data repository in write mode SQLiteDatabase db = getWritableDatabase;

// Create a new map of values, where column names are the keys ContentValues values = new ContentValues; values.put(DatabaseMaster.Movie.COLUMN_NAME, name); values.put(DatabaseMaster.Movie.COLUMN_YEAR, year);

// Insert the new row, returning the primary key value of the new row long newRowId = db.insert(DatabaseMaster.Users.TABLE_NAME, null, values);

return true; }

public List ViewMovies{

SQLiteDatabase db = getReadableDatabase;

// Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { BaseColumns._ID, DatabaseMaster.Movie.COLUMN_NAME, DatabaseMaster.Movie.COLUMN_YEAR,

};

// How you want the results sorted in the resulting Cursor String sortOrder = DatabaseMaster.Movie.COLUMN_NAME + " ASC";

Cursor cursor = db.query(               DatabaseMaster.Movie.TABLE_NAME,   // The table to query                projection,             // The array of columns to return (pass null to get all)                null,              // The columns for the WHERE clause                null,          // The values for the WHERE clause                null,                   // don't group the rows                null,                   // don't filter by row groups                sortOrder               // The sort order        );

List movieInfo = new ArrayList<>; while(cursor.moveToNext) { String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Movie.COLUMN_NAME)); String year = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Movie.COLUMN_YEAR)); movieInfo.add(name); movieInfo.add(year); }       cursor.close; return movieInfo; }

public long insertComments(String name, int rating,String comments){

// Gets the data repository in write mode SQLiteDatabase db = getWritableDatabase;

// Create a new map of values, where column names are the keys ContentValues values = new ContentValues; values.put(DatabaseMaster.Comments.COLUMN_NAME, name); values.put(DatabaseMaster.Comments.COLUMN_RATE, rating); values.put(DatabaseMaster.Comments.COLUMN_COMMENTS, comments);

// Insert the new row, returning the primary key value of the new row long newRowId = db.insert(DatabaseMaster.Users.TABLE_NAME, null, values); return newRowId; }

public List ViewComments{

SQLiteDatabase db = getReadableDatabase;

// Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { BaseColumns._ID, DatabaseMaster.Comments.COLUMN_NAME, DatabaseMaster.Comments.COLUMN_RATE, DatabaseMaster.Comments.COLUMN_COMMENTS

};

// How you want the results sorted in the resulting Cursor String sortOrder = DatabaseMaster.Comments.COLUMN_NAME + " ASC";

Cursor cursor = db.query(               DatabaseMaster.Comments.TABLE_NAME,   // The table to query                projection,             // The array of columns to return (pass null to get all)                null,              // The columns for the WHERE clause                null,          // The values for the WHERE clause                null,                   // don't group the rows                null,                   // don't filter by row groups                sortOrder               // The sort order        );

List movieInfo = new ArrayList<>; while(cursor.moveToNext) { String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Movie.COLUMN_NAME)); String year = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseMaster.Movie.COLUMN_YEAR)); movieInfo.add(name); movieInfo.add(year); }       cursor.close; return movieInfo; } }