android studio使用SQLiteOpenHelper()建立数据库的方法
作者:guo.714 时间:2023-10-28 12:50:35
在android studio中存储数据有三个方法,分别是:
(1)简单存储——SharedPreferences
(2)文件存储:内部存储——应用程序私有文件外部存储——SD卡资源文件——只读( RAW 、XML)
(3)数据库存储——SQLiteDataBase
今天我们讲讲在android studio中利用数据库存储中的SQLiteOpenHelper()方法建立数据库 SQLiteOpenH
elper中需要重载函数:
onCreate( ):利用SQL语句,在系统中创建数据库(表)功能
onUpgrade( ):利用SQL语句,在系统中升级数据库(表)功能
注:
onCreate()方法只有第一次数据库创建时会用到 onUpgrade( )方法在版本更新会用到
SQLiteOpenHelper中被调用的函数: getWritable
Database( ):获取一个可读写的SQLiteDatabase对象
getReadableDatabase( ):获取一个只读的SQLiteDatabase对象
创建数据库
1.新建一个类继承SQLiteOpenHelper
这里按Alt+Enter俩次添加构造方法
public class MySQLOpenHelper extends SQLiteOpenHelper {
public MySQLOpenHelper(@Nullable Context context , @Nullable String name , @Nullable SQLiteDatabase.CursorFactory factory , int version) {
super( context , name , factory , version );
}
(1)这个方法里面是用来创建数据库的
context是一个访问application环境全局信息的接口,通过它可以访问application的资源和相关的类 context其主要功能如下:启动Activity 启动和停止Service 发送广播消息(Intent) 注册广播消息(Intent)接收者可以访问APK中各种资源(如Resources和AssetManager等) 可以访问Package的相关信息 APK的各种权限管理 Context就是一个对APK包无所不知的大管家,大家需要什么,直接问它就可以了(2)name是数据库的名字(3)CursorFactory factory游标工厂,用来指向数据库里面的某一行 CursorFactory 用来创建游标的,默认值为null,指向一开始的位置 (4)version数据库的版本号 版本号只能增加,不能递减,要不然程序会崩溃
其他俩个方法是用来创建表的,接下来会讲到我们继续构建数据库:在 MySQLOpenHelper类中重构代码:
public class MySQLOpenHelper extends SQLiteOpenHelper {
public MySQLOpenHelper(@Nullable Context context ) {//name是数据库名字,后缀加.db
super( context , "SQLite.db" , null , 1 );
}
在MainActivity类中调用这个方法
MySQLOpenHelper helper=new MySQLOpenHelper( this );
然后再在MainActivity类中实例化数据库添加代码如下
MySQLOpenHelper helper=new MySQLOpenHelper( this );
//创建一个可读可写的数据库
SQLiteDatabase writableDatabase=helper.getWritableDatabase();
//创建一个只可读的数据库
SQLiteDatabase readableDatabase=helper.getReadableDatabase();
这个读者看需要自行构建代码,(只能存在一个)
我们的数据库创建完成了,接下来看看我们的数据库是否创建成功了呢在看之前我们先运行我们的虚拟机,因为数据库是建立在虚拟机里面的,运行完之后我们去找创建的文件。
android3.0以前打开虚拟机中的文件是用导航栏中的Tools中Android,3.0以后为了提供的更新工具把它舍弃了但我们可以通过View导航栏中Tool Windows中的Device File Explorer打开,找到data目录下的data下的程序的包名,找到创建的数据库(.db文件//另一个是相对应的日记文件),右键重新建立目录,然后用SQLite Expert Professional打开 SQLite Expert Professional下载地址:http://www.sqliteexpert.com 找到的文件如图
用SQLite Expert Professional 打开后如图
说明我们数据库已经创建好了
来源:https://blog.csdn.net/qq_45156021/article/details/104726466
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用DevExpress中的XtraCharts控件实现图表
![](https://img.aspxhome.com/file/2023/7/102167_0s.png)
基于Android Service 生命周期的详细介绍
![](https://img.aspxhome.com/file/2023/3/137833_0s.png)
Android自定义控件实现按钮滚动选择效果
![](https://img.aspxhome.com/file/2023/3/125443_0s.gif)
Spring boot中filter类不能注入@Autowired变量问题
![](https://img.aspxhome.com/file/2023/5/64375_0s.jpg)
Spring中的注解之@Override和@Autowired
![](https://img.aspxhome.com/file/2023/4/125564_0s.png)
VSCODE+cmake配置C++开发环境的实现步骤
![](https://img.aspxhome.com/file/2023/1/125911_0s.png)
详解三种C#实现数组反转方式
Spring Security使用中Preflight请求和跨域问题详解
![](https://img.aspxhome.com/file/2023/2/73012_0s.png)
轻松学习C#的String类
![](https://img.aspxhome.com/file/2023/6/122696_0s.png)
深入探究Java中的类加载机制
![](https://img.aspxhome.com/file/2023/7/75677_0s.png)
微信开发之使用java获取签名signature
![](https://img.aspxhome.com/file/2023/3/65203_0s.jpg)
200行java代码实现2048小游戏
![](https://img.aspxhome.com/file/2023/3/60693_0s.gif)
springboot 使用poi进行数据的导出过程详解
kotlin gson反序列化默认值失效深入讲解
Spring源码解析之事务传播特性
SpringBoot项目集成日志的实现方法
![](https://img.aspxhome.com/file/2023/0/86050_0s.png)
C# 开发(创蓝253)手机短信验证码接口的实例
idea右键没有java class选项问题解决方案
![](https://img.aspxhome.com/file/2023/4/132224_0s.png)
Android控件View的文字周围添加图标
![](https://img.aspxhome.com/file/2023/3/103913_0s.jpg)