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、效果演示:



未完待续。。。。
Android数据库(sqlite)之Room的更多相关文章
- android数据库SQLite的设计模式
Dao设计模式可能是使用最多的数据库的设计模式其基本思路是将数据库操作的代码 与设计代码分离以便于维护和升级.具体的实现方法是使用包,然后在设计代码中调 用数据库的操作代码,dao设计模式需要创建5个 ...
- Android数据库(sqlite)加密方案
最近因为一些项目的安全性需要将数据库加密,一开始想到的就是先将数据库通过AES加密,然后运行时再解密,另一种是将数据库里的内容加密. 很快这两种方案都是不理想的,第一种加密方式形同虚设,第二种,如果加 ...
- Android 数据库SQLite 写入SD卡
如果手机没有root,数据库文件是无法查看到的,不方便调试. 最好的办法是把数据库写进SD卡. 修改的地方有两处: 1.在你的helper类中把数据库文件名称 DATABASE_NAME 由原来的一个 ...
- android数据库sqlite增加删改查
http://hi-beijing.iteye.com/blog/1322040 http://www.cnblogs.com/wenjiang/archive/2013/05/28/3100860. ...
- 深入解析Sqlite的完美替代者,android数据库新王者——Realm
写在前面: 又到一年一度七夕虐狗节,看着大家忍受着各种朋友圈和QQ空间还有现实生活中的轮番轰炸,我实在不忍心再在这里给大家补刀,所以我觉得今天不虐狗,继续给大家分享有用的. 如果你比较关心androi ...
- android安卓Sqlite数据库实现用户登录注册
看了很多别人写的安卓SQlite数据的操作代码,一点也不通俗易懂,我觉得我写的不错,而且安卓项目也用上了,所以在博客园里保存分享一下!建立一个类 并继承SQLiteOpenHelper public ...
- Android中SQLite数据库小计
2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...
- sqlite升级--浅谈Android数据库版本升级及数据的迁移
Android开发涉及到的数据库采用的是轻量级的SQLite3,而在实际开发中,在存储一些简单的数据,使用SharedPreferences就足够了,只有在存储数据结构稍微复杂的时候,才会使用数据库来 ...
- android 对sqlite数据库的增删改查等各种操作
转载:http://blog.csdn.net/vrix/article/details/6717090 package com.sqlite.main; import java.io.File; i ...
- Android学习---SQLite数据库的增删改查和事务(transaction)调用
上一篇文章中介绍了手工拼写sql语句进行数据库的CRUD操作,本文将介绍调用sqlite内置的方法实现CRUD操作,其实质也是通过拼写sql语句. 首先,创建一个新的android项目: 其次,查看代 ...
随机推荐
- 用 lastIndexOf()、substr()、split()方法截取一段字符串
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索. split() 方法用于把一个字符串分割成字符串数组,抽取到分割符前面部分. subst ...
- redis-Hash(哈希表)
Redis hash 是一个string类型的field和value的映射表,它的添加.删除操作都是O(1)(平均).hash特别适用于存储对象,将一个对象存储在hash类型中会占用更少的内存,并且可 ...
- 「CF1039D」You Are Given a Tree
传送门 Luogu 解题思路 整体二分. 的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚 ...
- Myeclipse 安装时候android adt, android sdk常见问题
离线版adt安装 可以随意百度adt下载 安装时候注意断网模式,否则会连接到服务器耗费很长时间:如果安装报错,可能是adt与Myeclipse版本不匹配,如我用的是Myeclipse8.6,安装AD ...
- 图的数据结构的实现与遍历(DFS,BFS)
//图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public: MGraph(T a[], ...
- less在vscode中的配置方式
1.在vscode插件中下载easy less这个插件. 2.新建项目,分别建两个文件夹存放less和自动编译好的css,页面中引入文件引css就可以了. 3.根据你的文件位置,在用户设置中设置需要配 ...
- 十五 JSP开发模式&MVC设计模式
JSP开发模式: JavaBean + JSP : 缺点:页面代码过多,不利于维护,JSP页面代码变得臃肿 Servlet + JavaBean + JSP :MVC设计模式 M:model 模 ...
- switch不能case字符串
改用if(){ }else if(){ }
- SciPy 教程
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- WorkerServices构建Windows服务
.NET Core 3.1和WorkerServices构建Windows服务 介绍 ASP.NET Core 3增加了一个非常有意思的功能Worker Service.他是一个ASP.NET Cor ...