It is advised to create a Contact Class to handle the data easily first. However, without that, here is how I would accomplish the database entry task:
Custom SQLiteOpenHelper Class:
public class DBHandlerTest extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "database.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CONTACTS = "contacts";
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_PH_NO = "phone_number";
public static final String KEY_MACID = "macid";
public static final String CREATE_CONTACTS_TABLE =
"CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT(255) NOT NULL, "
+ KEY_PH_NO + " TEXT(255) NOT NULL, "
+ KEY_MACID + " TEXT(255) NOT NULL" + ")";
public DBHandlerTest(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
//re-create db
onCreate(db);
}
public void insertContact(String name, String phoneNumber, String macID) {
//create db instance
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contactValues = new ContentValues();
//get the values
contactValues.put(KEY_NAME, name);
contactValues.put(KEY_PH_NO, phoneNumber);
contactValues.put(KEY_MACID, macID);
//insert row into db
db.insert(TABLE_CONTACTS, null, contactValues);
db.close();
}
}
Implementing the method:
DBHandlerTest db = new DBHandlerTest(this);
db.insertContact("test@test.com", "0800-123456", "test_id");
db.close();