我是一名Windows手机开发者,是Android数据库的新手, 我无法在android中创建本地数据库 我无法理解我在哪里做错了
我将在我的数据库中存储一些数据,稍后将获取它以填充我的列表视图。
目前我无法创建示例数据库,下面是我的代码
public final class DbContract {
public DbContract() {
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static DatabaseHelper dbHandler;
Context context;
public DatabaseHelper database;
/* Inner classes that defines the all table contents */
/**
* Hospital database structure, query
* */
public static abstract class Hospital implements BaseColumns {
public static final String TABLE_NAME = "emergency_hospital";
public static final String HOSPITAL_NAME = "name";
public static final String HOSPITAL_ADDRESS = "address";
public static final String HOSPITAL_CONTACT_NUMBER = "number";
}
private static final String SQL_CREATE_EMERGENCY_HOSPITAL = "CREATE TABLE "
+ Hospital.TABLE_NAME + " (" + Hospital._ID
+ " INTEGER PRIMARY KEY," + Hospital.HOSPITAL_NAME + TEXT_TYPE
+ COMMA_SEP + Hospital.HOSPITAL_ADDRESS + TEXT_TYPE + COMMA_SEP
+ Hospital.HOSPITAL_CONTACT_NUMBER + TEXT_TYPE + COMMA_SEP + " );";
private static final String SQL_DELETE_EMERGENCY_HOSPITAL = "DROP TABLE IF EXISTS "
+ Hospital.TABLE_NAME;
/**
* Handles database versions, create and populate tables in database
**/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "sample.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_EMERGENCY_HOSPITAL);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade
// policy is
// to simply to discard the whole database and start over
db.execSQL(SQL_DELETE_EMERGENCY_HOSPITAL);
onCreate(db);
}
}
public static DatabaseHelper getDbHandler() {
return dbHandler;
}
public void HospitalData() {
// Gets the data repository in write mode
SQLiteDatabase db = database.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(Hospital.HOSPITAL_NAME, "Bellevue Hospital Centre");
values.put(Hospital.HOSPITAL_ADDRESS, "Manhattan");
values.put(Hospital.HOSPITAL_CONTACT_NUMBER, "4356754");
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(Hospital.TABLE_NAME, null, values);
}
}
在我的主要活动中
private static DatabaseHelper dbHelper;
private SQLiteDatabase db;
dbHelper = DbContract.getDbHandler();
db = dbHelper.getReadableDatabase();
我的清单上写着
<provider
android:name=".database.DbContract"
android:authorities="com.demo" >
</provider>
这是初始化数据库创建的正确方法吗?
E/AndroidRuntime(2069): java.lang.RuntimeException: Unable to get provider com.demo.database.DbContract: java.lang.ClassCastException: com.demo.database.DbContract cannot be cast to android.content.ContentProvider
E/AndroidRuntime(2069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime(2069): Caused by: java.lang.ClassCastException: com.demo.database.DbContract cannot be cast to android.content.ContentProvider
谢谢!
答案 0 :(得分:1)
您的mainfest文件包含名称为“.database.DbContract”的提供程序标记。这是您获得异常的根本原因。 提供者标记用于声明内容提供者,名称标记可以是内容提供者的包名称,但在您的情况下,它不是任何内容提供者子类的名称。因此获得类强制转换异常。 请阅读内容提供者的基础知识: http://developer.android.com/guide/topics/providers/content-provider-basics.html
希望对你有帮助!!!