package se.chalmers.watchme.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "movieManager";
    private static final String TABLE_MOVIES = "movies";
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_RATING = "rating";
    private static final String KEY_NOTE = "note";
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
	
	//Creates new tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_MOVIES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
                + KEY_RATING + " INTEGER," + KEY_NOTE + " TEXT" +  ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
	}

	//Updrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
		onCreate(db);
	}
	
	
}

@@ -43,31 +43,193 @@
 ort 
-android.content.Context
+java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport se.chalmers.watchme.model.Movie;%0Aimport android.content.ContentValues;%0Aimport android.content.Context;%0Aimport android.database.Cursor
 ;%0Aim
@@ -1438,12 +1438,1654 @@
 );%0A%09%7D%0A%09%0A
-
 %09
+/**%0A%09 * Adds a Movie to the database.%0A%09 * @param movie The Movie you want to add.%0A%09 */%0A%09public void addMovie(Movie movie) %7B%0A%09%09SQLiteDatabase db = this.getWritableDatabase();%0A%09%09 %0A%09%09//Is the ID inserted automatic?%0A%09    ContentValues values = new ContentValues();%0A%09    values.put(KEY_TITLE, movie.getTitle()); // Contact Name%0A%09    values.put(KEY_RATING, movie.getRating()); // Contact Phone Number%0A%09    values.put(KEY_NOTE, movie.getNote());%0A%09 %0A%09    // Inserting Row%0A%09    db.insert(TABLE_MOVIES, null, values);%0A%09    db.close(); // Closing database connection%0A%09%7D%0A%09%0A%09/**%0A%09 * @param id The unique id of the Movie%0A%09 * @return the Movie asked for.%0A%09 */%0A%09public Movie getMovie(int id) %7B%0A%09%09SQLiteDatabase db = this.getReadableDatabase();%0A%0A%09%09Cursor cursor = db.query(TABLE_MOVIES, new String%5B%5D %7B KEY_ID,%0A%09%09%09%09KEY_TITLE, KEY_RATING, KEY_NOTE %7D, KEY_ID + %22=?%22,%0A%09%09%09%09new String%5B%5D %7B String.valueOf(id) %7D, null, null, null, null);%0A%09%09if (cursor != null)%0A%09%09%09cursor.moveToFirst();%0A%0A%09%09Movie movie = new Movie(cursor.getString(0), Integer.parseInt(cursor.getString(1)), cursor.getString(2));%0A%09%09return movie;%0A%09%7D%0A%09%0A%09/**%0A%09 * @return All Movies stored in the database.%0A%09 */%0A%09public List%3CMovie%3E getAllMovies() %7B%0A%09%09List%3CMovie%3E allMovies = new ArrayList%3CMovie%3E();%0A%0A%09%09String selectQuery = %22SELECT * FROM %22 + TABLE_MOVIES;%0A%0A%09%09SQLiteDatabase db = this.getWritableDatabase();%0A%09%09Cursor cursor = db.rawQuery(selectQuery, null);%0A%0A%09%09if (cursor.moveToFirst()) %7B%0A%09%09%09do %7B%0A%09%09%09%09Movie movie = new Movie(cursor.getString(0),%0A%09%09%09%09%09%09Integer.parseInt(cursor.getString(1)),%0A%09%09%09%09%09%09cursor.getString(2));%0A%09%09%09%09allMovies.add(movie);%0A%09%09%09%7D while (cursor.moveToNext());%0A%09%09%7D%0A%09%09%0A%09%09return allMovies;%0A%09%7D
 %0A%7D%0A

Create and Read-methods for Movie-table implemented in the DatabaseHandler