了解Android.com的"将数据保存在SQL数据库"教程存在、数据库、教程、数据

2023-09-06 06:57:26 作者:既然不爱何必要伤我

我在阅​​读本教程: http://developer.android。 COM /培训/基础/​​数据存储/ databases.html 并提出了很多问题。

这是我的code,而下面的教程我做:

 进口android.content.Context;进口android.database.sqlite.SQLiteDatabase;进口android.database.sqlite.SQLiteOpenHelper;进口android.provider.BaseColumns;公共final类SongDbHelper扩展SQLiteOpenHelper {    //如果更改了数据库架构,则必须增加数据库版本。    公共静态最终诠释DATABASE_VERSION = 1;    公共静态最后弦乐DATABASE_NAME =Song.db;    私有静态最后弦乐TEXT_TYPE =TEXT;    私有静态最后弦乐PRIMARY_KEY_TYPE =INTEGER PRIMARY KEY    私有静态最后弦乐COMMA_SEP =;    私有静态最后弦乐SQL_CREATE_ENTRIES =            CREATE TABLE+ SongEntry.TABLE_NAME +(+                    SongEntry._ID + PRIMARY_KEY_TYPE + COMMA_SEP +                    SongEntry.COLUMN_NAME_SONG_TITLE + TEXT_TYPE + COMMA_SEP +                    SongEntry.COLUMN_NAME_ENGLISH_LYRICS + TEXT_TYPE + COMMA_SEP +                    SongEntry.COLUMN_NAME_ROMAJI_LYRICS + TEXT_TYPE + COMMA_SEP +                    SongEntry.COLUMN_NAME_KANJI_LYRICS + TEXT_TYPE + COMMA_SEP +                    SongEntry.COLUMN_NAME_INFO + TEXT_TYPE +            );    私有静态最后弦乐SQL_DELETE_ENTRIES =            DROP TABLE IF EXISTS+ SongEntry.TABLE_NAME;    公共SongDbHelper(上下文的背景下){        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);    }    公共无效的onCreate(SQLiteDatabase DB){        db.execSQL(SQL_CREATE_ENTRIES);    }    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){        //这个数据库仅用于在线数据的高速缓存,因此其升级政策        //简单地丢弃数据并重新开始        db.execSQL(SQL_DELETE_ENTRIES);        的onCreate(DB);    }    公共无效onDowngrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){        onUpgrade(DB,oldVersion,静态网页);    }    公共静态抽象类SongEntry实现BaseColumns {        公共静态最后弦乐TABLE_NAME =歌;        公共静态最后弦乐COLUMN_NAME_SONG_TITLE =SONG_TITLE;        公共静态最后弦乐COLUMN_NAME_ENGLISH_LYRICS =english_lyrics;        公共静态最后弦乐COLUMN_NAME_ROMAJI_LYRICS =romaji_lyrics;        公共静态最后弦乐COLUMN_NAME_KANJI_LYRICS =kanji_lyrics;        公共静态最后弦乐COLUMN_NAME_INFO =信息;    }} 

我们是否应该调用 SongDbHelper.onCreate()每一个应用程序打开的时间? (我想象把它在我的主要活动的的onCreate()方法。

Android高级进阶之路 Android中View绘制流程浅析

如果你调用 SongDbHelper.onCreate()不止一次,将它重新创建表,或将 SongDbHelper.onCreate() 只是默默地优雅地失败?

为什么应用程序需要调用 SongDbHelper.onCreate()反正?如果我把我的应用程序在App Store,会不会我只是让人们下载,我已经充满了数据的数据库?

在/在哪里我的应用我叫 SongDbHelper.onUpgrade()?我认为,当用户注意到,出现这种情况没有可用于这个应用程序的最新信息。点击这里下载或者类似的东西。我只是困惑,因为我想像用户只需下载与数据库中的 .db的文件完好;因此不会需要调用 SongDbHelper.onCreate()

如果应用程序必须调用 SongDbHelper.onCreate(),它如何填充值的数据库?我认为这会破坏数据库的目的,如果我也有code,在值罢了,因为那时值将(在code的和的数据库)中复制。

什么是填充值的数据库的最佳方式?与终端,或者以编程方式从应用程序,或仅在Android应用中一个单独的程序?

解决方案

答案:

点1,2,3:由于只需在教程中提到你在的此链接,我们不应该叫 SongDbHelper.onCreate()。取而代之的是,当我们想从有数据库引用的 Helper类,我们使用 CONSTUCTOR 这样的:

  SongDbHelper mDbHelper =新SongDbHelper(的getContext());//这将在内部调用超级方法//这将在数据库中创建表 

4点: onUpgrade()也是不是我们的责任,明确地调用。当我们改变数据库模式,我们更新 DATABASE_VERSION 和Android框架会在内部调用 onUpgrade()我们

5点:你可以用一个从你的 XML 存储歌词一到数据库。这是正确的做法,据我了解

更新:

最好的办法是存储数据库这是由Web服务器上的之前从,下载Web服务器并有应用的然后读/写到数据库中。这不会打败目的数据库,此外它的不会复制数据库条目和code项(XML)。除此之外,您应用规模也随之变小,因为应用程序会在运行数据库下载,而不是在设备内存中存储最初

I was reading this tutorial: http://developer.android.com/training/basics/data-storage/databases.html and it raised a lot of questions.

This is my code I made while following the tutorial:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public final class SongDbHelper 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 = "Song.db";
    private static final String TEXT_TYPE = " TEXT";
    private static final String PRIMARY_KEY_TYPE = " INTEGER PRIMARY KEY";
    private static final String COMMA_SEP = ",";
    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + SongEntry.TABLE_NAME + " (" +
                    SongEntry._ID + PRIMARY_KEY_TYPE + COMMA_SEP +
                    SongEntry.COLUMN_NAME_SONG_TITLE + TEXT_TYPE + COMMA_SEP +
                    SongEntry.COLUMN_NAME_ENGLISH_LYRICS + TEXT_TYPE + COMMA_SEP +
                    SongEntry.COLUMN_NAME_ROMAJI_LYRICS + TEXT_TYPE + COMMA_SEP +
                    SongEntry.COLUMN_NAME_KANJI_LYRICS + TEXT_TYPE + COMMA_SEP +
                    SongEntry.COLUMN_NAME_INFO + TEXT_TYPE +
            " )";
    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + SongEntry.TABLE_NAME;

    public SongDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(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
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }


    public static abstract class SongEntry implements BaseColumns {
        public static final String TABLE_NAME = "Song";
        public static final String COLUMN_NAME_SONG_TITLE = "song_title";
        public static final String COLUMN_NAME_ENGLISH_LYRICS = "english_lyrics";
        public static final String COLUMN_NAME_ROMAJI_LYRICS = "romaji_lyrics";
        public static final String COLUMN_NAME_KANJI_LYRICS = "kanji_lyrics";
        public static final String COLUMN_NAME_INFO = "info";

    }
}

Are we supposed to call SongDbHelper.onCreate() every time the app opens up? (I'm imagining putting it in my main activity's onCreate() method.

If you call SongDbHelper.onCreate() more than once, will it recreate the table, or will SongDbHelper.onCreate() just fail silently and gracefully?

Why does the app need to call SongDbHelper.onCreate() anyways? If I put my app on the app store, won't I just let people download the database that I already filled with data?

When/where in my app do I call SongDbHelper.onUpgrade()? I assume this happens when the user notices that "There is an update available for this app. Click here to download it" or something like that. I'm just confused, because I'd imagine the user simply downloads a .db file with the database intact; and therefore won't need to call SongDbHelper.onCreate().

If the app has to call SongDbHelper.onCreate(), how does it fill the database with values? I think it would defeat the purpose of a database if I also have code that fills in the values, because then the values would be duplicated (in the code and the database).

What is the best way to fill a database with values? With the terminal, or programmatically in a separate program from the app, or only in the Android app?

解决方案

Answer for :

Point 1,2,3 : As simply mentioned in tutorial you are following at this link, we are not supposed to call SongDbHelper.onCreate(). Instead of it, when we want to have reference of database from Helper class, we use the CONSTUCTOR like :

SongDbHelper mDbHelper = new SongDbHelper(getContext());
// this will call super method internally and
// this will create table in database

Point 4 : onUpgrade() is also not responsibility of ours to call explicitely. When we change database schema, we update DATABASE_VERSION and android framework will internally call onUpgrade() for us

Point 5 : You can store song lyrics one by one from your xml to database. This is right way as far as i know

Update :

The best way would be to store your database on a web server which is made prior, download it from the web server and have the app then read/write into database. This will not defeat purpose of database, in addition it will not duplicate database entries and code entries (xml). In addition to that, your app size will also be smaller because application will download database in runtime, rather than storing in device memory initially