Android实现创建或升级数据库时执行语句
作者:shichen2014 发布时间:2022-07-10 11:58:58
标签:Android,执行,语句
本文实例讲述了Android创建或升级数据库时执行的语句,如果是创建或升级数据库,请使用带List参数的构造方法,带SQL语句的构造方法将在数据库创建或升级时执行。
具体程序代码如下:
import java.util.List;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SimpleSQLiteOpenHelper extends SQLiteOpenHelper {
private static final int INIT_VERSION = 1;
/**
* 创建或升级数据库时执行的语句。
*/
private List<String> sqlStatementExed;
/**
* 如果是创建或升级数据库,请使用带List参数的构造方法。
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param factory
* to use for creating cursor objects, or null for the default
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
*/
public SimpleSQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
sqlStatementExed = null;
}
/**
* 带SQL语句的构造方法。此SQL语句将在数据库创建或升级时执行。
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param factory
* to use for creating cursor objects, or null for the default
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
* @param sqlStatementExed
* 在数据库创建或升级的时候将执行的语句。
*/
public SimpleSQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version, List<String> sqlStatementExed) {
super(context, name, factory, version);
this.sqlStatementExed = sqlStatementExed;
}
/**
* 如果是创建或升级数据库,请使用带List参数的构造方法。
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
*/
public SimpleSQLiteOpenHelper(Context context, String name, int version) {
super(context, name, null, version);
sqlStatementExed = null;
}
/**
* 如果是创建或升级数据库,请使用带List参数的构造方法。
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
*/
public SimpleSQLiteOpenHelper(Context context, String name) {
super(context, name, null, INIT_VERSION);
sqlStatementExed = null;
}
/**
* 如果是创建或升级数据库,请使用带List参数的构造方法。
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param version
* number of the database (starting at 1); if the database is
* older, onUpgrade(SQLiteDatabase, int, int) will be used to
* upgrade the database; if the database is newer,
* onDowngrade(SQLiteDatabase, int, int) will be used to
* downgrade the database
* @param sqlCreateStatement
* 在创建或升级数据库时要执行的语句。
*/
public SimpleSQLiteOpenHelper(Context context, String name, int version,
List<String> sqlCreateStatement) {
super(context, name, null, version);
this.sqlStatementExed = sqlCreateStatement;
}
/**
* @param context
* @param name
* @param sqlCreateStatement
* 在创建或升级数据库时要执行的语句。
*/
public SimpleSQLiteOpenHelper(Context context, String name,
List<String> sqlCreateStatement) {
super(context, name, null, INIT_VERSION);
this.sqlStatementExed = sqlCreateStatement;
}
/*
* (non-Javadoc)
* @see
* android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
* .SQLiteDatabase)
*/
@Override
@Deprecated
public void onCreate(SQLiteDatabase db) {
exeSqlStatementExed(db);
}
/*
* (non-Javadoc)
* @see
* android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
@Override
@Deprecated
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
exeSqlStatementExed(db);
}
}
/**
* 初始化或升级数据库时执行的SQL语句。。
*/
private void exeSqlStatementExed(SQLiteDatabase db) {
if (sqlStatementExed != null) {
for (String statement : sqlStatementExed) {
db.execSQL(statement);
}
}
}
}
希望本文所述方法对于大家进行Android程序开发能够起到一定的帮助作用。


猜你喜欢
- 实例如下:一 json optString 解析的TimesTamp string二 long dateSec = (long) (Doub
- SpringCloud启动失败问题Nacos配置读取失败org.yaml.snakeyaml.error.YAMLException: ja
- java文件的多线程断点续传大致原理,供大家参考,具体内容如下谈到文件断点续传那么就离不开java.io.RandomAcessFile H
- 用户User的注册类型有Super和Common两种public eumn RegistrationType{ &nb
- 本文实例为大家分享了Android实现仿网易音乐唱片播放效果的具体代码,供大家参考,具体内容如下效果图: 在values中创建attrs.x
- 前言:在工作中一次排查慢接口时,查到了一个函数耗时较长,最终定位到是通过 List 去重导致的。由于测试环境还有线上早期数据较少,这个接口的
- 安装jdk(介绍三种方法)查看java版本:java -version方法一:利用yum源来安装jdk(此方法不需要配置环境变量)查看yum
- @RestControllerAdvice与@ControllerAdvice的区别@RestControllerAdvice注解与@Con
- Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框
- 一.异步冷数据流在Kotlin协程:协程的基础与使用中,通过使用协程中提供的flow方法可以创建一个Flow对象。这种方法得到的Flow对象
- 1.springboot启动过程中,首先会收集需要加载的bean的定义,作为BeanDefinition对象,添加到BeanFactory中
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 重写 equals()方法 和 hashCode()方法最近看了学习了集合的简单的知识,碰到了讲解 Set 的部分,感觉很好奇,这里对于 S
- 本文实例讲述了C#对list列表进行随机排序的方法。分享给大家供大家参考。具体实现方法如下:public List<T> Ran
- NO.1–注释在程序中,尤其是复杂的程序中,适当地加入注释可以增加程序的可读性,有利于程序的修改、调试和交流。注释的内容
- 经常坐地铁,却不知道地铁多少条线路?哪个站下车?今天就带领大家熟悉并绘制深圳地铁路线图。WPF在绘制矢量图方面有非常强大的优势,利用WPF可
- 导出excel是咱Java开发的必备技能啦,之前项目有这个功能,现在将其独立出来,分享一下。所用技术就是SpringBoot,然后是MVC架
- 单例模式一个类只有一个实例,并且可以全局访问使用应用场景如账户管理类,数据库操作类等(某个对象频繁被访问使用)常用方式饿汉式懒汉式同步加锁D
- 工具类-java精确到小数点后6位验证要求,必须精确到小数点后6位,但是后面都是0的话,double会省略0,正则验证不通过,所以有了下面解
- 之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘;Java实现两人五子棋游戏(三) 画出棋子;Java实现两人五子棋游戏(四) 落