how do i create the database in my android app?

前端 未结 2 1912
再見小時候
再見小時候 2021-01-16 23:32

I\'m trying to use SQLite in my android app. I\'ve created the helper class like this:

public class EventoSQLHelper {

    // Configuração da base(nome e ver         


        
相关标签:
2条回答
  • 2021-01-16 23:36

    Well, let's quote the Docs:

    A helper class to manage database creation and version management.

    You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

    So what this means is, that you don't have to explicity create the "database-file" it's done for you by the SQLiteOpenHelper. So the first time you're opening your Database it will be created. (It will automatically run through onCreate in your DataBaseHelper)

    0 讨论(0)
  • 2021-01-16 23:39

    You typically use a SQLiteOpenHelper from within a ContentProvider.

    http://developer.android.com/guide/topics/providers/content-provider-basics.html

    The SQLite database is a file, but normally you needn't care about that.

    The database file is indeed created when the helper's onCreate method is called (assuming no exception is thrown.) Remember that, unless you uninstall the app, you shouldn't expect this method to be called again.

    During the normal lifetime of an app, you'll probably provide updates and post the new version on the app store where people will either download it for the first time and install it (onCreate called) or update it (onUpgrade called ONLY if you incremented the database version number.)

    Since the Google play store currently doesn't provide a way to allow users to go back to an older version of your app, you typically don't have to worry about onDowngrade. If, however, you are providing older versions of your app outside of the Google play store, you should at least override onDowngrade so it doesn't throw a SQLiteException which is what the default implementation does.

    Now, back to onUpgrade. Some things you might need to do here are add a table, modify an existing table, drop a table, etc. I have handled this by writing .sql scripts that I put in the assets folder of my app with a name like upgrade_v1_v2.sql.

    There are some gotchas with SQLite you should be aware of that are detailed here:

    http://www.sqlite.org/omitted.html

    There are usually workarounds, however. For example, if you wanted to add a new column with a foreign key constraint to an existing table, you can't use the ALTER TABLE command for this since it is currently not supported by SQLite. Instead, you'll have to make a temporary table with the new set of columns you want, then copy the data from the old table to this temporary table, then drop the old table, and finally rename the temporary table to the old table's name - all preferably within a transaction to guard against something going wrong halfway through.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题