无法在我的Android应用程序中创建SQLite数据库

时间:2013-04-20 02:21:02

标签: android sqlite android-sqlite oncreate

我正在使用我的应用程序对象(Android building bloc)来调用我的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

3 个答案:

答案 0 :(得分:4)

在您要求访问数据库之前,实际上不会创建数据库。来自documentation

  

创建辅助对象以创建,打开和/或管理数据库。这个   方法总是很快返回。实际上并不是数据库   创建或打开,直到getWritableDatabase()之一或   调用getReadableDatabase()。

答案 1 :(得分: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();
    }
}

可能对你有帮助。

答案 2 :(得分:0)

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

生成的另一个错误是:

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

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

public static final String C_SCHEDULE = "YYYY_MM_DD";