· 6 years ago · Sep 19, 2019, 11:08 AM
1package com.student.lecture9;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.widget.Toast;
10
11public class DBHelper extends SQLiteOpenHelper {
12
13 private static final String DATABASE_NAME = "stdb";
14 private static final int DATABASE_VERSION = 3;
15 private static final String DATABASE_CREATE = "create table student (id integer primary key autoincrement, fullname varchar(100) not null, birthday varchar(100) not null, gender varchar(100) not null, telephone varchar(100), country varchar(100) not null, city varchar(100) not null);";
16 private static final String DATABASE_TABLE = "student";
17 private static final String KEY_ROWID = "id";
18 private static final String KEY_NAME = "fullname";
19 private static final String KEY_BDAY = "birthday";
20 private static final String KEY_GENDER = "gender";
21 private static final String KEY_TELE = "telephone";
22 private static final String KEY_CTR = "country";
23 private static final String KEY_CTY = "city";
24
25 private Context context = null;
26 private SQLiteDatabase db;
27
28 public DBHelper(Context context){
29 super(context, DATABASE_NAME, null, DATABASE_VERSION);
30 this.context = context;
31 }
32
33 @Override
34 public void onCreate(SQLiteDatabase db) {
35 try {
36 db.execSQL(DATABASE_CREATE);
37 }catch (Exception e){
38 e.printStackTrace();
39 }
40 }
41
42 @Override
43 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44 db.execSQL("DROP TABLE IF EXISTS contacts ");
45 onCreate(db);
46 }
47
48 public DBHelper open() throws SQLException {
49 db = getWritableDatabase();
50 return this;
51 }
52
53 public void close(){
54 db.close();
55 }
56
57 public long insertStudent(String name, String bday, String gender, String tele, String country, String city) {
58 ContentValues initialValues = new ContentValues();
59 initialValues.put(KEY_NAME, name);
60 initialValues.put(KEY_BDAY, bday);
61 initialValues.put(KEY_GENDER, gender);
62 initialValues.put(KEY_TELE, tele);
63 initialValues.put(KEY_CTR, country);
64 initialValues.put(KEY_CTY, city);
65 return db.insert(DATABASE_TABLE, null, initialValues);
66 }
67
68 public long deleteStudent(int id) {
69 return db.delete(DATABASE_TABLE, KEY_ROWID+"="+id, null);
70 }
71
72 public boolean updateStudent(String name, String bday, String gender, String tele, String country, String city, String where, String[] whereValues){
73 ContentValues args = new ContentValues();
74 args.put(KEY_NAME, name);
75 args.put(KEY_BDAY, bday);
76 args.put(KEY_GENDER, gender);
77 args.put(KEY_TELE, tele);
78 args.put(KEY_CTR, country);
79 args.put(KEY_CTY, city);
80 return db.update(DATABASE_TABLE, args, where, whereValues) > 0;
81 }
82
83 public Cursor getAllStudent(){
84 return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_GENDER}, null, null, null, null, null);
85 }
86
87 public Cursor getDetailStudent(String key, String[] where) throws SQLException{
88 Cursor mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_BDAY, KEY_GENDER, KEY_TELE, KEY_CTR, KEY_CTY}, key, where, null, null, null);
89 return mCursor;
90 }
91}