· 8 years ago · Jul 25, 2018, 10:22 PM
1package com.rough.app;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10
11public class DBAdapter
12{
13 public static final String KEY_ROWID = "_id";
14 public static final String KEY_ISBN = "isbn";
15 public static final String KEY_TITLE = "title";
16 public static final String KEY_PUBLISHER = "publisher";
17 private static final String TAG = "DBAdapter";
18
19 private static final String DATABASE_NAME = "books";
20 private static final String DATABASE_TABLE = "titles";
21
22 private static final int DATABASE_VERSION = 2;
23
24 private static final String DATABASE_CREATE =
25 "create table titles (_id integer primary key autoincrement, "
26 + "isbn text not null, title text not null, "
27 + "publisher text not null);";
28
29 private final Context context;
30 private DatabaseHelper DBHelper;
31 private SQLiteDatabase db;
32
33 public DBAdapter(Context ctx)
34 {
35 this.context = ctx;
36 DBHelper = new DatabaseHelper(context);
37 }
38
39 private static class DatabaseHelper extends SQLiteOpenHelper
40 {
41 DatabaseHelper(Context context)
42 {
43 super(context, DATABASE_NAME, null, DATABASE_VERSION);
44 }
45
46 @Override
47 public void onCreate(SQLiteDatabase db)
48 {
49 db.execSQL(DATABASE_CREATE);
50 }
51
52 @Override
53 public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion)
54 {
55 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
56 + newVersion + ", which will destroy all old data");
57 db.execSQL("DROP TABLE IF EXISTS titles");
58 onCreate(db);
59 }
60 }
61 //---opens the database---
62 public DBAdapter open() throws SQLException
63 {
64 db = DBHelper.getWritableDatabase();
65 return this;
66 }
67
68 //---closes the database---
69 public void close()
70 {
71 DBHelper.close();
72 }
73
74
75 //---insert a title into the database---
76 public long insertTitle(String isbn, String title, String publisher)
77 {
78 ContentValues initialValues = new ContentValues();
79 initialValues.put(KEY_ISBN, isbn);
80 initialValues.put(KEY_TITLE, title);
81 initialValues.put(KEY_PUBLISHER, publisher);
82 return db.insert(DATABASE_TABLE, null, initialValues);
83 }
84
85 //---deletes a particular title---
86 public boolean deleteTitle(long rowId)
87 {
88 return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
89 }
90
91 //---retrieves all the titles---
92 public Cursor getAllTitles()
93 {
94 return db.query(DATABASE_TABLE, new String[] {
95 KEY_ROWID,
96 KEY_ISBN,
97 KEY_TITLE,
98 KEY_PUBLISHER},
99 null,
100 null,
101 null,
102 null,
103 null);
104 }
105
106 //---retrieves a particular title---
107 public Cursor getTitle(long rowId) throws SQLException
108 {
109 Cursor mCursor =
110 db.query(true, DATABASE_TABLE, new String[] {
111 KEY_ROWID,
112 KEY_ISBN,
113 KEY_TITLE,
114 KEY_PUBLISHER
115 },
116 KEY_ROWID + "=" + rowId,
117 null,
118 null,
119 null,
120 null,
121 null);
122 if (mCursor != null) {
123 mCursor.moveToFirst();
124 }
125 return mCursor;
126 }
127
128 //---updates a title---
129 public boolean updateTitle(long rowId, String isbn,
130 String title, String publisher)
131 {
132 ContentValues args = new ContentValues();
133 args.put(KEY_ISBN, isbn);
134 args.put(KEY_TITLE, title);
135 args.put(KEY_PUBLISHER, publisher);
136 return db.update(DATABASE_TABLE, args,
137 KEY_ROWID + "=" + rowId, null) > 0;
138 }
139
140 public Cursor getAllTitles1()
141 {
142 return db.query(DATABASE_TABLE, new String[] {
143 KEY_ROWID,
144 KEY_ISBN,
145 KEY_TITLE
146 },
147 null,
148 null,
149 null,
150 null,
151 null);
152 }
153
154 public void addRow(String filename) {
155
156 // this is a key value pair holder used by android's SQLite functions
157 ContentValues values = new ContentValues();
158 values.put(KEY_ISBN, filename);
159
160 // ask the database object to insert the new data
161 try{db.insert(DATABASE_TABLE, null, values);}
162 catch(Exception e)
163 {
164 Log.e("DB ERROR", e.toString());
165 e.printStackTrace();
166 }
167 }
168
169
170
171 public void updateRow(long rowID, String filename)
172 {
173 // this is a key value pair holder used by android's SQLite functions
174 ContentValues values = new ContentValues();
175 values.put(KEY_ISBN, filename);
176 // ask the database object to update the database row of given rowID
177 try {db.update(DATABASE_TABLE, values, KEY_ISBN + "=" + rowID, null);}
178 catch (Exception e)
179 {
180 Log.e("DB Error", e.toString());
181 e.printStackTrace();
182 }
183 }
184
185
186}