4

我正在使用我的应用程序对象(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
4

3 回答 3

4

在您要求访问数据库之前,不会实际创建数据库。从文档中:

创建一个帮助对象来创建、打开和/或管理数据库。此方法总是很快返回。在调用 getWritableDatabase() 或 getReadableDatabase() 之一之前,不会实际创建或打开数据库。

于 2013-04-20T02:34:30.160 回答
0

我正在更改您的代码,输入此代码并尝试。

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;
    private SQLiteDatabase db;

    // 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+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    C_CREATED_AT + " TEXT, " + 
                    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);
            onCreate(db);
        }
    } // end of dbhelper class


    public PhData(Context context) {
        this.context = context;
        this.dbHelper = new DbHelper(context);
        db=this.dbHelper.getWritableDatabase();
        db=this.dbHelper.getReadableDatabase();
        Log.d(TAG, "Initialized data");
    }

    public void close() {
        if (db != null)
            db.close();
    }

    public Database open() throws SQLException{
        this.dbHelper= new ContactsDBHelper(con);
        db=this.dbHelper.getWritableDatabase();
        db=this.dbHelper.getReadableDatabase();
        return this;
    }   

    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();
    }
}

可能对你有帮助。

于 2013-04-20T04:35:10.533 回答
0

实际上,我发现了导致我遇到另一个问题的源问题:我必须将 dbHelper 的上下文传递到构造函数中才能从我的 Application 类中调用它。

产生的另一个错误是:

public static final String C_SCHEDULE = "YYYY-MM-DD";

您不能在创建数据库时使用减号作为分隔符,因为虚拟机将其视为数学符号。所以我的声明必须改为:

public static final String C_SCHEDULE = "YYYY_MM_DD";
于 2013-04-20T20:34:30.070 回答