Android的数据库将在开始 [英] Android Database Insert At The Beginning

查看:98
本文介绍了Android的数据库将在开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要插入所需数据,我在一开始应用程序,我将使用这些数据。我想插入一次,并且必须有没有重复的。因此,在的onCreate,我做这样的:如果表中的行数(如学生等)为0,我插入的学生。我不认为这是做到这一点的最好办法。因此,我想了解是否有更好的办法。

I want to insert required data for my application at the beginning and I will use these data. And I want to insert once, and there must be no duplicate. Therefore, in "onCreate", I'm doing like that : if the row count of table(such as student etc) is 0, I'm inserting students. I don't think it's the best way to do this. Therefore I want to learn if there is a better way.

推荐答案

如果你希望你的数据库填充在安装时,从来没有任何时候,你唯一合理的选择是你的pre填充的数据库与您的APK包作为一个内置的资源。这具有简化您的应用程序的优势。

If you want your database populated at install time and never any other time, your only reasonable option is to package your pre-populated database with your APK as a built-in resource. This has the advantage of simplifying your app.

另外,如果您实现的 SQLiteOpenHelper $ C $为你的数据库,任何你在 SQLiteOpenHelper.onCreate(SQLiteDatabase)插入将永远只能无论是在你第一次运行应用程序插入或者当有人清除所有的应用程序的数据(这些数据或多或少地把你回到一个新的安装反正)。该 SQLiteOpenHelper 超知道是否要当你调用一个运行创造code中的 getWritableDatabase() getReadOnlyDatabase()的方法来获取数据库的参考。

Alternately, if you implement the SQLiteOpenHelper for your database, anything you insert during SQLiteOpenHelper.onCreate(SQLiteDatabase) will only ever be inserted either on the first run of you app or when someone clears all your app's data (which is more or less putting you back to a fresh install anyway). The SQLiteOpenHelper superclass knows whether or not to run the creation code when you call one of the getWritableDatabase() or getReadOnlyDatabase() methods to get your database reference.

值得一提的是,安卓并没有真正让你运行一个安装程序的方式桌面软件一样。如果您不需要做任何设置工作,你需要能够检测并记住,当你的应用程序之前已经运行。

It is worth noting that Android doesn't really let you run an installer the way desktop software does. If you need to do any setup work, you need to be able to detect and remember when your app has been run before.

这篇关于Android的数据库将在开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