我正在使用我的应用程序对象(Android 构建块)在我的 oncreate 方法中调用数据库构造函数。但我不知道为什么不能创建它。这就是我的代码的样子:
我的数据库类,将 DbHelper 作为内部类:
package com.example.pharmacie;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class PhData {
private static final String TAG = PhData.class.getSimpleName();
private static final int VERSION = 1;
private static final String DATABASE = "ph_info.db";
private static final String TABLE = "ph_info";
public static final String C_ID = "_id";
public static final String C_CREATED_AT = "created_at";
public static final String C_NAME = "Pharmacie ";
public static final String C_TELE = " ";
public static final String C_ADRESS = "Tet";
public static final String C_HAS_SHIFT = "yes"; // yes if night shift
public static final String C_SCHEDULE = "YYYY-MM-DD";
private static final String GET_ALL_ORDER_BY = C_CREATED_AT + " DESC";
private static final String[] MAX_CREATED_AT_COLUMNS = { "max("
+ PhData.C_CREATED_AT + ")" };
Context context;
private DbHelper dbHelper;
// Inner class: DbHelper implementations
class DbHelper extends SQLiteOpenHelper {
public DbHelper() {
super(context, DATABASE, null, VERSION);
Log.d(TAG, "Constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "Creating database: " + DATABASE);
db.execSQL("create table " + TABLE + " (" + C_ID
+ " int primary key, " + C_CREATED_AT + " int, " + C_NAME
+ " text, " + C_ADRESS + " text, "
+ C_HAS_SHIFT + " text, " + C_SCHEDULE
+ " text, " + C_TELE + " text)");
Log.d(TAG, "database created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + TABLE);
this.onCreate(db);
}
} // end of dbhelper class
public PhData(Context context) {
this.context = context;
this.dbHelper = new DbHelper();
Log.d(TAG, "Initialized data");
}
public void close() {
this.dbHelper.close();
}
public void insertOrIgnore(ContentValues values) {
Log.d(TAG, "insertOrIgnore on " + values);
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
try {
db.insertWithOnConflict(TABLE, null, values,
SQLiteDatabase.CONFLICT_IGNORE);
} finally {
db.close();
}
}
/**
* Deletes ALL the data
*/
public void delete() {
// Open Database
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Delete the data
db.delete(TABLE, null, null);
// Close Database
db.close();
}
}
这是我的应用程序类,我在其中调用构造函数来创建我的数据库:
package com.example.pharmacie;
import android.app.Application; import android.util.Log;
public class PharmacieApplication extends Application {
private static final String TAG = PharmacieApplication.class.getSimpleName(); PhData phData;
@Override public void onCreate() { super.onCreate();
phData = new PhData(this);
Log.d(TAG, "onCreated"); }
@Override public void onTerminate() { super.onTerminate();
Log.d(TAG, "onTerminated"); }
}
这就是我在我的 logcat 中得到的:
04-20 03:15:05.968: D/PhData(368): Constructor called
04-20 03:15:05.968: D/PhData(368): Initialized data
04-20 03:15:05.968: D/PharmacieApplication(368): onCreated
04-20 03:15:06.568: D/JoursListActivity(368): onCreated