Android实现创建或升级数据库时执行语句
作者:shichen2014 时间:2022-07-10 11:58:58
本文实例讲述了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程序开发能够起到一定的帮助作用。
标签:Android,执行,语句
0
投稿
猜你喜欢
详解Android自定义控件属性
2023-09-27 18:00:34
Feign如何实现第三方的HTTP请求
2023-06-10 16:20:20
Android提高之MediaPlayer音视频播放
2021-05-29 08:44:37
Java实现堆排序(大根堆)的示例代码
2023-09-13 15:24:01
JAVASE系统实现抽卡功能
2023-11-19 19:49:41
详解Spring Boot集成MyBatis(注解方式)
2023-10-03 17:45:47
C#特性 匿名类型与隐式类型局部变量使用介绍
2023-09-29 12:42:50
Spring boot配置绑定和配置属性校验的方式详解
2022-04-21 03:06:06
c#二维码生成的代码分享
2022-11-28 15:19:37
Android基础之startActivityForResult()的用法详解
2022-05-28 03:37:29
开源自研内存分析利器Android Bitmap Monitor图片定位详解
2023-02-04 13:45:36
spring 整合mybatis后用不上session缓存的原因分析
2021-12-09 10:11:03
详解IntelliJ IDEA创建spark项目的两种方式
2023-12-05 05:48:21
Android实现悬浮对话框代码
2022-11-23 16:12:45
springtask 的使用方法和 cron 表达式解析
2023-06-19 20:05:56
Android中Activity之间跳转和参数传递的实例
2023-01-31 21:40:32
Java 访问剪切板(复制,粘贴)的示例
2023-11-10 12:26:13
JAVA JDK8 List分组获取第一个元素的方法
2021-06-24 13:07:51
Springmvc Controller接口代码示例
2023-11-28 10:13:25
Java安全之Tomcat6 Filter内存马问题
2022-11-20 07:29:23