-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdatabase.java
93 lines (70 loc) · 2.63 KB
/
database.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class sharedPreference {
}
/////////////////// Database
EditText eid, ename;
Button ins, del, dis;
TextView res;
MyDbHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eid = findViewById(R.id.idBox);
ename = findViewById(R.id.nameBox);
ins = findViewById(R.id.insertBtn);
del = findViewById(R.id.deleteBtn);
dis = findViewById(R.id.displayBtn);
res = findViewById(R.id.result);
db = new MyDbHelper(getApplicationContext(), "students", null, 1);
}
public void add(View view) {
String sid = eid.getText().toString();
String sname = ename.getText().toString();
db.insertRecord(sid, sname);
Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_LONG).show();
}
public void delete(View view) {
String sid = eid.getText().toString();
db.deleteRecord(sid);
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_LONG).show();
}
public void display(View view) {
String tabledata = db.displayRecord();
res.setText(tabledata);
}
}
//// Class
public class MyDbHelper extends SQLiteOpenHelper {
public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table students (stuId varChar(10),stuname varChar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void insertRecord(String sid,String sname){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("insert into students values (?,?)",new String[]{sid,sname});
// db.close();
}
public void deleteRecord(String sid){
SQLiteDatabase db = this.getWritableDatabase();
db.delete("students","stuId=?",new String[]{sid});
}
public String displayRecord(){
String tdata = "";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from students",null);
while (cursor.moveToNext()) {
String i = cursor.getString(0);
String n = cursor.getString(1);
tdata += i+":"+n+"\n";
}
// db.close();
return tdata;
}
}