×

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