· 8 years ago · Aug 03, 2018, 08:44 AM
1SQLiteDatabase getWritableDatabase() not working
2getAllContacts()
3
4SQLiteDatabase db = this.getWritableDatabase();
5
6package com.lifeApp;
7
8import android.content.Context;
9import android.database.sqlite.SQLiteDatabase;
10import android.database.sqlite.SQLiteOpenHelper;
11
12import java.util.ArrayList;
13import java.util.List;
14
15import android.content.ContentValues;
16import android.database.Cursor;
17
18public class DatabaseHandler extends SQLiteOpenHelper {
19
20 // All Static variables
21 // Database Version
22 private static final int DATABASE_VERSION = 1;
23
24 // Database Name
25 private static final String DATABASE_NAME = "contactsManager";
26
27 // Contacts table name
28 private static final String TABLE_CONTACTS = "contacts";
29
30 // Contacts Table Columns names
31 private static final String KEY_ID = "id";
32 private static final String FIRSTNAME = "firstname";
33 private static final String LASTNAME = "lastname";
34 private static final String PASSWORD = "password";
35 private static final String EMAIL = "email";
36
37 private static final String KEY_PH_NO = "phone_number";
38
39private static final String KEY_NAME = null;
40
41 public DatabaseHandler(Context context) {
42 super(context, DATABASE_NAME, null, DATABASE_VERSION);
43 }
44
45 // Creating Tables
46 @Override
47 public void onCreate(SQLiteDatabase db) {
48
49 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
50 + KEY_ID + " INTEGER PRIMARY KEY,"
51 + KEY_NAME + " TEXT,"
52 + FIRSTNAME + " TEXT,"
53 + LASTNAME + " TEXT,"
54 + PASSWORD + " TEXT"
55 + EMAIL + " TEXT"
56 + KEY_PH_NO + " TEXT" + ")";
57 db.execSQL(CREATE_CONTACTS_TABLE);
58 }
59
60 // Upgrading database
61 @Override
62 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
63 // Drop older table if existed
64 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
65
66 // Create tables again
67 onCreate(db);
68 }
69
70 /**
71 * All CRUD(Create, Read, Update, Delete) Operations
72 */
73
74 // Adding new contact
75 void addContact(Contact contact) {
76 SQLiteDatabase db = this.getWritableDatabase();
77
78 ContentValues values = new ContentValues();
79 values.put(KEY_NAME, contact.getName()); // Contact Name
80 values.put(FIRSTNAME, contact.getFirstname());
81 values.put(LASTNAME, contact.getLastname());
82 values.put(EMAIL, contact.getEmail());
83 values.put(PASSWORD, contact.getPassword());
84 values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
85
86 // Inserting Row
87 db.insert(TABLE_CONTACTS, null, values);
88 db.close(); // Closing database connection
89 }
90
91 // Getting single contact
92 Contact getContact(int id) {
93 SQLiteDatabase db = this.getReadableDatabase();
94
95 Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
96 KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
97 new String[] { String.valueOf(id) },null,null,null,null);
98 if (cursor != null)
99 cursor.moveToFirst();
100
101 Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
102 cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6));
103 // return contact
104 return contact;
105 }
106
107 // Getting All Contacts
108 public List<Contact> getAllContacts() {
109 List<Contact> contactList = new ArrayList<Contact>();
110 // Select All Query
111 String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
112
113 SQLiteDatabase db = this.getWritableDatabase();
114 Cursor cursor = db.rawQuery(selectQuery, null);
115
116 // looping through all rows and adding to list
117 if (cursor.moveToFirst()) {
118 do {
119 Contact contact = new Contact();
120 contact.setID(Integer.parseInt(cursor.getString(0)));
121 contact.setFirstname(cursor.getString(1));
122 contact.setLastname(cursor.getString(2));
123 contact.setPassword(cursor.getString(3));
124 contact.setEmail(cursor.getString(4));
125 // Adding contact to list
126 contactList.add(contact);
127 } while (cursor.moveToNext());
128 }
129
130 // return contact list
131 return contactList;
132 }
133
134 // Updating single contact
135 public int updateContact(Contact contact) {
136 SQLiteDatabase db = this.getWritableDatabase();
137
138 ContentValues values = new ContentValues();
139 values.put(KEY_NAME, contact.getName());
140 values.put(FIRSTNAME, contact.getFirstname());
141 values.put(LASTNAME, contact.getLastname());
142 values.put(PASSWORD, contact.getPassword());
143 values.put(EMAIL, contact.getEmail());
144 values.put(KEY_PH_NO, contact.getPhoneNumber());
145
146 // updating row
147 return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
148 new String[] { String.valueOf(contact.getID()) });
149 }
150
151 // Deleting single contact
152 public void deleteContact(Contact contact) {
153 SQLiteDatabase db = this.getWritableDatabase();
154 db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
155 new String[] { String.valueOf(contact.getID()) });
156 db.close();
157 }
158
159 // Getting contacts Count
160 public int getContactsCount() {
161 String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
162 SQLiteDatabase db = this.getReadableDatabase();
163 Cursor cursor = db.rawQuery(countQuery, null);
164 cursor.close();
165
166 // return count
167 return cursor.getCount();
168 }
169
170}
171
172String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
173 + KEY_ID + " INTEGER PRIMARY KEY,"
174 + KEY_NAME + " TEXT,"
175 + FIRSTNAME + " TEXT,"
176 + LASTNAME + " TEXT,"
177 + PASSWORD + " TEXT" <==
178 + EMAIL + " TEXT" <==
179 + KEY_PH_NO + " TEXT" + ")";
180 db.execSQL(CREATE_CONTACTS_TABLE);