2013-05-09 58 views
0

我的SQLite數據庫似乎正在輸入條目,但我無法將它們拉回來查看它們,每次我加載活動時,我的應用程序崩潰,更改數據庫版本會引發SQL異常並強制關閉應用程序。Android SQLite數據庫版本,已更改的結構現在無法查看數據庫

下面是其中的SQL被處理的類:

package com.uhi.fatfighter; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Stats { 

    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_WEIGHT = "weight"; 
    public static final String KEY_WAIST = "waist"; 
    public static final String KEY_CHEST = "chest"; 
    public static final String KEY_LEGS = "legs"; 
    public static final String KEY_ARMS = "arms"; 

    private static final String DATABASE_NAME = "statsDB"; 
    private static final String DATABASE_TABLE = "personalStats"; 
    private static final int DATABASE_VERSION = 3; 

    private DbHelper ffHelper; 
    private final Context ffContext; 
    private SQLiteDatabase ffDatabase; 

    private static class DbHelper extends SQLiteOpenHelper { 

     public DbHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 

     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID 
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_WEIGHT 
        + " TEXT NOT NULL, " + KEY_WAIST + " TEXT NOT NULL, " 
        + KEY_CHEST + " TEXT NOT NULL, " + KEY_LEGS 
        + " TEXT NOT NULL, " + KEY_ARMS + " TEXT NOT NULL);"); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE); 
      onCreate(db); 

     } 
    } 

    public Stats(Context c) { 
     ffContext = c; 
    } 

    public Stats open() throws SQLException { 
     ffHelper = new DbHelper(ffContext); 
     ffDatabase = ffHelper.getWritableDatabase(); 
     return this; 

    } 

    public void close() { 
     ffHelper.close(); 

    } 

    public long createEntry(String weight, String waist, String chest, String legs, String arms) { 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_WEIGHT, weight); 
     cv.put(KEY_WAIST, waist); 
     cv.put(KEY_CHEST, chest); 
     cv.put(KEY_LEGS, legs); 
     cv.put(KEY_ARMS, arms); 
     return ffDatabase.insert(DATABASE_TABLE, null, cv); 

    } 

    public String getData() { 
     String[] columns = new String[] { KEY_ROWID, KEY_WEIGHT, KEY_WAIST, KEY_CHEST, KEY_LEGS, KEY_ARMS }; 
     Cursor c = ffDatabase.query(DATABASE_TABLE, columns, null, null, null, 
       null, null); 
     String result = ""; 
     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iWeight = c.getColumnIndex(KEY_WEIGHT); 
     int iWaist = c.getColumnIndex(KEY_WAIST); 
     int iChest = c.getColumnIndex(KEY_CHEST); 
     int iLegs = c.getColumnIndex(KEY_LEGS); 
     int iArms = c.getColumnIndex(KEY_ARMS); 
     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
      result = result + c.getString(iRow) + " " + c.getString(iWeight) 
        + " " + c.getString(iWaist) 
        + " " + c.getString(iChest) 
        + " " + c.getString(iLegs) 
        + " " + c.getString(iArms) + "\n"; 

     } 

     return result; 
    } 
} 

以下是調用SQL處理類的活動:

package com.uhi.fatfighter; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.widget.TextView; 


public class DBView extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.view_stats); 
     TextView tv = (TextView) findViewById(R.id.tvDBInfo); 
     Stats dbInfo = new Stats(this); 
     dbInfo.open(); 

     String data = dbInfo.getData(); 
     dbInfo.close(); 
     if (!TextUtils.isEmpty(data)) { 
      tv.setText(data); 
     } 


    } 



} 

我說,這是工作的罰款,直到我說一些額外的領域

的logcat:

