· 9 years ago · Sep 28, 2016, 04:24 PM
1public class DatabaseClass extends SQLiteOpenHelper {
2
3 public static final String DATABASE_NAME = "unipi_login1.db";
4 public static final int DATABASE_VERSION = 1;
5
6 public SQLiteDatabase db;
7 private final Context context;
8
9 public DatabaseClass(Context context) {
10 // context: use to open or create the database
11 super(context, DATABASE_NAME, null, DATABASE_VERSION);
12 this.context = context;
13 }
14
15 @Override
16 public void onCreate(SQLiteDatabase db) {
17 db.execSQL("CREATE TABLE credentials (USERNAME TEXT PRIMARY KEY, PASSWORD TEXT NOT NULL)");
18 Log.w("DB", "credentials created");
19 db.execSQL("CREATE TABLE news ( title TEXT PRIMARY KEY, " +
20 " date TEXT NOT NULL , content_preview TEXT NOT NULL)");
21 Log.w("DB", "news created");
22 }
23
24 @Override
25 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
26 Log.w("DB", "Upgrading from version " + oldVersion + " to " + newVersion);
27 db.execSQL("DROP TABLE IF EXISTS credentials");
28 db.execSQL("DROP TABLE IF EXISTS news");
29 onCreate(db);
30 }
31
32 public DatabaseClass open() throws SQLException {
33 Log.w("DB", "Database opened");
34 db = this.getWritableDatabase();
35 return this;
36 }
37
38 public void close() {
39 Log.w("DB", "Database closed");
40 db.close();
41 }
42
43 public void insertEntry(String username, String password) {
44 ContentValues newValues = new ContentValues();
45 newValues.put("USERNAME", username);
46 newValues.put("PASSWORD", password);
47 Log.w("DB", newValues + " inserted");
48 db.insertWithOnConflict("credentials", null, newValues, SQLiteDatabase.CONFLICT_IGNORE);
49 }
50
51 public String getSingleEntry(String username) {
52 Cursor cursor = db.query("credentials", null, " USERNAME=?", new String[] {username}, null, null, null);
53 if (cursor.getCount() < 1) {
54 cursor.close();
55 return "NOT EXIST";
56 }
57 cursor.moveToFirst();
58 String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
59 cursor.close();
60 return password;
61 }
62
63
64 public ArrayList<News> getAllNews(){
65 ArrayList<News> newslist = new ArrayList<News>();
66 // String[] newsColumns = { "title", "date", "text"};
67 // grab all the information
68 Cursor cursor = db.query("news", null, null, null, null, null, null);
69 // loop through
70 for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()){
71 News news = cursorToNews(cursor);
72 newslist.add(news);
73 }
74 cursor.close();
75 return newslist;
76 }
77
78 private News cursorToNews(Cursor cursor){
79 News.Img img = News.Img.e2;
80 News news = new News(cursor.getString(0), cursor.getString(1), cursor.getString(2), img);
81 return news;
82 }
83
84 public SQLiteDatabase getDatabaseInstance() {
85 return db;
86 }
87
88 public void insertNews(String title, String date, String content) {
89 ContentValues newValues = new ContentValues();
90 newValues.put("NEWS_TITLE", title);
91 newValues.put("NEWS_DATE", date);
92 newValues.put("NEWS_TITLE", content);
93 Log.w("DB", newValues + " inserted");
94 db.insertWithOnConflict("news", null, newValues, SQLiteDatabase.CONFLICT_IGNORE);
95 }
96
97 /* public int deleteEntry(String username) {
98 Log.w("DB", username + " deleted");
99 return db.delete(TABLE_NAME, "USERNAME=?", new String[] {username});
100 }*/
101
102 /* public void updateEntry(String username, String password) {
103 Log.w("DB", "Entry Updated");
104 ContentValues updatedValues = new ContentValues();
105 updatedValues.put("USERNAME", username);
106 updatedValues.put("PASSWORD", password);
107 db.update(TABLE_NAME, updatedValues, "USERNAME=?", new String[] { username });
108 }
109*/
110
111
112}