ConversationDAO
hacker98
java
5 months ago
5.6 kB
3
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.Conversation; import java.util.ArrayList; import java.util.List; public class ConversationDAO { private SQLiteDatabase db; private SQLiteOpenHelper dbHelper; // Constructor public ConversationDAO(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 Conversation public void insertConversation(Conversation conversation) { open(); ContentValues values = new ContentValues(); values.put("IPreceive", conversation.getIPreceive()); values.put("name", conversation.getName()); values.put("last_mess", conversation.getLast_mess()); values.put("last_mess_time", conversation.getLast_mess_time()); values.put("keyEncrypt128", conversation.getKeyEncrypt128()); values.put("keyEncrypt256", conversation.getKeyEncrypt256()); values.put("algorithm",conversation.getAlgorithm()); long result = 0; try{ result = db.insert("Conversation", null, values); }catch (SQLException e){ e.printStackTrace(); Log.d("Socket",e.getMessage()); } close(); } // Lấy dữ liệu từ bảng Conversation theo IPreceive (chỉ trả về 1 kết quả) public Conversation getConversationByIPReceive(String IPReceive) { open(); Conversation conversation = null; Cursor cursor = db.query("Conversation", null, "IPreceive = ?", new String[]{IPReceive}, null, null, null); if (cursor != null && cursor.moveToFirst()) { conversation = new Conversation(); conversation.setConver_id(cursor.getInt(cursor.getColumnIndexOrThrow("conver_id"))); conversation.setIPreceive(cursor.getString(cursor.getColumnIndexOrThrow("IPreceive"))); conversation.setName(cursor.getString(cursor.getColumnIndexOrThrow("name"))); conversation.setLast_mess(cursor.getString(cursor.getColumnIndexOrThrow("last_mess"))); conversation.setLast_mess_time(cursor.getString(cursor.getColumnIndexOrThrow("last_mess_time"))); conversation.setKeyEncrypt128(cursor.getString(cursor.getColumnIndexOrThrow("keyEncrypt128"))); conversation.setKeyEncrypt256(cursor.getString(cursor.getColumnIndexOrThrow("keyEncrypt256"))); conversation.setAlgorithm(cursor.getString(cursor.getColumnIndexOrThrow("algorithm"))); } if (cursor != null) { cursor.close(); } close(); return conversation; } // Cập nhật dữ liệu trong bảng Conversation public void updateConversation(Conversation conversation) { open(); ContentValues values = new ContentValues(); values.put("IPreceive", conversation.getIPreceive()); values.put("name", conversation.getName()); values.put("last_mess", conversation.getLast_mess()); values.put("last_mess_time", conversation.getLast_mess_time()); values.put("keyEncrypt128", conversation.getKeyEncrypt128()); values.put("keyEncrypt256", conversation.getKeyEncrypt256()); values.put("algorithm",conversation.getAlgorithm()); int result = db.update("Conversation", values, "conver_id = ?", new String[]{String.valueOf(conversation.getConver_id())}); close(); } // Lấy toàn bộ dữ liệu từ bảng Conversation public List<Conversation> getAllConversations() { List<Conversation> conversations = new ArrayList<>(); open(); Cursor cursor = db.query("Conversation", null, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { do { Conversation conversation = new Conversation(); conversation.setConver_id(cursor.getInt(cursor.getColumnIndexOrThrow("conver_id"))); conversation.setIPreceive(cursor.getString(cursor.getColumnIndexOrThrow("IPreceive"))); conversation.setName(cursor.getString(cursor.getColumnIndexOrThrow("name"))); conversation.setLast_mess(cursor.getString(cursor.getColumnIndexOrThrow("last_mess"))); conversation.setLast_mess_time(cursor.getString(cursor.getColumnIndexOrThrow("last_mess_time"))); conversation.setKeyEncrypt128(cursor.getString(cursor.getColumnIndexOrThrow("keyEncrypt128"))); conversation.setKeyEncrypt256(cursor.getString(cursor.getColumnIndexOrThrow("keyEncrypt256"))); conversation.setAlgorithm(cursor.getString(cursor.getColumnIndexOrThrow("algorithm"))); conversations.add(conversation); } while (cursor.moveToNext()); } if (cursor != null) { cursor.close(); } close(); return conversations; } }
Editor is loading...
Leave a Comment