Android 外部数据库与内部数据库的混淆
我正在开发我的第一个 Android 应用程序,我正在尝试实现一个包含一堆食物类型(字符串)(例如“牛肉、猪肉、鸡肉”)的数据库,并为它们设置烹饪时间值(整数),例如 10000,50000, 30000 等。我的问题是实现这个的最佳方法是什么。我觉得最好的方法可能是使用外部数据库,但我也在想这是否会让我的生活变得地狱。基本上我应该做一些像我发现的可以在内部 SQlite 上工作的示例一样的事情:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+NAMES_TABLE
+" (_id INT PRIMARY KEY ,name VARCHAR);"
);
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(1,'marco');");
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(2,'luca');");
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(3,'qlimax');");
}
这会在启动时将值添加到内部数据库,只有当它们不存在时。
或
我应该使用 SQlitebrowser 创建一个数据库并将其放在 /assets 中并从中读取吗?
现在我正在从 ArrayAdapter 读取我的值,并且我尝试替换它并从数据库读取它,它看起来像这样:
final FoodType type[] = new FoodType[3];
type[0] = new FoodType("Filet Mignon",50000);
type[1] = new FoodType("Skirt Steak",0);
type[2] = new FoodType("Flank Steak",25000);
ArrayAdapter<FoodType> typeAdapter = new ArrayAdapter<FoodType>(this,android.R.layout.simple_spinner_item,type);
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
我应该如何处理这个问题?内部还是外部?
I am working on my first Android Application and I am trying to implement a database with a bunch of food types (strings) such as "Beef, Pork, Chicken" and have cook time values (int) for them such as 10000,50000,30000 etc. My question is what is the best way to implement this. I feel like the best way may be using an external database, but i am also thinking if it will make my life hell. Basically should I do something like an example I found that works off the internal SQlite:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+NAMES_TABLE
+" (_id INT PRIMARY KEY ,name VARCHAR);"
);
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(1,'marco');");
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(2,'luca');");
db.execSQL("INSERT INTO "+NAMES_TABLE+" values(3,'qlimax');");
}
This would add the values to the internal database when launched, only if they are not there.
OR
should I create a database with SQlitebrowser and place it in /assets and read from it?
Right now I am reading my values from an ArrayAdapter, and I am trying to replace it and read it from a database instead, it looks like this:
final FoodType type[] = new FoodType[3];
type[0] = new FoodType("Filet Mignon",50000);
type[1] = new FoodType("Skirt Steak",0);
type[2] = new FoodType("Flank Steak",25000);
ArrayAdapter<FoodType> typeAdapter = new ArrayAdapter<FoodType>(this,android.R.layout.simple_spinner_item,type);
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
How should I go about this? Internal or External?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我个人的经验,这取决于您的数据库有多大以及将会有多大。例如,我当前正在编写的应用程序有包含超过 50000 条记录的表,因此最好将已创建的数据库复制到资产目录中。
另一方面,如果您的数据库足够简单……最好从代码中填充它(这会避免一些令人头疼的问题,相信我)。
关于您的适配器...将其替换为 SimpleCursorAdapter,它需要在构造函数中进行很少的更改。
In my personal experience, it depends on how big your database is and will be. For instance, the app I'm currently writing have tables with more than 50000 records, so it's better to copy an already created database to the assets directory.
On the other hand, if your database is simple enough... it's much better to populate it from the code (it will prevent some headaches, trust me).
With regards to your adapter... replace it with a
SimpleCursorAdapter
, it requires few changes in the constructor.