-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbHelper.java
More file actions
147 lines (101 loc) · 3.6 KB
/
DbHelper.java
File metadata and controls
147 lines (101 loc) · 3.6 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.example.b10z.passwordmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.EOFException;
/**
* Created by b10z on 11/18/2017.
*/
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "storage.db";
private static final String TABLE_NAME = "users_table";
public DbHelper(Context context) {
super(context, DATABASENAME, null, 2);
}
//default android methods
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"create table users_table " +
"(Id INTEGER PRIMARY KEY AUTOINCREMENT, Username TEXT NOT NULL,Password TEXT NOT NULL, Data TEXT)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users_table");
onCreate(db);
}
public boolean Insert_db(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Username", username);
contentValues.put("Password", password);
db.insert("users_table", null, contentValues);
db.close();
return true;
}
//checking if the name exist in the DB
public boolean nameCheck(String name){
String selectQuery = "SELECT * FROM " + TABLE_NAME+" WHERE Username= '"+name+"' ;";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount() <= 0){
cursor.close();
db.close();
return false;
}
cursor.close();
db.close();
return true;
}
// checking if password is correct
public boolean AuthCheck(String username, String password) {
SQLiteDatabase db = getReadableDatabase();
Cursor mCursor;
try {
mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE Username= '" + username.trim() + "' AND Password= '" + password.trim() + "';", null);
} catch (Exception e) {
db.close();
return false;
}
mCursor.moveToFirst();
if (mCursor != null) {
if (mCursor.getCount() > 0) {
mCursor.close();
db.close();
return true;
} else
mCursor.close();
db.close();
return false;
} else {
mCursor.close();
db.close();
return false;
}
}
//data registration
public boolean Data_Register(String usernme, String data) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Data", data);
db.update(TABLE_NAME, contentValues,"Username='"+usernme+"';",null);
db.close();
return true;
}
//get data from DB
public String getData(String username){
String selectQuery = "SELECT Data FROM " + TABLE_NAME+" WHERE Username= '"+username+"';";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
String data=null;
if( cursor != null && cursor.moveToFirst() ){
data = cursor.getString( cursor.getColumnIndex("Data") );
}
cursor.close();
db.close();
return data;
}
}