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 FriendSQLiteDBHelper extends SQLiteOpenHelper implements FriendDao {
public static final String DBNAME
= "antilaila8";
public static final int DBVERSION = 1;
public FriendSQLiteDBHelper
(Context context
) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql
= "create table if not exists friend("
+ "id integer primary key auto_increment,"
+ "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) {
db.execSQL("drop table if exists friend");
onCreate(db);
}
}
@Override
public void insert(Friend f) {
String sql
= "insert into friend values(?,?,?,?)";
getWritableDatabase
().
execSQL(sql,
new Object[]{
null,
f.getName(),
f.getAddress(),
f.getPhone()
});
}
@Override
public void update(Friend f) {
String sql
= "update friend set name=?, address=?, phone=? where id=?";
getWritableDatabase
().
execSQL(sql,
new Object[]{
f.getName(),
f.getAddress(),
f.getPhone(),
f.getId()
});
}
@Override
public void delete(int id) {
String sql
= "delete from friend";
getWritableDatabase
().
execSQL(sql,
new Object[]{id
});
}
@Override
public void deleteAllFriends() {
getWritableDatabase().execSQL("delete from friend");
getWritableDatabase().execSQL("update sqlite_sequence set seq = 0 where name = friend");
}
@Override
public Friend getAFriendById(int id) {
Friend result = null;
String sql
= "select * from friend where id=?";
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<>();
String sql
= "select * from friend";
Cursor cursor
= getReadableDatabase
().
rawQuery(sql,
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();
}