2011-07-09 80 views
0

您好我想知道什麼時候當我創建MyDatabase的Manager類的對象SqliteOPenHelper的onCreate()方法將稱爲SqliteOpenHelper onCreate()方法

問題是,當我運行程序的時候,創建數據庫,但表不創建,它拋出異常表不退出。

package pk.com.db; 

import org.apache.http.Header; 

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

public class AdatabaseManager { 

    // Constant through out the class we use 

    private SQLiteDatabase sqlitedatabase; //reference to the database manaager 
    private final String DB_NAME = "MYBooks"; 
    private final int DB_VERSION= 1; 


    //Now table name and it's fields 

    private final String DB_TABLE="Book_Details"; //this all constant are use as Key 
    private final String FK_ID="ID"; 
    private final String Field_ONE="NAME"; 
    private final String Field_TWO="PRICE"; 
    private final String Field_THREE="AUTHER"; 
    private CustomSqliteOpenHelper helper; 

    private static final String DATABASE_CREATE = 
      "create table if not exites Book_Details(_id integer primary key autoincrement, " 
      + "NAME text not null,PRICE text not null, " 
      + "AUTHER text not null);"; 



    public AdatabaseManager(Context context){ 
     helper=new CustomSqliteOpenHelper(context); 
     this.sqlitedatabase=helper.getReadableDatabase(); 


    } 
    public void openDb(){ 
     try 
     { 
      this.sqlitedatabase=helper.getWritableDatabase(); 



     }catch (Exception e) { 

      Log.i(getClass().getName(), e.toString()); 
      System.out.print(e); 
     } 

    } 
    public void closedb(){ 
     try{ 
      helper.close(); 

     }catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    public void addRow(String strName,String strPrice,String strAuother){ 
     try{ 

      ContentValues values=new ContentValues(); 
      values.put(Field_ONE,strName); 
      values.put(Field_TWO,strPrice); 
      values.put(Field_THREE,strAuother); 

      sqlitedatabase.insert(DB_TABLE,null,values); 

     }catch (Exception e) { 
      Log.i(getClass().getName(),e.toString()); 

     }  
    } 

    private class CustomSqliteOpenHelper extends SQLiteOpenHelper 
{ 
    public CustomSqliteOpenHelper(Context context){ 

     super(context,DB_NAME,null,DB_VERSION); 

    } 


    **@Override 
    public void onCreate(SQLiteDatabase db) { 
    try{ 



      db.execSQL(DATABASE_CREATE); 



    }catch (Exception e) { 
     System.out.println("********************************"); 
     System.out.print(e); 
     System.out.println("********************************"); 
    } 

    }** 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

} 

}

在這裏,我打電話給我的AdatabaseManager

//************************************ 
import android.app.Activity; 
import android.os.Bundle; 

public class activity_db extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     AdatabaseManager _adm=new AdatabaseManager(this); //it think it open database 
     /* _adm.openDb(); 
     _adm.addRow("ANDROID EBOOKS","2999","KINGRAJARANN"); 
     _adm.closedb();*/ 


    } 
} 
+0

如果我是讓你的權利,雅有權假設..得到的onCreate只要你實例CustomSqliteOpenHelper ......它只是你可以覆蓋並自動調用是Android SDK中的回調調用。 –

+0

但它不會創建表,告訴我,當它創建的表,按當第一次創建數據庫我的知識。 –

回答

0

問題的至少一部分是在你的DATABASE_CREATE串拼寫錯誤:

private static final String DATABASE_CREATE = 
     "create table if not exites Book_Details(_id integer primary key autoincrement, " 
     + "NAME text not null,PRICE text not null, " 
     + "AUTHER text not null);"; 

「存在的詞「被拼寫爲」exite「。修正,這將是:

private static final String DATABASE_CREATE = 
     "create table if not exists Book_Details(_id integer primary key autoincrement, " 
     + "NAME text not null,PRICE text not null, " 
     + "AUTHER text not null);"; 

順便問一下,你的代碼並不顯示您的openDB方法是否在任何地方調用。 onCreate實際上不會被調用,直到你打電話getWritableDatabase,這是通過你的openDB方法調用。

相關問題