简易实现Room管理Sqlite数据库
数据库要放那种数据 实体行Entity 和列
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class Word {
@PrimaryKey (autoGenerate = true)//行主键 由系统自动生成
private int id ;
@ColumnInfo(name = "English_name")//列的名称,不写也可以会默认为变量名word
private String word;
@ColumnInfo(name = "Chinese_name")
private String meaning;
public Word(){
}
//设置构造方法
public Word(String word, String meaning) {
this.word = word;
this.meaning = meaning;
}
//设置变量的getter和setter ID的setter可以不用写因为系统会自动生成
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getMeaning() {
return meaning;
}
public void setMeaning(String meaning) {
this.meaning = meaning;
}
}
Dao层
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 //Database access object 也可以声明为抽象类或者接口
//最关键的类,数据库操作的接口 控制操作数据库的各种方法
public interface WordDao {
@Insert //修饰词的作用在于让系统编译的时候自动生成相关的代码
// Word...表示可以传递一个或者多个参数,返回值可以是void或者是long 返回一个插入后的id
void insertWord(Word... words);
@Update
//返回值可以是void 或者是int 返回修改了多少条数据,这条代码修改了多少条记录
void updateWord(Word... words);
@Delete
void deleteWord(Word... words);
//习惯性SQL语句都用大写
@Query("DELETE FROM WORD")
void deleteAllword();
@Query("SELECT * FROM WORD ORDER BY ID DESC")
List<Word> getAllWord();
}
Database抽象类继承RoomDatabase的抽象类
import androidx.room.Database;
import androidx.room.RoomDatabase;
//实现的代码都不需要自己写 所以声明为抽象类
@Database(entities = {Word.class},version = 1,exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
//若有多个Entities实体应该写多个Dao
public abstract WordDao getWordDao();
}
在MainAtivity中的操作
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
WordDao wordDao;
WordDatabase wordDatabase;
Button update,delete,deleteAll,add;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
update=findViewById(R.id.update);
delete=findViewById(R.id.delete);
deleteAll=findViewById(R.id.deleteAll);
add=findViewById(R.id.add);
wordDatabase= Room.databaseBuilder(this,WordDatabase.class,"word_database")
.allowMainThreadQueries()//数据库中的操作一般不在主线程 这里强行在主线层中进行操作
.build();
wordDao=wordDatabase.getWordDao();
updateView();
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word=new Word("OJBK","很欧尅");
Word word1=new Word("Total","完全");
wordDao.insertWord(word,word1);
updateView();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word=new Word("kkkk","看看看看");
word.setId(53);
wordDao.updateWord(word);
updateView();
}
});
deleteAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wordDao.deleteAllword();
updateView();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word=new Word();
word.setId(52);
wordDao.deleteWord(word);
updateView();
}
});
}
public void updateView(){
List<Word> words=wordDao.getAllWord();
String s="";
for (int i=0;i<words.size();i++){
Word word=words.get(i);
s+=word.getId()+": "+word.getWord()+": "+word.getMeaning()+"\n";
}
textView.setText(s);
}
}
网友评论