Android崩溃SQLite无法复制

时间:2014-07-18 12:34:02

标签: android sqlite

我发布了一个Android应用程序,我在其中更新了添加SQLite数据库的新版本。我的所有测试似乎都运行良好。但是,由于我在过去2天内发布了大约300个用户更新,因此我有9个用户报告完全相同的崩溃。

第一次调用SQLite数据库时出现错误。所以我的第一个想法是SQLite数据库创建和初始插入语句在第一次调用之前是不完整的。

这是错误:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.janzen.bodybuildingdiet/com.janzen.bodybuildingdiet.DietActivity}:    
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while   
compiling: UPDATE DAYMEAL SET DAY_INT=
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): ,    
while compiling: UPDATE DAYMEAL SET DAY_INT=
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at   
 android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:923)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:534)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at  android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:65)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1370)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1309)
at com.janzen.bodybuildingdiet.StatusData.updateFieldDayMeal(StatusData.java:1083)
at com.janzen.bodybuildingdiet.DietActivity.setupInitialDay(DietActivity.java:124)
at com.janzen.bodybuildingdiet.DietActivity.onCreate(DietActivity.java:103)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)

这是UPDATE DAYMEAL发生错误的方法,但是我不认为这个方法的代码是问题,因为它在App的其他部分运行良好。

    public void updateFieldDayMeal(String updateArray[]) {
    open();
    String fieldToUpdate = updateArray[0];
    String valueToUpdate = updateArray[1];
    String query = "UPDATE DAYMEAL SET " + fieldToUpdate + "="
            + valueToUpdate;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    c.close();
    db.close();
}

这是我的onCreate(在我的应用程序的主要活动中)和setupInitialDay方法,它调用SQLite类:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_diet);

    statusData = new StatusData(getBaseContext());
    setupButtons();
    setupProgressBar();
    setupInitialDay();
    setupValues();

    firstTime();
    populateDietList();
    registerClickCallback();

    Appirater.appLaunched(this);

}

private void setupInitialDay(){

    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Date d = new Date();
    String dayOfTheWeek = mic.dayWeekNumber(sdf.format(d));

    String [] updateArray = new String[] { "day_int", dayOfTheWeek };
    statusData.updateFieldDayMeal(updateArray);
}

public String dayWeekNumber (String day) {
    String dayNum = "";
    if (day.equals("Sunday")) {
        dayNum = "7";
    } else if (day.equals("Tuesday")) {
        dayNum = "2";
    } else if (day.equals("Wednesday")) {
        dayNum = "3";
    } else if (day.equals("Thursday")) {
        dayNum = "4";
    } else if (day.equals("Friday")) {
        dayNum = "5";
    } else if (day.equals("Saturday")) {
        dayNum = "6";
    } else if (day.equals("Monday") {
        dayNum = "1";
    }
    return dayNum;
}

2 个答案:

答案 0 :(得分:0)

如果您在非英语设备上使用英文日名称,则将工作日转换为包含SimpleDateFormat的字符串,然后通过比较此字符串转换回数字将无效。

不是使用字符串,而是创建Calendar object;然后使用get(Calendar.DAY_OF_WEEK),然后将数字转换为您自己的数字。

答案 1 :(得分:-1)

而是使用execSQL来更新 -

public void updateFieldDayMeal(String updateArray[]) {
    open();
    String fieldToUpdate = updateArray[0];
    String valueToUpdate = updateArray[1];
    String query = "UPDATE DAYMEAL SET " + fieldToUpdate + "="
            + valueToUpdate;

    db.execSQL(query);

    db.close();
}