· 9 years ago · Dec 12, 2016, 10:22 AM
1private static final int DATABASE_VERSION = 1;
2private static final String DATABASE_NAME = "contacts.db";
3private static final String TABLE_NAME = "contacts";
4private static final String COLUMN_ID = "id";
5private static final String COLUMN_NAME = "name";
6private static final String COLUMN_UNAME = "uname";
7private static final String COLUMN_PASS = "pass";
8private static final String TABLE_DIARY = "DIARY";
9private static final String KEY_ID = "ID";
10private static final String KEY_TITLE = "TITLE";
11private static final String KEY_CONTENT = "CONTENT";
12SQLiteDatabase db;
13
14//private static final String DB_NAME = "diaryManager",
15
16private static final String TABLE_CREATE = "CREATE TABLE contacts (id integer primary key not null, " +
17 "name text not null, uname text not null, pass text not null)";
18private static final String DIARY_CREATE = "CREATE TABLE DIARY (ID integer primary key not null, " +
19 "TITLE text not null, CONTENT text not null)";
20
21public DatabaseHelper(Context context){
22 super(context, DATABASE_NAME, null, DATABASE_VERSION);
23}
24
25public void insertContact(Contact c){
26 db = this.getWritableDatabase();
27 ContentValues values = new ContentValues();
28
29 String query = "SELECT * FROM contacts";
30 Cursor cursor = db.rawQuery(query, null);
31 int count = cursor.getCount();
32
33 values.put(COLUMN_ID, count);
34 values.put(COLUMN_NAME, c.getName());
35 values.put(COLUMN_UNAME, c.getUname());
36 values.put(COLUMN_PASS, c.getPass());
37
38 db.insert(TABLE_NAME, null, values);
39 db.close();
40}
41
42public void createDiary(diaryList dlist) {
43 db = this.getWritableDatabase();
44 ContentValues values = new ContentValues();
45
46 String query = "SELECT * FROM DIARY";
47 Cursor cursor = db.rawQuery(query, null);
48 int count = cursor.getCount();
49
50 values.put(KEY_ID, count);
51 values.put(KEY_TITLE, dlist.getTitle());
52 values.put(KEY_CONTENT, dlist.getContent());
53
54 db.insert(TABLE_DIARY, null, values);
55 db.close();
56}
57
58public String searchPass(String uname){
59 db = this.getReadableDatabase();
60 String query = "SELECT uname, pass FROM " + TABLE_NAME;
61 Cursor cursor = db.rawQuery(query, null);
62 String a, b;
63 b = "not found";
64 if(cursor.moveToFirst())
65 {
66 do {
67 a = cursor.getString(0);
68 if(a.equals(uname))
69 {
70 b = cursor.getString(1);
71 break;
72 }
73 }while (cursor.moveToNext());
74 }
75 return b;
76}
77
78public boolean deleteDiary(diaryList dList) {
79 db = this.getWritableDatabase();
80
81 try {
82 db.delete(TABLE_DIARY, KEY_ID + "=?", new String[]{String.valueOf(dList.getID())});
83 db.close();
84 return true;
85 } catch (Exception e) {
86 e.printStackTrace();
87 return false;
88 }
89}
90
91public int getDiaryCount() {
92 db = this.getWritableDatabase();
93 Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_DIARY, null);
94 int count = cursor.getCount();
95 db.close();
96 cursor.close();
97 return count;
98}
99
100public List<diaryList> getAllDiary() {
101 List<diaryList> lists = new ArrayList<diaryList>();
102 diaryList dList;
103 db = this.getWritableDatabase();
104
105 Cursor cursor = null;
106 try {
107 cursor = db.rawQuery("SELECT * FROM " + TABLE_DIARY, null);
108 if (cursor.moveToFirst()) {
109 do {
110 lists.add(new diaryList(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)));
111 } while (cursor.moveToNext());
112 }
113 } catch (Exception e) {
114 e.printStackTrace();
115 }
116 cursor.close();
117 db.close();
118 return lists;
119}
120
121@Override
122public void onCreate(SQLiteDatabase db) {
123 db.execSQL(TABLE_CREATE);
124 this.db = db;
125 db.execSQL(DIARY_CREATE);
126 this.db = db;
127}
128
129@Override
130public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
131 String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
132 db.execSQL(query);
133 this.onCreate(db);
134 query = "DROP TABLE IF EXISTS " + TABLE_DIARY;
135 db.execSQL(query);
136 this.onCreate(db);
137}