· 8 years ago · Apr 19, 2018, 07:14 AM
1package com.example.admin.database;
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.util.Log;
10
11/**
12 * Created by admin on 19/4/2561.
13 */
14
15public class DBAdapter {
16 static final String KEY_ROWID = "_id";
17 static final String KEY_NAME = "name";
18 static final String KEY_EMAIL = "email";
19 static final String TAG = "DBAdapter";
20 static final String DATABASE_NAME = "MyDB";
21 static final String DATABASE_TABLE = "contacts";
22 static final int DATABASE_VERSION = 1;
23 static final String DATABASE_CREATE =
24 "create table contacts (_id integer primary key autoincrement, "
25 + "name text not null, email text not null);";
26 final Context context;
27 DatabaseHelper DBHelper;
28 SQLiteDatabase db;
29 public DBAdapter(Context ctx)
30 {
31 this.context = ctx;
32 DBHelper = new DatabaseHelper(context);
33 }
34
35 private static class DatabaseHelper extends SQLiteOpenHelper
36 {
37 DatabaseHelper(Context context)
38 {
39 super(context, DATABASE_NAME, null, DATABASE_VERSION);
40 }
41 @Override
42 public void onCreate(SQLiteDatabase db)
43 {
44 try {
45 db.execSQL(DATABASE_CREATE);
46 } catch (SQLException e) {
47 e.printStackTrace();
48 }
49 }
50 @Override
51 public void onUpgrade(SQLiteDatabase db, int oldVersion, int
52 newVersion)
53 {
54 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
55 + newVersion + ", which will destroy all old data");
56 db.execSQL("DROP TABLE IF EXISTS contacts");
57 onCreate(db);
58 }
59 }
60 //---opens the database---
61 public DBAdapter open() throws SQLException{
62 db = DBHelper.getWritableDatabase();
63 return this;
64 }
65
66 //---closes the database---
67 public void close()
68 {
69 DBHelper.close();
70 }
71 //---insert a contact into the database---
72 public long insertContact(String name, String email)
73 {
74 ContentValues initialValues = new ContentValues();
75 initialValues.put(KEY_NAME, name);
76 initialValues.put(KEY_EMAIL, email);
77 return db.insert(DATABASE_TABLE, null, initialValues);
78 }
79 //---deletes a particular contact---
80 public boolean deleteContact(long rowId)
81 {
82 return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)
83 > 0;
84 }
85 //---retrieves all the contacts---
86 public Cursor getAllContacts()
87 {
88 return db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
89 KEY_NAME,
90 KEY_EMAIL}, null, null, null, null, null);
91 }
92
93 //---retrieves a particular contact---
94 public Cursor getContact(long rowId) throws SQLException
95 {
96 Cursor mCursor =
97 db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
98 KEY_NAME, KEY_EMAIL},
99 KEY_ROWID + "=" + rowId, null,
100 null, null, null, null);
101 if (mCursor != null) {
102 mCursor.moveToFirst();
103 }
104 return mCursor;
105 }
106 //---updates a contact---
107 public boolean updateContact(long rowId, String name, String email)
108 {
109 ContentValues args = new ContentValues();
110 args.put(KEY_NAME, name);
111 args.put(KEY_EMAIL, email);
112 return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId,
113 null) >
114 0;
115 }
116 public boolean deleteAll(){
117 return db.delete(DATABASE_TABLE, "1", null) > 0;
118 }
119}