I'm trying to make an application that allow users to add contacts, then preview them. I'm using SQLite to save the data locally. I have created an SQLHelper for my database, but it is showing any data when I try to SELECT all.
SQLHelper
package com.freelancer.camelo.contacts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyContactsDb.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_SURNAME = "surname";
public static final String CONTACTS_COLUMN_ADDRESS = "address";
public static final String CONTACTS_COLUMN_POSTAL = "postal";
public static final String CONTACTS_COLUMN_PHONE = "phone";
private HashMap hp;
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,surname text,phone text, address text,postal text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String name, String surname, String phone, String address,String postal) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CONTACTS_COLUMN_NAME, name);
contentValues.put(CONTACTS_COLUMN_SURNAME, surname);
contentValues.put(CONTACTS_COLUMN_PHONE, phone);
contentValues.put(CONTACTS_COLUMN_ADDRESS, address);
contentValues.put(CONTACTS_COLUMN_POSTAL, postal);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String surname, String phone, String address,String postal) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CONTACTS_COLUMN_NAME, name);
contentValues.put(CONTACTS_COLUMN_SURNAME, surname);
contentValues.put(CONTACTS_COLUMN_PHONE, phone);
contentValues.put(CONTACTS_COLUMN_ADDRESS, address);
contentValues.put(CONTACTS_COLUMN_POSTAL, postal);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllContacts() {
ArrayList<String> array_list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM "+CONTACTS_TABLE_NAME;
Cursor res = db.rawQuery( selectQuery, null );
res.moveToFirst();
while(!res.isAfterLast()){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
I'm using this bunch of code to insert into the db and it actually toasts "added"
if (mydb.insertContact("one", "two", "three", "foir", "five")) {
Toast.makeText(AddContactActivity.this, "added", Toast.LENGTH_SHORT).show();
}
This is when I'm trying to retrieve data
ArrayList<String> contactsArrayList = mydb.getAllContacts();
if (contactsArrayList.size() == 0) {
Toast.makeText(this, "DB is empty", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < contactsArrayList.size(); i++) {
Toast.makeText(this, contactsArrayList.get(i).toString(), Toast.LENGTH_SHORT).show();
}
}
This simply keeps toasting "DB is empty" everytime I run the activity.