0
我是Windows Phone的開發者和新的Android數據庫, 我無法創造的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
謝謝!
你有什麼錯誤嗎? –
其實你寫錯了查詢...首先你必須糾正它... –
我已經添加了日誌可以請你檢查 – MobiDev