在Android Studio中没有使用静态sql数据库帮助程序(单例)调用onCreate()

时间:2014-08-05 08:37:46

标签: java android sqlite static singleton

我正在尝试实现静态数据库,因为我在重新打开已关闭的资源时遇到了很多问题。在调用getWritableDatabase()时,我希望调用DatabaseHelper的onCreate并开始设置我的表。但是,它不会被调用。

来自MainActivtity.java:

    public class MainActivity extends Activity {

    DatabaseHelper db;
    String buttonClicked;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        db = DatabaseHelper.getInstance(this);
    }

来自DatabaseHelper.java:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String LOG = "";
    private static DatabaseHelper sInstance;
    private static SQLiteDatabase db = null;
    private static Context context;

    // Database name
    private static final String DATABASE_NAME = "workout.db";

    // Database version number
    private static final int DATABASE_VERSION = 1;

    // Table names
    public static final String TABLE_DAY_OF_WEEK = "table_day_of_week";
    public static final String TABLE_DAY_HAS_EX = "table_day_exercise";
    public static final String TABLE_EXERCISE = "table_exercise";
    public static final String TABLE_EX_HAS_HIS = "table_exercise_history";
    public static final String TABLE_HISTORY = "table_history";

    // Common column names
    public static final String KEY_ID = "_id";
    public static final String COLUMN_EXERCISE_ID = "exercise_id";
    public static final String COLUMN_EXERCISE = "exerciseName";
    public static final String COLUMN_REPETITIONS = "numReps";
    public static final String COLUMN_NOTES = "notes";

    // Dayofweek table columns
    public static final String COLUMN_WEEKDAY = "dayName";
    public static final String COLUMN_SUMMARY = "summary";

    // Day Has Exercises table columns
    public static final String COLUMN_DAY_ID = "day_id";

    // Exercise Has History table columns
    public static final String COLUMN_HISTORY_ID = "history_id";

    // History table columns
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_COMPLETED_TIME = "time";


    // Dayofweek table creation statement
    private static final String CREATE_DAYOFWEEK_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_DAY_OF_WEEK + "( " + KEY_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_WEEKDAY + " TEXT, " + COLUMN_SUMMARY + " TEXT )";

    // Day Has Exercises  table creation statement
    private static final String CREATE_DAY_HAS_EX_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_DAY_HAS_EX + "( " + KEY_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DAY_ID + " INTEGER, " + COLUMN_EXERCISE_ID  + " INTEGER )";

    // Exercise table creation statement
    private static final String CREATE_EXERCISE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EXERCISE + "( " + KEY_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_EXERCISE + " TEXT, " + COLUMN_REPETITIONS + " INTEGER, " + COLUMN_NOTES + " TEXT )";

    // Exercise Has History table creation statement
    private static final String CREATE_EX_HAS_HISTORY_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EX_HAS_HIS + "( " + KEY_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_EXERCISE_ID + " INTEGER, " + COLUMN_HISTORY_ID + " INTEGER )";

    // History table creation statement
    private static final String CREATE_HISTORY_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_HISTORY + "( " + KEY_ID +
            " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_EXERCISE + " TEXT, " + COLUMN_WEEKDAY + " TEXT, " + COLUMN_REPETITIONS +
            " INTEGER, " + COLUMN_NOTES + " TEXT, " + COLUMN_DATE + " TEXT, " + COLUMN_COMPLETED_TIME + " TEXT )";

    private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
        DatabaseHelper.context = context;
    }

    public static synchronized DatabaseHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
            db = sInstance.getWritableDatabase();
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    // create required tables
    db.execSQL(CREATE_DAYOFWEEK_TABLE);
    db.execSQL(CREATE_EXERCISE_TABLE);

    // enter days of the week default entries
    insertWeekday("Monday", db);
    insertWeekday("Tuesday", db);
    insertWeekday("Wednesday", db);
    insertWeekday("Thursday", db);
    insertWeekday("Friday", db);
    insertWeekday("Saturday", db);
    insertWeekday("Sunday", db);
    }
    public boolean insertWeekday(String dayName, SQLiteDatabase db) {

    boolean createSuccessful = false;
    ContentValues values = new ContentValues();
    values.put(COLUMN_WEEKDAY, dayName);
    values.put(COLUMN_SUMMARY, "");

    createSuccessful = db.insertWithOnConflict(TABLE_DAY_OF_WEEK, null, values,
            SQLiteDatabase.CONFLICT_IGNORE) > 0;
    return createSuccessful;
    }

public ObjectDay readSummary(String dName/*, SQLiteDatabase db*/) {
    getReadableDatabase();

    ObjectDay objectDay = new ObjectDay();
    objectDay.setDayName(dName);

    String sql = "SELECT * FROM " + TABLE_DAY_OF_WEEK + " WHERE " + COLUMN_WEEKDAY + " = " + dName;

    Cursor cursor = db.rawQuery(sql, null);

    if (cursor.moveToFirst()) {

        int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_ID)));
        String summary = cursor.getString(cursor.getColumnIndex(COLUMN_SUMMARY));

        objectDay.setId(id);
        objectDay.setSummary(summary);
    }

    cursor.close();
    return objectDay;
}

我已经尝试重置AVD手机并擦除用户数据,但它没有帮助。我一直指的示例代码是

http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html

https://gist.github.com/jcxavier/1486739。任何见解都会非常感激。

编辑:好的,显然正在调用onCreate。问题发生在以后。我为一周中的几天创建列名。但是当我去查询数据库时,它说没有找到该列。我已经介入了它,看起来这些列已添加,所以我不知所措。

1 个答案:

答案 0 :(得分:0)

您需要在SQL中引用字符串文字:

String sql = "SELECT * FROM " + TABLE_DAY_OF_WEEK + " WHERE " + COLUMN_WEEKDAY + " = " + dName;

Cursor cursor = db.rawQuery(sql, null);

更改为:

String sql = "SELECT * FROM " + TABLE_DAY_OF_WEEK + " WHERE " + COLUMN_WEEKDAY + " = '" + dName + "'";

Cursor cursor = db.rawQuery(sql, null);

请注意dName附近的单引号。

甚至更好,使用参数:

String sql = "SELECT * FROM " + TABLE_DAY_OF_WEEK + " WHERE " + COLUMN_WEEKDAY + " = ?";

Cursor cursor = db.rawQuery(sql, new String[] { dName });

如果没有引号或?参数占位符,则会将其解释为标识符(列名称)而不是文字值。