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程序开发能够起到一定的帮助作用。
0
投稿
猜你喜欢
- 本文分为俩部分:第一部分介绍如何给Eclipse安装Spring Boot开发插件spring tool suite(简称STS);第二部分
- 有些SIM卡在出厂时并没有预置VoiceMail number,但运营商又要求能够根据PLMN去自适应的从手机中读取到预设的VM numbe
- 前言经过前面《Unity3D入门教程》系列讲解,再加上我们自己的探索,相信大家已经掌握了Unity3D的相关知识和基本方法。本文将使用前面学
- 本文实例讲述了Java利用反射自动封装成实体对象的方法。分享给大家供大家参考。具体分析如下:利用此方法的时候需要传递的参数的名称,必须以行号
- 1概述众所周知,Java支持平台无关性、安全性和网络移动性。而Java平台由Java虚拟机和Java核心类所构成,它为纯Java程序提供了统
- 如果出现:org.apache.ibatis.binding.BindingException: Invalid bound stateme
- init和clinit区别①init和clinit方法执行时机不同init是对象构造器方法,也就是说在程序执行 new 一个对象调用该对象类
- RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样。RP
- 场景既然要搞懂Redis分布式锁,那肯定要有一个需要它的场景。高并发售票问题就是一个经典案例。搭建环境准备redis服务,设置redis的键
- 本文实例讲述了Java使用DateFormatter格式化日期时间的方法。分享给大家供大家参考,具体如下:Java版本:1.8开始impor
- 目录注解是什么?元注解是什么?标准的元注解:@Target元注解:@Retention元注解:@Documented元注解:@Inherit
- 加密配置文件的SQL账号密码一般项目的配置文件里的信息都是明文的,导致有时候比较敏感的信息也直接暴露得超级明显,比如SQL的链接 账号 密码
- 1. 下载myeclipse版本对应的STS插件下载地址:https://spring.io/tools/sts/all2.在myeclip
- 本文实例为大家分享了unity实现按页码翻页效果的具体代码,供大家参考,具体内容如下用来做背包 商店的按页翻页功能,先上效果图其中,drag
- 目录一、复习二、两者对比三、在什么情况下才会使用volatile四、Java中的原子性操作五、Java中的CAS操作六、ABA问题七、Uns
- 利用Java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识。w表示白棋,b表示黑棋import java.util.S
- 最近项目需求中有需要导出Execl表格的要求,而且还是大量的数据,于是把之前的整理了一下,抽成了一个统一的工具类,需要时直接调用工具类即可,
- FTP(File Transfer Protocol)就是文件传输协议。通过FTP客户端从远程FTP服务器上拷贝文件到本地计算机称为下载,将
- 😎 先看效果一人分饰多角(bushi)😏 后端代码🍗 先引入websocket依赖<!-- websocket消息推送 -->&
- C# 从枚举值获取对应的文本描述详解有时枚举值在显示时,需要显示枚举值对应的文本串。一种方案是在调用的地方使用switch或者if来判断枚举