欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-05-16 12:28:07
本文实例讲述了android创建或升级数据库时执行的语句,如果是创建或升级数据库,请使用带list参数的构造方法,带sql语句的构造方法将在数据库创建或升级时执行。 具体...

本文实例讲述了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程序开发能够起到一定的帮助作用。