05-09 13:44:32.761: E/Trace(7576): error opening trace file: No such file or directory (2) 
05-09 13:44:32.894: E/InputDispatcher(1294): channel '415d5068 com.uhi.fatfighter/com.uhi.fatfighter.Splash (server)' ~ Channel is unrecoverably broken and will be disposed! 
05-09 13:44:34.964: E/Trace(7593): error opening trace file: No such file or directory (2) 
05-09 13:44:34.988: E/Trace(7598): error opening trace file: No such file or directory (2) 
05-09 13:44:54.250: E/AndroidRuntime(7598): FATAL EXCEPTION: main 
05-09 13:44:54.250: E/AndroidRuntime(7598): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi.fatfighter/com.uhi.fatfighter.DBView}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread.access$600(ActivityThread.java:142) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.os.Looper.loop(Looper.java:137) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread.main(ActivityThread.java:4928) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at dalvik.system.NativeStart.main(Native Method) 
05-09 13:44:54.250: E/AndroidRuntime(7598): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:747) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.Activity.setContentView(Activity.java:1867) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at com.uhi.fatfighter.DBView.onCreate(DBView.java:16) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.Activity.performCreate(Activity.java:5008) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139) 
05-09 13:44:54.250: E/AndroidRuntime(7598):  ... 11 more 
05-09 13:44:56.765: E/Trace(7630): error opening trace file: No such file or directory (2) 
05-09 13:45:10.125: E/Trace(7649): error opening trace file: No such file or directory (2) 
05-09 13:45:10.433: E/Trace(7664): error opening trace file: No such file or directory (2) 
05-09 13:45:11.183: E/Trace(7678): error opening trace file: No such file or directory (2) 
05-09 13:45:12.203: E/Trace(7695): error opening trace file: No such file or directory (2) 
05-09 13:45:14.187: E/AndroidRuntime(7630): FATAL EXCEPTION: main 
05-09 13:45:14.187: E/AndroidRuntime(7630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi.fatfighter/com.uhi.fatfighter.DBView}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread.access$600(ActivityThread.java:142) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.os.Looper.loop(Looper.java:137) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread.main(ActivityThread.java:4928) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at dalvik.system.NativeStart.main(Native Method) 
05-09 13:45:14.187: E/AndroidRuntime(7630): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:747) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.Activity.setContentView(Activity.java:1867) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at com.uhi.fatfighter.DBView.onCreate(DBView.java:16) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.Activity.performCreate(Activity.java:5008) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139) 
05-09 13:45:14.187: E/AndroidRuntime(7630):  ... 11 more 
05-09 13:45:18.433: E/Trace(7722): error opening trace file: No such file or directory (2) 
05-09 13:52:02.109: E/Trace(7767): error opening trace file: No such file or directory (2) 
05-09 13:52:03.554: E/Trace(7781): error opening trace file: No such file or directory (2) 
05-09 13:55:06.617: E/Trace(7807): error opening trace file: No such file or directory (2) 
05-09 13:55:26.808: E/AndroidRuntime(7807): FATAL EXCEPTION: main 
05-09 13:55:26.808: E/AndroidRuntime(7807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi.fatfighter/com.uhi.fatfighter.DBView}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread.access$600(ActivityThread.java:142) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.os.Looper.loop(Looper.java:137) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread.main(ActivityThread.java:4928) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at dalvik.system.NativeStart.main(Native Method) 
05-09 13:55:26.808: E/AndroidRuntime(7807): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:747) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.Activity.setContentView(Activity.java:1867) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at com.uhi.fatfighter.DBView.onCreate(DBView.java:16) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.Activity.performCreate(Activity.java:5008) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139) 
05-09 13:55:26.808: E/AndroidRuntime(7807):  ... 11 more 
05-09 13:58:40.242: E/Trace(7828): error opening trace file: No such file or directory (2) 
05-09 13:58:40.500: E/Trace(7841): error opening trace file: No such file or directory (2) 
05-09 13:58:40.937: E/PhotoDatabaseHelper(7828): query fail: empty cursor: [email protected] 
05-09 13:58:40.937: E/WidgetProvider(7828): cannot load widget: 5 

我可以看到班級在施放TextView時遇到了問題

+0

發佈例外。請。 – 2013-05-09 12:15:26

+0

它很難閱讀,因爲它被推到強制關閉消息後面,eclipse在'EXIST'附近顯示一個語法錯誤,並且我可能得到的錯誤部分必須與DROP TABLE IF EXTERN – dyatesupnorth 2013-05-09 12:24:45

回答

1

您的放置查詢是錯誤的。 EXIST是一個錯誤的關鍵字。

它應該是:

db.execSQL( 「DROP TABLE IF EXISTS」 + DATABASE_TABLE);

+0

附近的語法錯誤有關。能夠使用新版本號將字段添加到數據庫,但仍然無法查看字段。 – dyatesupnorth 2013-05-09 12:46:06

+0

當你使用這樣的實現時最重要的是,因爲你丟掉了表,所以數據庫中的所有數據都會丟失。確保你在表格中有數據。 – 2013-05-09 12:49:56

+0

我的插入語句似乎是成功的,我有一個try catch中的對話框,當我點擊'查看數據庫'時,應該會發現任何錯誤,但是應用程序組合關閉 – dyatesupnorth 2013-05-09 12:54:47

1
DROP TABLE IF EXISTS 

是正確的語法。

0

問題在於SQLITE視圖的XML文件,我檢查了我所有的TextView字段都設置正確,他們沒有。這解決了這個問題。