Android “我怎么能?”;扩展;SQLite数据库?
我正在构建一个Android应用程序,它收集设备上的所有联系信息(我相信是通过SQLite)。不幸的是,我需要一些来自互联网的附加信息,我想存储在提到的数据库中,以便用户下次打开我的应用程序时能够查询(由我提供)这些信息,这样即使没有网络连接,他也可以使用它。 我使用内容提供商获取联系人详细信息Android “我怎么能?”;扩展;SQLite数据库?,android,database,Android,Database,我正在构建一个Android应用程序,它收集设备上的所有联系信息(我相信是通过SQLite)。不幸的是,我需要一些来自互联网的附加信息,我想存储在提到的数据库中,以便用户下次打开我的应用程序时能够查询(由我提供)这些信息,这样即使没有网络连接,他也可以使用它。 我使用内容提供商获取联系人详细信息 实现这一点最有效、相对简单的方法是什么?您需要的是创建自己的内容提供商—使用SQLite作为存储技术。您需要创建一个扩展SQLiteOpenHelper的类,该类为onCreate(Database D
实现这一点最有效、相对简单的方法是什么?您需要的是创建自己的内容提供商—使用SQLite作为存储技术。您需要创建一个扩展
SQLiteOpenHelper
的类,该类为onCreate(Database Database)
提供实现,并在那里指定SQL查询以创建所需的表。例如:
public class YourDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "yourDB.db";
private static final int DATABASE_VERSION = 1;
public YourDBHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
//here you write the code to create the table as you desire:
String query = "CREATE TABLE MoreContactInfo <your columns def>...";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
//do the upgrade (e.g drop and create table)
}
}
要插入数据,请按如下方式重写insert方法:
@Override
public Uri insert(Uri uri, ContentValues values) {
//ideally you use URI-Matcher to figure out who to treat the insert
SQLiteDatabase sqlDB = yourDatabase.getWritableDatabase();
long id = sqlDB.insert("<your-table-name-here>", null, values);
return Uri.parse("<your-content-provider-base-uri"> +"/"+id);
}
@覆盖
公共Uri插入(Uri、ContentValues){
//理想情况下,您可以使用URI匹配器来确定谁来处理插入
SQLiteDatabase sqlDB=yourDatabase.getWritableDatabase();
long id=sqlDB.insert(“,null,value);
返回Uri.parse("从设备获取所有联系人并将其与修改后的值一起保存在应用程序的数据库中,这意味着我必须获取数据库的内容,然后获取默认数据库。如果存在差异,请更新我的数据库,并在关机后将其全部保存。这不是很慢吗?不,在应用程序启动时获取所有联系人并将其保存在应用程序的数据库中。不要每次都查询手机的数据库,也不要更改应用程序的数据库。这似乎是个好答案。我还会看一下您的演示文稿。谢谢。:)
@Override
public Uri insert(Uri uri, ContentValues values) {
//ideally you use URI-Matcher to figure out who to treat the insert
SQLiteDatabase sqlDB = yourDatabase.getWritableDatabase();
long id = sqlDB.insert("<your-table-name-here>", null, values);
return Uri.parse("<your-content-provider-base-uri"> +"/"+id);
}