1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
package ryuslash.org.dvdroid;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DVDDataSource
{
private SQLiteDatabase database;
private SQLiteHelper dbHelper;
private String[] allColumns = { SQLiteHelper.COLUMN_ID,
SQLiteHelper.COLUMN_TITLE,
SQLiteHelper.COLUMN_WATCHED,
SQLiteHelper.COLUMN_WISHLIST };
public DVDDataSource(Context context)
{
dbHelper = new SQLiteHelper(context);
}
public void open() throws SQLException
{
database = dbHelper.getWritableDatabase();
}
public void close()
{
dbHelper.close();
}
public DVD createDVD(String title)
{
ContentValues values = new ContentValues();
values.put(SQLiteHelper.COLUMN_TITLE, title);
values.put(SQLiteHelper.COLUMN_WATCHED, 0);
values.put(SQLiteHelper.COLUMN_WISHLIST, 1);
long insertId = database.insert(SQLiteHelper.TABLE_DVD, null,
values);
Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
allColumns,
SQLiteHelper.COLUMN_ID + " = "
+ insertId, null, null, null,
null);
cursor.moveToFirst();
DVD newDVD = cursorToDVD(cursor);
cursor.close();
return newDVD;
}
public void deleteDVD(DVD dvd)
{
long id = dvd.getId();
System.out.println("DVD deleted with id: " + id);
database.delete(SQLiteHelper.TABLE_DVD,
SQLiteHelper.COLUMN_ID + " = " + id, null);
}
public List<DVD> getWishlist()
{
List<DVD> dvds = new ArrayList<DVD>();
Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
allColumns, "wishlist = 1", null,
null, null, "title");
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
DVD dvd = cursorToDVD(cursor);
dvds.add(dvd);
cursor.moveToNext();
}
cursor.close();
return dvds;
}
private DVD cursorToDVD(Cursor cursor)
{
DVD dvd = new DVD();
dvd.setId(cursor.getLong(0));
dvd.setTitle(cursor.getString(1));
dvd.setWatched(cursor.getInt(2));
dvd.setWishlist(cursor.getInt(3));
return dvd;
}
}
|