MyinfoDAO

 avatar
hacker98
plain_text
9 months ago
4.3 kB
2
Indexable
DAO
package com.example.chatappdemo.DAO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.chatappdemo.DatabaseHelper;
import com.example.chatappdemo.model.Myinfo;
import com.example.chatappdemo.model.ReceiveRequest;

import java.util.ArrayList;
import java.util.List;

public class MyinfoDAO {
    private SQLiteDatabase db;
    private SQLiteOpenHelper dbHelper;

    // Constructor
    public MyinfoDAO(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // Mở kết nối
    private void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
    }

    // Đóng kết nối
    private void close() {
        if (db != null && db.isOpen()) {
            db.close();
        }
    }

    // Thêm dữ liệu vào bảng MyModel
    public long insertMyModel(Myinfo model) {
        open();
        ContentValues values = new ContentValues();
        values.put("myIP", model.getMyIP());
        values.put("name", model.getName());
        values.put("publicKey", model.getPublicKey());
        values.put("privateKey", model.getPrivateKey());
        long result = 0;
        try {
            result = db.insert("Myinfo", null, values);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.d("Socket", e.getMessage());
        }
        close();
        return result;
    }

    // Cập nhật dữ liệu trong bảng MyModel
    public int updateMyModel(Myinfo model) {
        open();
        ContentValues values = new ContentValues();
        values.put("myIP", model.getMyIP());
        values.put("name", model.getName());
        values.put("publicKey", model.getPublicKey());
        values.put("privateKey", model.getPrivateKey());

        int result = db.update("Myinfo", values, "myIP = ?", new String[]{model.getMyIP()});
        close();
        return result;
    }

    // Xóa dữ liệu theo myIP
    public int deleteMyModelByIP(String myIP) {
        open();
        int result = db.delete("Myinfo", "myIP = ?", new String[]{myIP});
        close();
        return result;
    }

    // Lấy dữ liệu theo myIP
    // Lấy dữ liệu từ bảng receive_request theo IP_request
    public Myinfo getInfoByIP(String MyIP) {
        Myinfo myinfo=null;
        open();

        Cursor cursor = db.query("Myinfo", null, "myIP = ?", new String[]{MyIP}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            myinfo = new Myinfo();
            myinfo.setMyIP(cursor.getString(cursor.getColumnIndexOrThrow("myIP")));
            myinfo.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
            myinfo.setPublicKey(cursor.getString(cursor.getColumnIndexOrThrow("publicKey")));
            myinfo.setPrivateKey(cursor.getString(cursor.getColumnIndexOrThrow("privateKey")));
        }
        if (cursor != null) {
            cursor.close();
        }
        close();
        return myinfo;
    }


    // Lấy toàn bộ dữ liệu từ bảng MyModel
    public List<Myinfo> getAllMyModels() {
        List<Myinfo> modelList = new ArrayList<>();
        open();
        Cursor cursor = db.query("Myinfo", null, null, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Myinfo model = new Myinfo();
                model.setMyIP(cursor.getString(cursor.getColumnIndexOrThrow("myIP")));
                model.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
                model.setPublicKey(cursor.getString(cursor.getColumnIndexOrThrow("publicKey")));
                model.setPrivateKey(cursor.getString(cursor.getColumnIndexOrThrow("privateKey")));

                modelList.add(model);
            } while (cursor.moveToNext());
        }

        if (cursor != null) {
            cursor.close();
        }
        close();
        return modelList;
    }
}
Editor is loading...
Leave a Comment