回答
你不箱与ContentProvider的数据库,但与SQLiteOpenHelper类。至少,这是做
class MyDatabase extends SQLiteOpenHelper {
public MyDatabase(Context context, dbName, null, version){
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String createItemsTable = "create table mytable ("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"_title TEXT NOT NULL, " +
"_subtitle TEXT NULL " +
");";
// Begin Transaction
db.beginTransaction();
try{
// Create Items table
db.execSQL(createItemsTable);
// Transaction was successful
db.setTransactionSuccessful();
} catch(SQLException ex){
Log.e(this.getClass().getName(), ex.getMessage(), ex);
} finally {
// End transaction
db.endTransaction();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropItemsTable = "DROP TABLE IF EXISTS mytable";
// Begin transaction
db.beginTransaction();
try {
if(oldVersion<2){
// Do your update/alter code here
}
db.setTransactionSuccessful();
} catch(Exception ex){
Log.e(this.getClass().getName(), ex.getMessage(), ex);
} finally {
// Ends transaction
// If there was an error, the database won't be altered
db.endTransaction();
}
}
}
的更好的方式来简单地
MyDatabase myDb = new MyDatabase(getContext(),"databasename.db", null, 1);
实例化你的助手,那么助手将创建数据库时,如果它不存在,如果旧版本升级存在,或者如果它存在,只要打开它,版本匹配
我用这个教程
http://www.devx.com/wireless/Article/41133/1763/page/2
我现在有一个很好的内容提供程序,可以让我轻松查询表格,并且比其他数据库更灵活。
这也适用,但很多描述... – 2010-10-09 18:30:58
关于我发布的教程,我不喜欢或必须做的唯一的事情就是使用完全限定的内容URI,因为我在同一个应用中使用它。我只是使用BooksProvider.TITLE或BooksProvider._ID,它使生活更加简单。但根据教程在外部软件包中使用它,您需要使用诸如“content://net.learn2develop.provider.Books/books”之类的内容。 – Opy 2010-10-12 14:23:30
- 1. Android内容提供商测试的真正内容提供商
- 2. android内容提供商
- 3. 内容提供商
- 4. 内容提供商
- 5. Android内容提供商光标查询
- 6. 无法访问Android内容提供商
- 7. 短信内容提供商在android?
- 8. Android联系人内容提供商
- 9. Android私人内容提供商?
- 10. Android的Facebook内容提供商权威
- 11. Android的内容提供商错误INSTALL_FAILED_CONFLICTING_PROVIDER
- 12. Android内容提供商Uri匹配
- 13. Android内容提供商列表
- 14. Android内容提供商独特查询
- 15. Android短信内容提供商
- 16. Android内容提供商Uri匹配
- 17. Android的定制内容提供商
- 18. Android的内容提供商权限
- 19. 注册内容提供商
- 20. 内容提供商了Exa
- 21. Spotify内容提供商
- 22. Content_URI在内容提供商
- 23. 访问内容提供商
- 24. 帮助内容提供商
- 25. 通过Android中的内容提供商提供图像
- 26. 内容提供商中的列表内容Android 3.1
- 27. Android的内容提供商给未知的URL内容:// COM
- 28. 来自内部存储与内容提供商的Android Intent.ACTION_SEND
- 29. 同一应用程序内的Android内容提供商
- 30. 冲突的内容提供商
谢谢...这工作! – 2010-10-09 18:30:21