2012-03-14 68 views
1

我在android應用程序上有一個奇怪的問題,我沒有這樣的表格異常,但我確信數據庫存在。對於一些將我重定向到其他帖子在這裏我想補充說,我在我自己的手機上嘗試應用程序不是一個模擬器,這是因爲我在這裏讀了很多問題,談論模擬器上的相同問題。沒有這樣的表格:商品:

任何想法或提示將不勝感激。

活動的有錯誤的代碼:

... 

public class chartView extends Activity{ 
    Button editbtm; 
    Button cleanbtm; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.chart_layout); 
     editbtm = (Button) findViewById(R.id.editBasketbtm); 
     cleanbtm = (Button) findViewById(R.id.cleanBasketbtm); 
     //fletch all goods that should be presented on the text View 
     dbAdapter goodsfletcher = new dbAdapter(chartView.this); 
    // goodsfletcher.createGoodsTable(); 
     goodsfletcher.open(); 
     ... 

德將對DBAdapter類的代碼:

public class dbAdapter { 

    public static final String KEY_ARTI = "ARTI"; 
    public static final String KEY_PROV = "PROV"; 
    public static final String KEY_CODART = "CODART"; 
    public static final String KEY_PRECBOL ="PRECBOL"; 
    public static final String KEY_QUANT ="QUANT"; 

    private static final String TAG = "DbAdapter"; 
    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 


    private static final String DATABASE_CREATE = "create table goods (CODART VARCHAR(10) primary key, ARTI varchar(50) not null, PROV varchar(15) not null, PRECBOL FLOAT(6,2) not null, QUANT int not null);"; 

    private static final String DATABASE_NAME = "ketanoBase"; 
    private static final String DATABASE_TABLE = "goods"; 
    private static final int DATABASE_VERSION = 1; 

    private final Context mCtx; 

    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); 
     } 

     @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 goods"); 
      onCreate(db); 
     } 
    } 


    public dbAdapter(Context ctx) { 
     this.mCtx = ctx; 
    } 


    public dbAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     mDbHelper.close(); 
    } 



    public void addĜood(String code, String title, String source, String quantity, String price ) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_ARTI, title); 
     initialValues.put(KEY_CODART, code); 
     initialValues.put(KEY_PROV, source); 
     initialValues.put(KEY_PRECBOL, price); 
     initialValues.put(KEY_QUANT, quantity); 
     mDb.insert(DATABASE_TABLE, code, initialValues); 

    } 


    public boolean deleteGood(long rowId) { 

     return mDb.delete(DATABASE_TABLE, KEY_CODART + "=" + rowId, null) > 0; 
    } 

    public void eraseGoods(){ 
     String sql = "drop table if exists goods"; 
     mDb.execSQL(sql); 
    } 


    public Cursor fetchAllGoods() { 

     return mDb.query(DATABASE_TABLE, new String[] {KEY_CODART, KEY_ARTI, 
       KEY_PROV, KEY_PRECBOL, KEY_QUANT}, null, null, null, null, null); 
    } 

    public Cursor fetchGood(long rowId) throws SQLException { 

     Cursor mCursor = 

       mDb.query(true, DATABASE_TABLE, new String[] {KEY_CODART, 
         KEY_ARTI, KEY_PROV,KEY_PRECBOL, KEY_QUANT}, KEY_CODART + "=" + rowId, null, 
         null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 

    } 


    public boolean updateGood(long rowId, String quantity) { 
     ContentValues args = new ContentValues(); 

     args.put(KEY_QUANT,quantity); 

     return mDb.update(DATABASE_TABLE, args, KEY_CODART + "=" + rowId, null) > 0; 
    } 
} 

OG錯誤的logcat的:

03-14 09:52:05.668: W/KeyCharacterMap(30808): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
03-14 09:52:06.384: D/AndroidRuntime(30808): Shutting down VM 
03-14 09:52:06.384: W/dalvikvm(30808): threadid=1: thread exiting with uncaught exception (group=0x40018560) 
03-14 09:52:06.394: E/AndroidRuntime(30808): FATAL EXCEPTION: main 
03-14 09:52:06.394: E/AndroidRuntime(30808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.chartView}: android.database.sqlite.SQLiteException: no such table: goods: , while compiling: SELECT CODART, ARTI, PROV, PRECBOL, QUANT FROM goods 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.os.Looper.loop(Looper.java:130) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.main(ActivityThread.java:3835) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at java.lang.reflect.Method.invokeNative(Native Method) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at java.lang.reflect.Method.invoke(Method.java:507) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at dalvik.system.NativeStart.main(Native Method) 
03-14 09:52:06.394: E/AndroidRuntime(30808): Caused by: android.database.sqlite.SQLiteException: no such table: goods: , while compiling: SELECT CODART, ARTI, PROV, PRECBOL, QUANT FROM goods 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.remotedata.firstapp.dbAdapter.fetchAllGoods(dbAdapter.java:138) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.remotedata.firstapp.chartView.onCreate(chartView.java:31) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722) 
03-14 09:52:06.394: E/AndroidRuntime(30808): ... 11 more 
+0

該表尚未創建。你對SQLiteOpenHelper的使用在某種程度上是錯誤的 – zapl 2012-03-14 13:45:40

+0

因此,如果我在類的onCreate方法上創建它,表是不會被創建的? – 2012-03-14 13:51:03

+0

嗯,你在構造函數中訪問數據庫,並在oncreate中創建它。我也會說當你創建一個DatabaseHelper對象時,數據庫還沒有被創建。 – dan 2012-03-14 14:07:14

回答

2

我相信,數據庫文件存在,但該表不存在,如錯誤所述。您是否嘗試刪除數據庫文件並重新運行?由於文件存在,DatabaseHelper.onCreate()將不會被調用。只有在文件不存在並且創建了新數據庫時纔會調用它。

+0

我現在試試 – 2012-03-14 14:02:35

+0

感謝它現在的作品現在我添加一些代碼,以防止這種情況發生在用戶設備上.. – 2012-03-14 14:30:36