Android数据库(sqlite)之Room
说在前面:
1、使用Room需要添加的依赖:
dependencies { 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" }
2、数据库可视化工具安装及使用说明
3、涉及到的知识:
工程案例:
一、APP描述:对Word实体进行增删改
二、编写思路:
1、画界面:
1)上边是一个ScrollView(数据多的时候可滑动),ScrollView内有一个TextView
2)下边是四个按键,分别代表,插入、删除、删除所有、修改。
2、创建实体(Entity):
package com.me.roombasic; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Word { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "姓名") private String name; @ColumnInfo(name = "外号") private String other; public Word(String name, String other) { this.name = name; this.other = other; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } }
3、创建dao
package com.me.roombasic; 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 insertWord(Word ...words ); @Update void updateWord(Word... words); @Delete void deleteWord(Word... words); @Query("DELETE FROM WORD") void deleteAllWords(); @Query("SELECT * FROM WORD ORDER BY ID DESC") List<Word> getAllWords(); }
4、创建database
package com.me.roombasic; 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、暂时在mainActive.java中为TextView、Button绑定数据和监听:
package com.me.roombasic; 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 buttonInsert,buttonUpdate,buttonDelete,buttonQuery; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); buttonInsert = findViewById(R.id.buttoninster); buttonUpdate = findViewById(R.id.buttonupdate); buttonDelete = findViewById(R.id.buttondelete); buttonQuery = findViewById(R.id.buttonquery); wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database") .allowMainThreadQueries() .build(); wordDao = wordDatabase.getWordDao(); updateView(); buttonInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word1 = new Word("谷子腾","恶霸"); Word word2 = new Word("张凯鑫","胖鑫"); Word word3 = new Word("王正帅","笑天"); wordDao.insertWord(word1,word2,word3); updateView(); } }); buttonUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word2 = new Word("张凯鑫","bujv"); word2.setId(2); wordDao.updateWord(word2); updateView(); } }); buttonDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word2 = new Word("张凯鑫","胖鑫"); word2.setId(3); wordDao.deleteWord(word2); updateView(); } });buttonQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { wordDao.deleteAllWords(); updateView(); } }); } void updateView(){ List<Word> list = wordDao.getAllWords(); String text = ""; for(int i=0;i<list.size();i++){ text += list.get(i).getId() + ":" + list.get(i).getName() + "=" + list.get(i).getOther() + "\n"; } textView.setText(text); } }
6、效果演示: