-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseHelper.java
75 lines (67 loc) · 2.32 KB
/
DatabaseHelper.java
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
//Nayan Pasari
//ID: 111868106
package com.example.hackmatcher;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "register.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
/*
onCreate: Creates a table
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table user(username text primary key,password text)");
}
/*
onUpgrade: This will be used when there's any changes to the version.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" drop table if exists user");
}
/*
Inserts all the values to the table or content values
*/
public boolean insert(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username",username);
contentValues.put("password",password);
long res = db.insert("user", null, contentValues);
if (res == -1) {
return false;
} else {
return true;
}
}
/*
Checks or reads from the table if there's a valid username.
*/
public boolean checkUser(String username) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where username=?", new String[]{username});
if (cursor.getCount() > 0) {
return false;
} else {
return true;
}
}
/*
Checks or reads from the table if there's a valid username and password.
*/
public boolean checkUserPass(String username, String password) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where username=? and password=?", new String[]{username, password});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
}