使用Room來操作sqlite數據庫十分方便
1. 創建依賴,下方代碼來自官方
def room_version = "2.2.3"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
可能需要在修改項目的gradle而非模塊的gradle文件如下,否則可能會導致編譯的時候失敗
buildscript {
repositories {
google()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url'https://maven.aliyun.com/repository/public/' }
maven { url'https://maven.aliyun.com/repository/google/' }
maven { url'https://maven.aliyun.com/repository/jcenter/' }
maven { url'https://maven.aliyun.com/repository/central/' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url'https://maven.aliyun.com/repository/public/' }
maven { url'https://maven.aliyun.com/repository/google/' }
maven { url'https://maven.aliyun.com/repository/jcenter/' }
maven { url'https://maven.aliyun.com/repository/central/' }
jcenter()
}
}
2. 創建一個實體類(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 = "word")
private String word;
@ColumnInfo(name = "chinese_meaning")
private String chineseMeaning;
public Word(){
};
public Word(String word, String chineseMeaning) {
this.word = word;
this.chineseMeaning = chineseMeaning;
}
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 getChineseMeaning() {
return chineseMeaning;
}
public void setChineseMeaning(String chineseMeaning) {
this.chineseMeaning = chineseMeaning;
}
}
3. 創建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
public interface WordDao {
@Insert
void insertWords(Word... words);
@Update
void updateWords(Word... words);
@Delete
void deleteWords(Word... words);
@Query("delete from word")
void deleteAllWords();
@Query("select * from word order by id desc")
List<Word> getAllWords();
}
4. 創建database抽象類
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {Word.class}, version = 1,exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
public abstract WordDao getWordDao();
}
5. 在Activity中使用
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import java.util.List;
public class MainActivity extends AppCompatActivity {
WordDatabase wordDatabase;
WordDao wordDao;
TextView textView;
Button btnInsert, btnUpdate, btnClear, btnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wordDatabase = Room.databaseBuilder(this, WordDatabase.class, "word_database")
.allowMainThreadQueries()
.build();
wordDao = wordDatabase.getWordDao();
textView = findViewById(R.id.textView);
updateView();
btnInsert = findViewById(R.id.btnInsert);
btnUpdate = findViewById(R.id.btnUpdate);
btnClear = findViewById(R.id.btnClear);
btnDelete = findViewById(R.id.btnDelete);
btnInsert.setOnClickListener(v -> {
Word word1 = new Word("Hello", "你好");
Word word2 = new Word("World", "世界");
wordDao.insertWords(word1, word2);
updateView();
});
btnUpdate.setOnClickListener(v -> {
Word word = new Word("Hi", "你好啊");
word.setId(1);
wordDao.updateWords(word);
updateView();
});
btnClear.setOnClickListener(v -> {
wordDao.deleteAllWords();
updateView();
});
btnDelete.setOnClickListener(v -> {
Word word = new Word();
word.setId(10);
wordDao.deleteWords(word);
updateView();
});
}
void updateView(){
List<Word> words = wordDao.getAllWords();
String text = "";
for (Word word : words) {
text += word.getId() + ": " + word.getWord() + " = " + word.getChineseMeaning() + "\n";
}
textView.setText(text);
}
}