AsyncTask Room Database İşlemleri
30.01.2020 - 12:24
SQlite Room Database üzerinde çalışıyorum veritabanı işlemlerini AsyncTask ile nasıl yapabilirim ya da dahankolay bir yolla. Önemli olan atkaplanda çalışması.
AppDatabase.java
package com.atesandroid.sqlitedeneme;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@Database(entities = {User.class,Veresiye.class}, version =4)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public abstract VeresiyeDao veresiyeDao();
public static AppDatabase getAppDatabase(Context context){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,"musteri-database")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance(){
INSTANCE = null;
}
}
UserDao:
package com.atesandroid.sqlitedeneme;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface UserDao {
@Query("SELECT * FROM user where first_name LIKE :firstName AND last_name LIKE :lastName")
User findByFullName(String firstName,String lastName);
@Query("SELECT * FROM user where first_name LIKE :firstName")
List<User> findByFirstName(String firstName);
@Query("SELECT * FROM user WHERE first_name LIKE :lastName")
User findByLastName(String lastName);
@Query("SELECT * FROM user")
List<User> allUsers();
@Query("SELECT COUNT(*) from user")
int countUsers();
@Insert
void insertUsers(User... users);
@Insert
void insertUser(User user);
@Insert
void insertUserList(List<User> users);
@Delete
void delete(User user);
@Update
void update(User user);
}
38
Görüntülenme
0 Beğeni