×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: anti laila
Added: May 26, 2022 2:59 AM
Modified: May 26, 2022 3:30 AM
Views: 11
  1. import android.content.Context;
  2. import android.database.Cursor;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import java.io.Serializable;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Objects;
  9.  
  10. /**
  11.  *
  12.  * @author antilaila8
  13.  */
  14. public class FriendSQLiteDBHelper extends SQLiteOpenHelper implements FriendDao {
  15.     public static final String DBNAME = "antilaila8";
  16.     public static final int DBVERSION = 1;
  17.  
  18.     public FriendSQLiteDBHelper(Context context) {
  19.         super(context, DBNAME, null, DBVERSION);
  20.     }
  21.  
  22.     @Override
  23.     public void onCreate(SQLiteDatabase db) {
  24.         String sql = "create table if not exists friend("
  25.                 + "id integer primary key auto_increment,"
  26.                 + "name varchar(20) not null,"
  27.                 + "address varchar(30) null,"
  28.                 + "phone varchar(14) null)";
  29.         db.execSQL(sql);
  30.     }
  31.  
  32.     @Override
  33.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34.         if (oldVersion < newVersion) {
  35.             db.execSQL("drop table if exists friend");
  36.             onCreate(db);
  37.         }
  38.     }
  39.  
  40.     @Override
  41.     public void insert(Friend f) {
  42.         String sql = "insert into friend values(?,?,?,?)";
  43.         getWritableDatabase().execSQL(sql, new Object[]{
  44.             null,
  45.             f.getName(),
  46.             f.getAddress(),
  47.             f.getPhone()
  48.         });
  49.     }
  50.  
  51.     @Override
  52.     public void update(Friend f) {
  53.         String sql = "update friend set name=?, address=?, phone=? where id=?";
  54.         getWritableDatabase().execSQL(sql, new Object[]{
  55.             f.getName(),
  56.             f.getAddress(),
  57.             f.getPhone(),
  58.             f.getId()
  59.         });
  60.     }
  61.  
  62.     @Override
  63.     public void delete(int id) {
  64.         String sql = "delete from friend";
  65.         getWritableDatabase().execSQL(sql, new Object[]{id});
  66.     }
  67.  
  68.     @Override
  69.     public void deleteAllFriends() {
  70.         getWritableDatabase().execSQL("delete from friend");
  71.         getWritableDatabase().execSQL("update sqlite_sequence set seq = 0 where name = friend");
  72.     }
  73.  
  74.     @Override
  75.     public Friend getAFriendById(int id) {
  76.         Friend result = null;
  77.         String sql = "select * from friend where id=?";
  78.         Cursor cursor = getReadableDatabase().rawQuery(sql, new String[]{String.valueOf(id)});
  79.         if (cursor.moveToFirst()) {
  80.             result = new Friend(
  81.                     cursor.getInt(0), //id
  82.                     cursor.getString(1), //name
  83.                     cursor.getString(2), //address
  84.                     cursor.getString(3) //phone
  85.             );
  86.         }
  87.         return result;
  88.     }
  89.  
  90.     @Override
  91.     public List<Friend> getAllFriends() {
  92.         List<Friend> result = new ArrayList<>();
  93.         String sql = "select * from friend";
  94.         Cursor cursor = getReadableDatabase().rawQuery(sql, null);
  95.         while (cursor.moveToNext()) {
  96.             result.add(new Friend(
  97.                     cursor.getInt(0), //id
  98.                     cursor.getString(1), //name
  99.                     cursor.getString(2), //address
  100.                     cursor.getString(3) //phone
  101.             ));
  102.         }
  103.  
  104.         return result;
  105.     }
  106.  
  107. }
  108.  
  109. //model Friend
  110. class Friend implements Serializable {
  111.  
  112.     private int id;
  113.     private String name;
  114.     private String address;
  115.     private String phone;
  116.  
  117.     public Friend(int id, String name, String address, String phone) {
  118.         this.id = id;
  119.         this.name = name;
  120.         this.address = address;
  121.         this.phone = phone;
  122.     }
  123.  
  124.     public Friend(String name, String address, String phone) {
  125.         this.name = name;
  126.         this.address = address;
  127.         this.phone = phone;
  128.     }
  129.  
  130.     public int getId() {
  131.         return id;
  132.     }
  133.  
  134.     public void setId(int id) {
  135.         this.id = id;
  136.     }
  137.  
  138.     public String getName() {
  139.         return name;
  140.     }
  141.  
  142.     public void setName(String name) {
  143.         this.name = name;
  144.     }
  145.  
  146.     public String getAddress() {
  147.         return address;
  148.     }
  149.  
  150.     public void setAddress(String address) {
  151.         this.address = address;
  152.     }
  153.  
  154.     public String getPhone() {
  155.         return phone;
  156.     }
  157.  
  158.     public void setPhone(String phone) {
  159.         this.phone = phone;
  160.     }
  161.  
  162.     @Override
  163.     public int hashCode() {
  164.         int hash = 3;
  165.         hash = 59 * hash + this.id;
  166.         hash = 59 * hash + Objects.hashCode(this.name);
  167.         hash = 59 * hash + Objects.hashCode(this.address);
  168.         hash = 59 * hash + Objects.hashCode(this.phone);
  169.         return hash;
  170.     }
  171.  
  172.     @Override
  173.     public boolean equals(Object obj) {
  174.         if (this == obj) {
  175.             return true;
  176.         }
  177.         if (obj == null) {
  178.             return false;
  179.         }
  180.         if (getClass() != obj.getClass()) {
  181.             return false;
  182.         }
  183.         final Friend other = (Friend) obj;
  184.         if (this.id != other.id) {
  185.             return false;
  186.         }
  187.         if (!Objects.equals(this.name, other.name)) {
  188.             return false;
  189.         }
  190.         if (!Objects.equals(this.address, other.address)) {
  191.             return false;
  192.         }
  193.         return Objects.equals(this.phone, other.phone);
  194.     }
  195.  
  196.     @Override
  197.     public String toString() {
  198.         return String.format("%-3d %-20s %-30s %-14s", getId(), getName(), getAddress(), getPhone());
  199.     }
  200. }
  201.  
  202. //inteface FriendDao
  203. interface FriendDao{
  204.     void insert(Friend f);
  205.     void update(Friend f);
  206.     void delete(int id);
  207.     void deleteAllFriends();
  208.     Friend getAFriendById(int id);
  209.     List<Friend> getAllFriends();
  210. }