如果啓動應用程序時數據庫不存在,我需要創建一個數據庫並在其中存儲一些初始數據。我怎樣才能做到這一點?使用初始數據創建和引導數據庫
我知道我應該使用類SQLiteOpenHelper
但還沒有找到它創建了一個dabatase 如果它不存在它與初始數據自舉它的一個例子。
如果啓動應用程序時數據庫不存在,我需要創建一個數據庫並在其中存儲一些初始數據。我怎樣才能做到這一點?使用初始數據創建和引導數據庫
我知道我應該使用類SQLiteOpenHelper
但還沒有找到它創建了一個dabatase 如果它不存在它與初始數據自舉它的一個例子。
尚未找到創建數據庫的示例,如果它不存在並使用初始數據引導它。
您看到的每個示例SQLiteOpenHelper
都會創建一個數據庫(如果它不存在)。這是點的一半SQLiteOpenHelper
(當模式需要更改時,另一半正在協助數據庫遷移)。實際的數據庫創建本身由SQLiteOpenHelper
處理。您只需覆蓋onCreate()
並創建您的模式(並且,如果相關,則使用啓動器數據填充表)。
因此,例如,在this sample app,我有一個SQLiteOpenHelper
子類,做到了這一點:
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.constants;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.hardware.SensorManager;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="constants.db";
private static final int SCHEMA=1;
static final String TITLE="title";
static final String VALUE="value";
static final String TABLE="constants";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE constants (title TEXT, value REAL);");
ContentValues cv=new ContentValues();
cv.put(TITLE, "Gravity, Death Star I");
cv.put(VALUE, SensorManager.GRAVITY_DEATH_STAR_I);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Earth");
cv.put(VALUE, SensorManager.GRAVITY_EARTH);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Jupiter");
cv.put(VALUE, SensorManager.GRAVITY_JUPITER);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Mars");
cv.put(VALUE, SensorManager.GRAVITY_MARS);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Mercury");
cv.put(VALUE, SensorManager.GRAVITY_MERCURY);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Moon");
cv.put(VALUE, SensorManager.GRAVITY_MOON);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Neptune");
cv.put(VALUE, SensorManager.GRAVITY_NEPTUNE);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Pluto");
cv.put(VALUE, SensorManager.GRAVITY_PLUTO);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Saturn");
cv.put(VALUE, SensorManager.GRAVITY_SATURN);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Sun");
cv.put(VALUE, SensorManager.GRAVITY_SUN);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, The Island");
cv.put(VALUE, SensorManager.GRAVITY_THE_ISLAND);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Uranus");
cv.put(VALUE, SensorManager.GRAVITY_URANUS);
db.insert(TABLE, TITLE, cv);
cv.put(TITLE, "Gravity, Venus");
cv.put(VALUE, SensorManager.GRAVITY_VENUS);
db.insert(TABLE, TITLE, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
throw new RuntimeException("How did we get here?");
}
}
我會做這樣的:
所有的在接下來的應用程序運行中,由於標誌將存在,所以初始化函數將被跳過。
你可以看到App類是在每個活動之前開始的東西。這不是一個項目需要的類,但要做這種初始化,它是放置它們的正確位置。