java 阳光数据库创建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 阳光数据库创建相关的知识,希望对你有一定的参考价值。
package com.example.android.sunshine.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.android.sunshine.data.WeatherContract.WeatherEntry;
/**
* Manages a local database for weather data.
*/
// COMPLETED (11) Extend SQLiteOpenHelper from WeatherDbHelper
public class WeatherDbHelper extends SQLiteOpenHelper {
// COMPLETED (12) Create a public static final String called DATABASE_NAME with value "weather.db"
/*
* This is the name of our database. Database names should be descriptive and end with the
* .db extension.
*/
public static final String DATABASE_NAME = "weather.db";
// COMPLETED (13) Create a private static final int called DATABASE_VERSION and set it to 1
/*
* If you change the database schema, you must increment the database version or the onUpgrade
* method will not be called.
*
* The reason DATABASE_VERSION starts at 3 is because Sunshine has been used in conjunction
* with the Android course for a while now. Believe it or not, older versions of Sunshine
* still exist out in the wild. If we started this DATABASE_VERSION off at 1, upgrading older
* versions of Sunshine could cause everything to break. Although that is certainly a rare
* use-case, we wanted to watch out for it and warn you what could happen if you mistakenly
* version your databases.
*/
private static final int DATABASE_VERSION = 1;
// COMPLETED (14) Create a constructor that accepts a context and call through to the superclass constructor
public WeatherDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// COMPLETED (15) Override onCreate and create the weather table from within it
/**
* Called when the database is created for the first time. This is where the creation of
* tables and the initial population of the tables should happen.
*
* @param sqLiteDatabase The database.
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
/*
* This String will contain a simple SQL statement that will create a table that will
* cache our weather data.
*/
final String SQL_CREATE_WEATHER_TABLE =
"CREATE TABLE " + WeatherEntry.TABLE_NAME + " (" +
/*
* WeatherEntry did not explicitly declare a column called "_ID". However,
* WeatherEntry implements the interface, "BaseColumns", which does have a field
* named "_ID". We use that here to designate our table's primary key.
*/
WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
WeatherEntry.COLUMN_DATE + " INTEGER, " +
WeatherEntry.COLUMN_WEATHER_ID + " INTEGER, " +
WeatherEntry.COLUMN_MIN_TEMP + " REAL, " +
WeatherEntry.COLUMN_MAX_TEMP + " REAL, " +
WeatherEntry.COLUMN_HUMIDITY + " REAL, " +
WeatherEntry.COLUMN_PRESSURE + " REAL, " +
WeatherEntry.COLUMN_WIND_SPEED + " REAL, " +
WeatherEntry.COLUMN_DEGREES + " REAL" + ");";
/*
* After we've spelled out our SQLite table creation statement above, we actually execute
* that SQL with the execSQL method of our SQLite database object.
*/
sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE);
}
// COMPLETED (16) Override onUpgrade, but don't do anything within it yet
/**
* This database is only a cache for online data, so its upgrade policy is simply to discard
* the data and call through to onCreate to recreate the table. Note that this only fires if
* you change the version number for your database (in our case, DATABASE_VERSION). It does NOT
* depend on the version number for your application found in your app/build.gradle file. If
* you want to update the schema without wiping data, commenting out the current body of this
* method should be your top priority before modifying this method.
*
* @param sqLiteDatabase Database that is being upgraded
* @param oldVersion The old database version
* @param newVersion The new database version
*/
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
}
}
package com.example.android.sunshine.data;
import android.provider.BaseColumns;
/**
* Defines table and column names for the weather database. This class is not necessary, but keeps
* the code organized.
*/
public class WeatherContract {
// COMPLETED (1) Within WeatherContract, create a public static final class called WeatherEntry that implements BaseColumns
/* Inner class that defines the table contents of the weather table */
public static final class WeatherEntry implements BaseColumns {
// Do steps 2 through 10 within the WeatherEntry class
// COMPLETED (2) Create a public static final String call TABLE_NAME with the value "weather"
/* Used internally as the name of our weather table. */
public static final String TABLE_NAME = "weather";
// COMPLETED (3) Create a public static final String call COLUMN_DATE with the value "date"
/*
* The date column will store the UTC date that correlates to the local date for which
* each particular weather row represents. For example, if you live in the Eastern
* Standard Time (EST) time zone and you load weather data at 9:00 PM on September 23, 2016,
* the UTC time stamp for that particular time would be 1474678800000 in milliseconds.
* However, due to time zone offsets, it would already be September 24th, 2016 in the GMT
* time zone when it is 9:00 PM on the 23rd in the EST time zone. In this example, the date
* column would hold the date representing September 23rd at midnight in GMT time.
* (1474588800000)
*
* The reason we store GMT time and not local time is because it is best practice to have a
* "normalized", or standard when storing the date and adjust as necessary when
* displaying the date. Normalizing the date also allows us an easy way to convert to
* local time at midnight, as all we have to do is add a particular time zone's GMT
* offset to this date to get local time at midnight on the appropriate date.
*/
public static final String COLUMN_DATE = "date";
// COMPLETED (4) Create a public static final String call COLUMN_WEATHER_ID with the value "weather_id"
/* Weather ID as returned by API, used to identify the icon to be used */
public static final String COLUMN_WEATHER_ID = "weather_id";
// COMPLETED (5) Create a public static final String call COLUMN_MIN_TEMP with the value "min"
/* Min and max temperatures in °C for the day (stored as floats in the database) */
public static final String COLUMN_MIN_TEMP = "min";
// COMPLETED (6) Create a public static final String call COLUMN_MAX_TEMP with the value "max"
public static final String COLUMN_MAX_TEMP = "max";
// COMPLETED (7) Create a public static final String call COLUMN_HUMIDITY with the value "humidity"
/* Humidity is stored as a float representing percentage */
public static final String COLUMN_HUMIDITY = "humidity";
// COMPLETED (8) Create a public static final String call COLUMN_PRESSURE with the value "pressure"
/* Pressure is stored as a float representing percentage */
public static final String COLUMN_PRESSURE = "pressure";
// COMPLETED (9) Create a public static final String call COLUMN_WIND_SPEED with the value "wind"
/* Wind speed is stored as a float representing wind speed in mph */
public static final String COLUMN_WIND_SPEED = "wind";
// COMPLETED (10) Create a public static final String call COLUMN_DEGREES with the value "degrees"
/*
* Degrees are meteorological degrees (e.g, 0 is north, 180 is south).
* Stored as floats in the database.
*
* Note: These degrees are not to be confused with temperature degrees of the weather.
*/
public static final String COLUMN_DEGREES = "degrees";
}
}
以上是关于java 阳光数据库创建的主要内容,如果未能解决你的问题,请参考以下文章