· 8 years ago · Aug 02, 2018, 05:46 PM
1package sg.edu.tp.cinemah;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9public class DatabaseHelper extends SQLiteOpenHelper{
10
11 private static final int DATABASE_VERSION = 1;
12 private static final String DATABASE_NAME = "contacts.db";
13 private static final String TABLE_NAME = "contacts";
14 private static final String COLUMN_ID = "id";
15 private static final String COLUMN_NAME = "name";
16 private static final String COLUMN_PASS = "pass";
17 SQLiteDatabase db;
18
19 private static final String TABLE_CREATE = "create table contacts (id integer primary key, not_null, auto_increment, " + "name text not null , pass text not null);";
20
21 public DatabaseHelper(Context context)
22 {
23 super(context , DATABASE_NAME , null , DATABASE_VERSION);
24 }
25
26 @Override
27 public void onCreate(SQLiteDatabase db) {
28 db.execSQL(TABLE_CREATE);
29 this.db = db;
30 }
31
32 /*public void insertContact(Contact c)
33 {
34 db = this.getWritableDatabase();
35 ContentValues values = new ContentValues();
36 values.put(COLUMN_NAME, c.getName());
37 values.put(COLUMN_PASS, c.getPassword());
38
39 db.insert(TABLE_NAME , null , values);
40 db.close();
41 }*/
42
43 public String searchPass(String username)
44 {
45 db = this.getReadableDatabase();
46 String query = "select username, pass from "+TABLE_NAME;
47 Cursor cursor = db.rawQuery(query , null);
48 String a, b;
49 b = "not found";
50
51 if(cursor.moveToFirst())
52 {
53 do{
54 a = cursor.getString(0);
55
56
57 if(a.equals(username))
58 {
59 b = cursor.getString(1);
60 break;
61 }
62 }
63 while (cursor.moveToNext());
64 }
65
66 return b;
67 }
68
69 @Override
70 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
71 String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
72 db.execSQL(query);
73 this.onCreate(db);
74 }
75
76 public void insertContact(Contact c) {
77 db = this.getWritableDatabase();
78 ContentValues values = new ContentValues();
79 values.put(COLUMN_NAME, c.getName());
80 values.put(COLUMN_PASS, c.getPassword());
81
82 db.insert(TABLE_NAME , null , values);
83 db.close();
84 }
85}