· 9 years ago · Nov 04, 2016, 09:32 AM
1package com.example.pr_idi.mydatabaseexample;
2
3/**
4 * MySQLiteHelper
5 * Created by pr_idi on 10/11/16.
6 */
7import android.content.Context;
8import android.database.sqlite.SQLiteDatabase;
9import android.database.sqlite.SQLiteOpenHelper;
10import android.util.Log;
11
12public class MySQLiteHelper extends SQLiteOpenHelper {
13
14 public static final String TABLE_FILMS = "films";
15 public static final String COLUMN_ID = "_id";
16 public static final String COLUMN_TITLE = "title";
17 public static final String COLUMN_COUNTRY = "country";
18 public static final String COLUMN_YEAR_RELEASE = "year_release";
19 public static final String COLUMN_DIRECTOR = "director";
20 public static final String COLUMN_PROTAGONIST = "protagonist";
21 public static final String COLUMN_CRITICS_RATE = "critics_rate";
22
23 private static final String DATABASE_NAME = "films.db";
24 private static final int DATABASE_VERSION = 1;
25
26 // Database creation sql statement
27 private static final String DATABASE_CREATE = "create table " + TABLE_FILMS + "( "
28 + COLUMN_ID + " integer primary key autoincrement, "
29 + COLUMN_TITLE + " text not null, "
30 + COLUMN_COUNTRY + " text not null, "
31 + COLUMN_YEAR_RELEASE + " integer not null, "
32 + COLUMN_DIRECTOR + " text not null, "
33 + COLUMN_PROTAGONIST + " text not null, "
34 + COLUMN_CRITICS_RATE + " integer"
35 + ");";
36
37 public MySQLiteHelper(Context context) {
38 super(context, DATABASE_NAME, null, DATABASE_VERSION);
39 }
40
41 @Override
42 public void onCreate(SQLiteDatabase database) {
43 database.execSQL(DATABASE_CREATE);
44 }
45
46 @Override
47 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
48 Log.w(MySQLiteHelper.class.getName(),
49 "Upgrading database from version " + oldVersion + " to "
50 + newVersion + ", which will destroy all old data");
51 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILMS);
52 onCreate(db);
53 }
54
55}