import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
*
* @author antilaila8
*/
public class FriendSQLiteDBHelperContentValues extends SQLiteOpenHelper implements FriendDao{
public static final String DBNAME
= "antilaila8";
public static final int DBVERSION = 1;
public FriendSQLiteDBHelperContentValues
(Context context
) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql
= "create table friend("
+ "id integer primary key autoincrement,"
+ "name varchar(20) not null,"
+ "address varchar(30) null,"
+ "phone varchar(14) null)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < newVersion){
getWritableDatabase().execSQL("drop table if exists friend");
onCreate(db);
}
}
@Override
public void insert(Friend f) {
ContentValues values = new ContentValues();
values.put("id", f.getId());
values.put("name", f.getName());
values.put("address", f.getAddress());
values.put("phone", f.getPhone());
getWritableDatabase().insert("friend", "id", values);
}
@Override
public void update(Friend f) {
ContentValues values = new ContentValues();
values.put("id", f.getId());
values.put("name", f.getName());
values.put("address", f.getAddress());
values.put("phone", f.getPhone());
getWritableDatabase
().
update("friend", values,
"id = ?",
new String[]{String.
valueOf(f.
getId())});
}
@Override
public void delete(int id) {
ContentValues values = new ContentValues();
values.put("id", id);
getWritableDatabase
().
delete("friend",
"id = ?",
new String[]{String.
valueOf(id
)});
}
@Override
public void deleteAllFriends() {
getWritableDatabase().delete("friend", null, null);
ContentValues values = new ContentValues();
values.put("seq", 0);
getWritableDatabase
().
update("sqlite_sequence", values,
"name=?",
new String[]{"friend"});
}
@Override
public Friend getAFriendById(int id) {
Friend result = null;
Cursor cursor
= getReadableDatabase
().
query("friend",
null,
"id=?",
new String[]{String.
valueOf(id
)},
null,
null,
null);
if(cursor.moveToFirst())
result = new Friend(
cursor.getInt(0), //id
cursor.getString(1), //name
cursor.getString(2), //address
cursor.getString(3) //phone
);
return result;
}
@Override
public List<Friend> getAllFriends() {
List<Friend> result = new ArrayList<>();
Cursor cursor
= getReadableDatabase
().
query("friend",
null,
null,
null,
null,
null,
null);
while(cursor.moveToNext())
result.add(new Friend(
cursor.getInt(0), //id
cursor.getString(1), //name
cursor.getString(2), //address
cursor.getString(3) //phone
));
return result;
}
}
//model Friend
private int id;
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
this.name = name;
this.address = address;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
return name;
}
public void setName
(String name
) {
this.name = name;
}
return address;
}
public void setAddress
(String address
) {
this.address = address;
}
return phone;
}
public void setPhone
(String phone
) {
this.phone = phone;
}
@Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + this.id;
hash = 59 * hash + Objects.hashCode(this.name);
hash = 59 * hash + Objects.hashCode(this.address);
hash = 59 * hash + Objects.hashCode(this.phone);
return hash;
}
@Override
public boolean equals
(Object obj
) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Friend other = (Friend) obj;
if (this.id != other.id) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.address, other.address)) {
return false;
}
return Objects.equals(this.phone, other.phone);
}
@Override
return String.
format("%-3d %-20s %-30s %-14s", getId
(), getName
(), getAddress
(), getPhone
());
}
}
//inteface FriendDao
interface FriendDao{
void insert(Friend f);
void update(Friend f);
void delete(int id);
void deleteAllFriends();
Friend getAFriendById(int id);
List<Friend> getAllFriends();
}