· 8 years ago · Dec 16, 2017, 05:56 PM
1EditText reName, rePin;
2 Button reRegister;
3 DatabaseHelper helper = new DatabaseHelper(this);
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_sign_up);
9
10 reName = (EditText)findViewById(R.id.reImie);
11 rePin = (EditText)findViewById(R.id.rePin);
12 reRegister = (Button)findViewById(R.id.reRejestruj);
13 reRegister.setOnClickListener(new View.OnClickListener() {
14 @Override
15 public void onClick(View view) {
16
17 String Imie = reName.getText().toString();
18 String Haslo = rePin.getText().toString();
19
20 Contacts c =new Contacts();
21 c.setName(Imie);
22 c.setPass(Haslo);
23
24 helper.insertContacts(c);
25
26
27 Intent intent = new Intent(SignUp.this, MainActivity.class);
28 startActivity(intent);
29 }
30 });
31 }
32
33public class DatabaseHelper extends SQLiteOpenHelper {
34 private static final int DATABASE_VERSION = 1;
35 private static final String DATABASE_NAME = "contacts.db";
36 private static final String TABLE_NAME = "contacts";
37 private static final String COLUMN_ID = "id";
38 private static final String COLUMN_NAME = "name";
39 private static final String COLUMN_PASS = "pass";
40 SQLiteDatabase db;
41
42 private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
43 "name text not null, pass text not null);";
44
45 public DatabaseHelper(Context context){
46 super(context, DATABASE_NAME, null, 1);
47
48 }
49
50 public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
51 super(context, name, factory, version);
52 }
53
54 @Override
55 public void onCreate(SQLiteDatabase sqLiteDatabase) {
56 db.execSQL(TABLE_CREATE);
57 this.db = db;
58
59 }
60
61 public void insertContacts(Contacts c){
62 db = this.getWritableDatabase();
63 ContentValues values = new ContentValues();
64 String query = "select + from contacts";
65 Cursor cursor = db.rawQuery(query, null);
66 int count = cursor.getCount();
67 values.put(COLUMN_ID, count);
68 values.put(COLUMN_NAME, c.getName());
69 values.put(COLUMN_PASS, c.getPass());
70 db.insert(TABLE_NAME, null, values);
71 db.close();
72 }
73
74 public String searchPass(String name) {
75 db = this.getReadableDatabase();
76 String query = "select * from "+TABLE_NAME;
77 Cursor cursor = db.rawQuery(query, null);
78 String a,b;
79 b = "not found";
80 if (cursor.moveToFirst()){
81 do {
82 a =cursor.getString(2);
83 if (a.equals(name)){
84 b= cursor.getString(3);
85 break;
86 }
87
88 }
89 while (cursor.moveToNext());
90 }
91 return b;
92 }
93
94
95 @Override
96 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
97 String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
98 db.execSQL(query);
99 this.onCreate(db);
100
101 }
102}