Android SQLite数据库安装程序错误?

时间:2012-04-06 17:22:54

标签: android database android-logcat

我已经建立了一个SQLite数据库。似乎当我运行一个需要访问它的类时,应用程序崩溃了。最初有人向我指出,表创建语句是问题,但在错误解决之后它仍然无效。数据库设置是: -

public class DataBaseHelper
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CODE = "code";
    public static final String KEY_DAYS = "days";
    public static final String KEY_BMI = "bmi"; 
    public static final String KEY_ROWID2 = "id2";
    public static final String KEY_DATE = "date";
    public static final String KEY_STEPS = "steps";
    public static final String KEY_CALs = "calories";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "workout";
    private static final String DATABASE_TABLE = "goals";
    private static final String DATATABLE = "acts";

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table goals (_id integer primary key autoincrement, "
        + "code text not null, days text not null, " 
        + "bmi text not null);";
    private static final String DATABASE_CREATE2 =
        "create table acts (id2 integer primary key autoincrement, "
                + "date text not null, steps text not null, " 
                + "calories text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DataBaseHelper(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE2);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

//---opens the database---
    public DataBaseHelper open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

//---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

//---insert a title into the database---
    public long insertTitle(String isbn, String title, String publisher) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_CODE, isbn);
        initialValues.put(KEY_DAYS, title);
        initialValues.put(KEY_BMI, publisher);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
    public long insertActivity(String date, String steps, String Calories) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_STEPS, steps);
        initialValues.put(KEY_CALs, Calories);
        return db.insert(DATATABLE, null, initialValues);
    }

//---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
    }
    public void deleteFirst()
    {
        Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 

        if(cursor.moveToFirst()) {
            long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 

            db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + rowId, null);
        }
     }

    public boolean deleteAct(long rowId) 
    {
        return db.delete(DATATABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
    }
//---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_CODE,
            KEY_DAYS,
            KEY_BMI}, 
            null, 
            null, 
            null, 
            null, 
            null);
}
public Cursor getAllActs() 
{
    return db.query(DATATABLE, new String[] {
            KEY_ROWID2, 
            KEY_DATE,
            KEY_STEPS,
            KEY_CALs}, 
            null, 
            null, 
            null, 
            null, 
            null);
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_CODE, 
                    KEY_DAYS,
                    KEY_BMI
                    }, 
                    KEY_ROWID + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
public Cursor getAct(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATATABLE, new String[] {
                    KEY_ROWID2,
                    KEY_DATE, 
                    KEY_STEPS,
                    KEY_CALs
                    }, 
                    KEY_ROWID2 + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
public boolean updateAct(long rowId, String date, 
        String steps, String cals) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_DATE, date);
            args.put(KEY_STEPS, steps);
            args.put(KEY_CALs, cals);
            return db.update(DATATABLE, args, 
                             KEY_ROWID2 + "=" + rowId, null) > 0;
        }

//---updates a title---
   public boolean updateTitle(long rowId, String isbn, 
   String title, String publisher) 
   {
       ContentValues args = new ContentValues();
       args.put(KEY_CODE, isbn);
       args.put(KEY_DAYS, title);
       args.put(KEY_BMI, publisher);
       return db.update(DATABASE_TABLE, args, 
                     KEY_ROWID + "=" + rowId, null) > 0;
   } 
}

并且Log cat是:

04-06 17:20:14.561: E/AndroidRuntime(24550): FATAL EXCEPTION: main
04-06 17:20:14.561: E/AndroidRuntime(24550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: no such column: bmi: , while compiling: SELECT _id, code, days, bmi FROM goals
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.os.Looper.loop(Looper.java:144)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.app.ActivityThread.main(ActivityThread.java:4937)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at java.lang.reflect.Method.invoke(Method.java:521)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at dalvik.system.NativeStart.main(Native Method)
04-06 17:20:14.561: E/AndroidRuntime(24550): Caused by: android.database.sqlite.SQLiteException: no such column: bmi: , while compiling: SELECT _id, code, days, bmi FROM goals
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1412)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1296)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1251)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1331)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at com.b00348312.workout.DataBaseHelper.getAllTitles(DataBaseHelper.java:131)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at com.b00348312.workout.WorkoutProgress.fillData(WorkoutProgress.java:27)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:22)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
04-06 17:20:14.561: E/AndroidRuntime(24550):    at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
04-06 17:20:14.561: E/AndroidRuntime(24550):    ... 11 more

我告诉我的问题是:

 04-06 17:20:14.561: E/AndroidRuntime(24550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: no such column: bmi: , while compiling: SELECT _id, code, days, bmi FROM goals

但是我不会在它所指的地方每次调用字段bmi时似乎都没有问题。有什么我想念的吗?

2 个答案:

答案 0 :(得分:2)

您正在尝试访问数据库中名为“bmi”的列,但该列不存在。

您很可能在创建数据库后添加了列,并且未修改openHelper以正确升级数据库。

添加“bmi”字段,将解决错误。由于数据库已存在,因此更改Create语句是不够的,因此不会运行Create语句。您应该覆盖“onUpgrade”以从一个数据库版本更改为另一个数据库版本。

或者删除当前数据库并从头开始重新创建。

答案 1 :(得分:1)

你必须卸载然后重新安装你的应用程序,因为如果没有实现onUpgrade,android不会更改数据库。

或转到设置和您的应用并清除应用数据,然后运行您的应用。