· 8 years ago · Jul 02, 2018, 05:40 PM
1package com.example.marc.finalproject.Model;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.database.sqlite.SQLiteQuery;
9
10import java.util.ArrayList;
11import java.util.List;
12
13import static java.sql.Types.FLOAT;
14
15
16public class VideosDatabase extends SQLiteOpenHelper implements IVideosDatabase {
17
18 private static final String VIDEOS = "VIDEOS";
19 private static final String DB_NAME = "myvideos.db";
20 private static final int DB_VERSION = 1;
21
22 private static final String VIDEO_ID = "VIDEO_ID";
23 private static final String VIDEO_TITLE = "VIDEO_TITLE";
24
25 private static final String INTEGER_NOT_NULL = "integer not null";
26 private static final String INTEGER = "integer";
27 private static final String TEXT_NOT_NULL = "text not null";
28 private static final String TEXT_NOT_NULL_UNIQUE = "text not null unique";
29 private static final String TEXT = "text";
30 private static final String REAL = "real";
31 private static final String VIEW_COUNT = "VIEW_COUNT";
32 private static final String THUMBNAIL_URL = "THUMBNAIL_URL";
33 private static final String VIDEO_RATING = "VIDEO_RATING";
34 private static final String VIDEO_COMMENT = "VIDEO_COMMENT";
35 private static final String CHANNEL = "CHANNEL";
36
37
38 public VideosDatabase(Context context) {
39 super(context, DB_NAME, null, DB_VERSION);
40 }
41
42 @Override
43 public void onCreate(SQLiteDatabase db) {
44 createVideosTable(db);
45 }
46
47 @Override
48 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49 db.execSQL("drop table if exists " + VIDEOS);
50 onCreate(db);
51 }
52
53
54 @Override
55 public List<VideoData> getAllVideos() {
56 SQLiteDatabase db = getReadableDatabase();
57
58 Cursor cursor = db.query(VIDEOS,null,
59 null,
60 null,
61 null,
62 null,
63 VIDEO_TITLE);
64
65 List<VideoData> videos = new ArrayList <>();
66
67 while(cursor.moveToNext()){
68 VideoData videoData = new VideoData(cursor.getString(1));
69 videoData.setVideoId(cursor.getString(0));
70 videoData.setRating(cursor.getFloat(2));
71 videoData.setChannel(cursor.getString(3));
72 videoData.setComment(cursor.getString(4));
73 videoData.setViewCount(cursor.getLong(5));
74 videoData.setThumbnailURL(cursor.getString(6));
75 videos.add(videoData);
76 }
77
78 cursor.close();
79 return videos;
80 }
81
82 @Override
83 public VideoData getVideoData(String id) {
84 SQLiteDatabase db = getReadableDatabase();
85
86 Cursor cursor = db.query(VIDEOS,
87 null,
88 VIDEO_ID + "=?",
89 new String[]{id},
90 null,
91 null,
92 null,
93 null);
94
95 VideoData videoData = null;
96
97 if(cursor.moveToNext()){
98 videoData = new VideoData(cursor.getString(1));
99 videoData.setVideoId(cursor.getString(0));
100 videoData.setRating(cursor.getFloat(2));
101 videoData.setChannel(cursor.getString(3));
102 videoData.setComment(cursor.getString(4));
103 videoData.setViewCount(cursor.getInt(5));
104 videoData.setThumbnailURL(cursor.getString(6));
105 }
106
107 cursor.close();
108 return videoData;
109 }
110
111 @Override
112 public void insertVideoData(VideoData videoData) {
113 SQLiteDatabase db = getWritableDatabase();
114
115 ContentValues values = new ContentValues();
116 values.put(VIDEO_ID, videoData.getVideoId());
117 values.put(VIDEO_TITLE, videoData.getTitle());
118 values.put(VIDEO_RATING, videoData.getRating());
119 values.put(CHANNEL, videoData.getChannel());
120 values.put(VIDEO_COMMENT, videoData.getComment());
121 values.put(VIEW_COUNT, videoData.getViewCount());
122 values.put(THUMBNAIL_URL, videoData.getThumbnailURL());
123
124 db.insertWithOnConflict(VIDEOS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
125 }
126
127 @Override
128 public void deleteVideo(String storeVideoId) {
129 SQLiteDatabase db = getWritableDatabase();
130 db.delete(VIDEOS, VIDEO_ID + "=?" , new String[]{storeVideoId});
131 }
132
133 private void createVideosTable(SQLiteDatabase db) {
134 String create = "create table " + VIDEOS + " ("
135 + VIDEO_ID + " " + TEXT_NOT_NULL_UNIQUE + " primary key not null,"
136 + VIDEO_TITLE + " " + TEXT + ","
137 + VIDEO_RATING + " " + REAL + ","
138 + CHANNEL + " " + TEXT + ","
139 + VIDEO_COMMENT + " " + TEXT + ","
140 + VIEW_COUNT + " " + INTEGER + ","
141 + THUMBNAIL_URL + " " + TEXT + ");";
142 db.execSQL(create);
143 }
144}