android什么时候创建SQL数据库?

时间:2015-10-01 20:06:50

标签: android sqlite

几个月前我刚刚开始在android中编程,并且在android实际创建SQL数据库文件时感到困惑。 dbhelper在多个活动中被引用,我不希望在创建数据库之前引用它。我应该在我的主要活动中实例化它吗?我创建了一个帮助器/契约类来处理大部分SQL操作,如下所示:

public class PantryActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pantry);
    PantryDbHelper pantryDb = new PantryDbHelper(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_pantry, menu);
    return true;
}


//The helper class for the SQL table
public class PantryDbHelper extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "Pantry.db";

    public PantryDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(PantryContract.SQL_CREATE_ENTRIES);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        //TODO add code to handle upgrades, import table over async task
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

    //TODO add write/read methods for SQL database


    //contract class for the SQL database, used to identify columns/set up tables
    public final class PantryContract {
        public PantryContract() {
        }

        public abstract class PantryColumns implements BaseColumns {
            //Column identifiers to be used in each table
            public static final String TABLE_NAME = "Pantry";
            public static final String COLUMN_NAME_FOOD_ITEM = "FoodItem";
            public static final String COLUMN_NAME_FOOD_TYPE = "FoodType";
            public static final String COLUMN_NAME_FOOD_AMOUNT = "Amount";
            public static final String COLUMN_NAME_EXPIRATION_DATE = "ExpirationDate";
        /*
        public static final String COLUMN_NAME_CALORIES = "Calories";
        public static final String COLUMN_NAME_CARBOHYDRATES = "Carbohydrates";
        public static final String COLUMN_NAME_FAT = "Fat";
        public static final String COLUMN_NAME_PROTEIN = "Protein";
        */
        }

        //Table data types/comma
        private static final String TEXT_TYPE = " TEXT";
        private static final String INTEGER_TYPE = " INTEGER";
        private static final String COMMA_SEP = ", ";
        private static final String PRIMARY_KEY = "PRIMARY KEY";
        private static final String UNIQUE_CONSTRAINT = " Unique";
        private static final String NOT_NULL_CONSTRAINT = " NOT NULL";

        //Table creation parameters
        private static final String SQL_CREATE_ENTRIES = "Create Table "+ PantryColumns.TABLE_NAME + " (" +
                PantryColumns._ID + INTEGER_TYPE + PRIMARY_KEY + COMMA_SEP +
                PantryColumns.COLUMN_NAME_FOOD_ITEM + TEXT_TYPE + UNIQUE_CONSTRAINT + COMMA_SEP +
                PantryColumns.COLUMN_NAME_FOOD_TYPE + TEXT_TYPE + COMMA_SEP +
                PantryColumns.COLUMN_NAME_FOOD_AMOUNT + TEXT_TYPE + COMMA_SEP +
                PantryColumns.COLUMN_NAME_EXPIRATION_DATE + TEXT_TYPE + " )";
            /*
            PantryColumns.COLUMN_NAME_CALORIES + INTEGER_TYPE + COMMA_SEP +
            PantryColumns.COLUMN_NAME_CARBOHYDRATES + INTEGER_TYPE + COMMA_SEP +
            PantryColumns.COLUMN_NAME_FAT + INTEGER_TYPE + COMMA_SEP +
            PantryColumns.COLUMN_NAME_PROTEIN + INTEGER_TYPE
            */
    }
}

}

2 个答案:

答案 0 :(得分:1)

SQLiteOpenHelper构造函数documentation状态:

  

直到其中一个数据库才真正创建或打开数据库   调用getWritableDatabase()或getReadableDatabase()。

答案 1 :(得分:0)

使用SQLiteDBHelper,您不必担心在错误的时间访问它,因为没有错误的时间。在第一次创建DB Helper的实例时,就会调用DB helper的onUpdate方法来编写创建表的查询。

如果您已经安装了旧版本的数据库并且正在使用新版本的DB安装应用程序,则会调用select方法。你可以在这里写更新查询。