欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现创建或升级数据库时执行语句

时间:2021-02-16 10:46:15|栏目: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程序开发能够起到一定的帮助作用。

上一篇:Android二级缓存加载图片实现照片墙功能

栏    目:Android代码

下一篇:Android编程开发中的正则匹配操作示例

本文标题:Android实现创建或升级数据库时执行语句

本文地址:http://www.codeinn.net/misctech/64306.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有