在创建Android应用本地数据库 [英] Creating local DB in android app

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

问题描述

根据 http://developer.android.com/指南/主题/数据/数据​​storage.html#DB 好像我需要另一个Java类文件(我做),但现在我碰到一个相当悲哀的问题:我不知道如何处理它。

Based on http://developer.android.com/guide/topics/data/data-storage.html#db it seems like I'd need another Java Class file (which I made), but now I've run into a rather sad problem: I'm not sure how to handle it.

我将如何把它在我的主要活动,并将它只有一次创建数据库,或将尝试重新创建数据库每次运行时?

How would I call it in my main activity and would it only create the database once, or would it try to re-create the database every time it runs?

推荐答案

这仅是第一次将创造的数据库。该后可以使用该SQLiteOpenHelper类的一个对象来访问该​​数据库。请注意,您不必担心数据库的娱乐,如果你使用SQLiteOpenHelper Android的方式会照顾的。

This would 'create' the database only for the first time. After that you can use an object of that SQLiteOpenHelper class to access that database. Note that you don't have to worry about the recreation of the database, android will take care of that if you use the SQLiteOpenHelper approach.

服用数据存储转发的例子,你会创建一个对象,再使用该getWriteableDatabase方法来获取对数据库的写入权限

Taking the data-storage example forward you would create an object of that and then use the getWriteableDatabase method on that to get write access to the database

DictionaryOpenHelper myDictionary;
myDictionary = new DictionaryOpenHelper(this);
SQLiteDatabase db = myDictionary.getWritableDatabase();

//to enter values in the db
ContentValues values = new ContentValues();
values.put("name","Rhyno");
db.insert(nameOfTable, null, values);

有关更详细的示例,请参阅本教程: http://marakana.com /forums/android/examples/55.html

For a more detailed example please refer to this tutorial: http://marakana.com/forums/android/examples/55.html

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

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