Untitled
unknown
java
4 years ago
4.0 kB
13
Indexable
package com.example.android.myaddressbook.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.android.myaddressbook.model.addressbook.AddressBook;
import java.util.Vector;
public class DBHandler extends SQLiteOpenHelper {
// Setting DB
private final static String DB_NAME = "my_address_book";
private final static int DB_VERSION = 1;
// Setting table
private static final String TABLE_NAME = "address_book";
private static final String COLUMN_NAME_ADDRESS_BOOK_ID = "address_book_id";
private static final String COLUMN_NAME_FIRST_NAME = "first_name";
private static final String COLUMN_NAME_LAST_NAME = "last_name";
private static final String COLUMN_NAME_CITY = "city";
private static final String COLUMN_NAME_STATE = "state";
private static final String COLUMN_NAME_PHONE = "phone";
private static final String COLUMN_NAME_EMAIL = "email";
private static final String COLUMN_NAME_PICTURE= "picture";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_NAME_ADDRESS_BOOK_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME_FIRST_NAME + " TEXT," +
COLUMN_NAME_LAST_NAME + " TEXT," +
COLUMN_NAME_CITY + " TEXT," +
COLUMN_NAME_STATE + " TEXT," +
COLUMN_NAME_PHONE + " TEXT," +
COLUMN_NAME_EMAIL + " TEXT," +
COLUMN_NAME_PICTURE + " TEXT" +
")";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
public DBHandler(@Nullable Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
public void createAddressBook(AddressBook addressBook) {
SQLiteDatabase db = getWritableDatabase();
/**
* INSERT INTO TABLE (col) VALUES (val) <= execSQL
*/
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME_FIRST_NAME, addressBook.getFirstName());
cv.put(COLUMN_NAME_LAST_NAME, addressBook.getLastName());
cv.put(COLUMN_NAME_CITY, addressBook.getCity());
cv.put(COLUMN_NAME_STATE, addressBook.getState());
cv.put(COLUMN_NAME_PHONE, addressBook.getPhone());
cv.put(COLUMN_NAME_EMAIL, addressBook.getEmail());
cv.put(COLUMN_NAME_PICTURE, addressBook.getPicture());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public Vector<AddressBook> getAddressBook() {
SQLiteDatabase db = this.getWritableDatabase();
Vector<AddressBook> addressBooks = new Vector<>();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
AddressBook addressBook = new AddressBook(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5),
cursor.getString(6),
cursor.getString(7)
);
addressBooks.add(addressBook);
} while (cursor.moveToNext());
return addressBooks;
}
return null;
}
}Editor is loading...