我想获取创建数据库的内容提供者的代码。我正在使用位于此处的工具 tools/sqllite3.exe 来检查数据库是否已创建。
请让我知道这件事的分步程序...
感谢:D
我想获取创建数据库的内容提供者的代码。我正在使用位于此处的工具 tools/sqllite3.exe 来检查数据库是否已创建。
请让我知道这件事的分步程序...
感谢:D
您不使用 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
我现在有一个不错的内容提供程序,它允许我轻松查询表,并且比那里的其他数据库灵活得多。